python中image显示图片,在Python中使用PhotoImage在Tkinter中显示图像

我试图创建一个有主菜单和设置菜单的应用程序。我想为每一个设置背景。但我从设置菜单开始。我收到一个错误声明:

_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()