首页 > 解决方案 > 我无法导入照片,因此它会出现在我的应用程序“pyimage1”不存在的按钮上

问题描述

在创建帖子之前,我尝试通过搜索和阅读其他用户错误来解决我的问题,当涉及到“pyimage1”不存在时,我无法将其应用于我的代码以使其有效工作。

我不太确定此时该尝试什么才能让这些按钮获得一些照片!我已经阅读了其他有同样问题的人的大量线程,但我无法让它工作。

错误代码:

Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):文件“C:\Users\korey\AppData\Local\Programs\Python\Python38\lib\tkinter__init__.py”,第 1883 行,调用 返回 self.func(*args ) 文件“C:/Users/korey/PycharmProjects/test rental/gui test.py”,第 186 行,在 car_menu_screen 按钮中(text="Coupe", height="15", width="75", command=login, image=coupephoto).place(x=420, y=220) 文件“C:\Users\korey\AppData\Local\Programs\Python\Python38\lib\tkinter__init__.py”,第 2645 行,在init Widget 中。init (self, master, 'button', cnf, kw) 文件“C:\Users\korey\AppData\Local\Programs\Python\Python38\lib\tkinter__init__.py”,第 2567 行,在init self.tk.call(_tkinter.TclError:图像“pyimage1”不存在

def car_menu_screen():
    global car_menu

    main_screen.destroy()  # Closes the main screen (login or register)
    car_menu = Tk()
    car_menu.geometry("1920x1080")  # Sets Window Size
    car_menu.title("Vehicle Choice Menu")  # Sets Window Title
    Label(text="What Type Of Vehicle Would You Like to Rent?", bg="red", width="300", height="2",
          font=("Arial Black", 13)).pack()
    Label(text="").pack()
    coupephoto = PhotoImage(file="coupe.png")
    sedanphoto = PhotoImage(file="sedan.png")
    suvphoto = PhotoImage(file="suv.png")
    sportsphoto = PhotoImage(file="sports.png")

    Button(text="Coupe", image=coupephoto, height="150", width="350", command=login).place(x=420, y=220)
    Label(text="").pack()
    Button(text="Sedan", image=sedanphoto, height="150", width="350", command=register).place(x=960, y=220)
    Label(text="").pack()
    Button(text="SUV", image=suvphoto, height="150", width="350", command=register).place(x=420, y=500)
    Label(text="").pack()
    Button(text="Sports", image=sportsphoto, height="150", width="350", command=register).place(x=960, y=500)

标签: pythonpython-3.xtkinter

解决方案


在函数末尾添加car_menu.mainloop()有效。

我不确定,但我得到了TclError: image "pyimage1" doesn't exist一个错误,tk 窗口没有打开。这可能是因为我使用 Jupyter 并重置 Jupyter 工作正常。

from tkinter import*

def car_menu_screen():
    global car_menu

    main_screen.destroy()  # Closes the main screen (login or register)
    car_menu = Tk()
    car_menu.geometry("1920x1080")  # Sets Window Size
    car_menu.title("Vehicle Choice Menu")  # Sets Window Title
    Label(text="What Type Of Vehicle Would You Like to Rent?", bg="red", width="300", height="2",
          font=("Arial Black", 13)).pack()
    Label(text="").pack()
    coupephoto = PhotoImage(file="coupe.png")
    sedanphoto = PhotoImage(file="sedan.png")
    suvphoto = PhotoImage(file="suv.png")
    sportsphoto = PhotoImage(file="sports.png")

    Button(text="Coupe", image=coupephoto, height="150", width="350", command=login).place(x=420, y=220)
    Label(text="").pack()
    Button(text="Sedan", image=sedanphoto, height="150", width="350", command=register).place(x=960, y=220)
    Label(text="").pack()
    Button(text="SUV", image=suvphoto, height="150", width="350", command=register).place(x=420, y=500)
    Label(text="").pack()
    Button(text="Sports", image=sportsphoto, height="150", width="350", command=register).place(x=960, y=500)

    car_menu.mainloop()

car_menu_screen()

这是我的截图。(我对此发表了评论main_screen.destroy()。)

在此处输入图像描述


推荐阅读