简体   繁体   English

在Python中使用PhotoImage在Tkinter中显示图像

[英]Using PhotoImage to display an image in Tkinter in Python

I'm trying to create an application that has a main menu and a settings menu. 我正在尝试创建一个具有主菜单和设置菜单的应用程序。 I want to set background for each of these. 我想为每个背景设置背景。 But I'm starting with the settings menu. 但是我从设置菜单开始。 I'be been getting an error stating: _tkinter.TclError: image "pyimage1" doesn't exist . 我收到一条错误消息: _tkinter.TclError: image "pyimage1" doesn't exist What am I doing wrong? 我究竟做错了什么?

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

I think the reason is that you use two Tk() instances in your code. 我认为原因是您在代码中使用了两个Tk()实例。 This is not very good. 这不是很好。 A tkinter application should only have one mainloop (ie one instance Tk()). 一个tkinter应用程序应该只有一个主循环(即一个实例Tk())。 To make other windows, use TopLevel widget. 要制作其他窗口,请使用TopLevel小部件。

Use this in your options function instead of making new Tk(): 在您的选项函数中使用此函数,而不要创建新的Tk():

 options_window = Toplevel()

Hope this helps. 希望这可以帮助。 Also make sure your image file paths are correct. 还要确保图像文件路径正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM