0

我正在尝试创建一个具有主菜单和设置菜单的应用程序。我想为每一个设置背景。但我从设置菜单开始。我收到一条错误消息: _tkinter.TclError: image "pyimage1" doesn't exist. 我究竟做错了什么?

from tkinter import *
from tkinter.ttk import *

install_directory = '...'


# ***********************************MAIN MENU*****************************************************
def root():

    # ~~~Defines window~~~
    main_window = Tk()
    main_window.iconbitmap(install_directory + r'\resources\icons\logo.ico')  # Changes the icon for window
    main_window.title('Auto Transfer')  # Changes window name
    main_window.geometry("300x200")

    # ~~Adds a background~~~
    background = PhotoImage(file=install_directory + r'\resources\backgrounds\stardust.gif')
    label = Label(main_window, image=background)
    label.pack()

    # ~~~Menu Bar~~~
    menubar = Menu(main_window)  # Creates the menu bar

    # ~~~File menu~~~
    filemenu = Menu(menubar, tearoff=0)
    filemenu.add_command(label="Quit", command=lambda: main_window.destroy())  # Exits the program

    # ~~~Settings menu~~~
    settingsmenu = Menu(menubar, tearoff=0)
    settingsmenu.add_command(label="Change settings...", command=lambda: options(main_window))

    # ~~~Add menus to bar~~~
    menubar.add_cascade(label='File', menu=filemenu)
    menubar.add_cascade(label='Settings', menu=settingsmenu)

    # ~~Adds menu bar to the screen~~~
    main_window.config(menu=menubar)

    # ~~Adds 'RUN' button~~


    # ~~~Runs window~~~
    main_window.mainloop()


# *********************************OPTIONS MENU****************************************************
def options(main_window):

    options_window = Toplevel()
    options_window.iconbitmap(install_directory + r'\resources\icons\logo.ico')  # Changes the icon for window
    options_window.title('Settings')  # Changes window name
    options_window.geometry("720x480")

    # ~~Adds a background~~~
    background = PhotoImage(file=install_directory + r'\resources\backgrounds\stardust.gif')
    label = Label(options_window, image=background)
    label.pack()




# *******************************RUN APP**************************************************************
if __name__ == '__main__':
    root()
4

1 回答 1

0

我认为原因是您Tk()在代码中使用了两个实例。这不是很好。一个 tkinter 应用程序应该只有一个主循环(即一个实例 Tk())。要制作其他窗口,请使用 TopLevel 小部件。

在您的选项函数中使用它而不是创建新的 Tk():

 options_window = Toplevel()

希望这可以帮助。还要确保您的图像文件路径正确。

于 2015-04-14T06:00:23.420 回答