【问题标题】:Using PhotoImage to display an image in Tkinter in Python在 Python 中使用 PhotoImage 在 Tkinter 中显示图像
【发布时间】:2015-04-14 05:43:11
【问题描述】:

我正在尝试创建一个具有主菜单和设置菜单的应用程序。我想为每一个设置背景。但我从设置菜单开始。我收到一条错误消息: _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()

【问题讨论】:

  • 哪一行给出了错误?
  • 这些行是应该显示图片的位置:background = PhotoImage(file = install_directory + r'\resources\backgrounds\subtle.gif') label = Label(options_window, image=background) label.pack()

标签: python tkinter photoimage


【解决方案1】:

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

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

 options_window = Toplevel()

希望这会有所帮助。还要确保您的图像文件路径正确。

【讨论】:

  • 消除了错误,但图片仍然没有显示在选项窗口中。我只是使用相同的代码:`background = PhotoImage(file='...') label = Label(options_window, image=background) label.pack()` 将相同的背景添加到主窗口,所以我不知道是什么问题。
  • 您能否编辑您的问题并提供更新的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多