【问题标题】:tkinter not displaying images on formstkinter 不在表单上显示图像
【发布时间】:2017-11-24 17:26:07
【问题描述】:

我一生都无法弄清楚为什么这不起作用。我正在使用 TKinter 在 Python 中显示图像。到目前为止我编写的代码如下所示:

from tkinter import *
from tkinter import messagebox

def unlock():
    root.withdraw()


    def logout():
        inside.destroy()
        root.deiconify()

    #######################
    ###                 ###
    ###  unlock Form    ###
    ###                 ###
    #######################


    inside = Tk()
    inside.geometry("576x576")
    inside.title("SAFE CRACKER")
    # LABELS, Textboxes and Buttons
    imageInside = PhotoImage(file = "images/inside.gif")
    imageLabel = Label(inside,image = imageInside).grid(row =1, columnspan = 2)
    label = Label(inside, text="Safe Cracker", font = ("Arial",16)).grid(row = 0, columnspan = 2)
    exitButton = Button(inside, text = "Exit", width = 15, command = logout )
    exitButton.grid(row = 3, column = 0,padx = 10, pady = 10)



#######################
###                 ###
###    Main Form    ###
###                 ###
#######################


root = Tk()
root.geometry("430x450")
root.title("SAFE CRACKER")
# LABELS, Textboxes and Buttons
label = Label(root, text="Safe Cracker", font = ("Arial",16)).grid(row = 0, columnspan = 2)
imageSafe = PhotoImage(file = "images/safe.gif")
imageLabel = Label(root,image = imageSafe).grid(row =1, columnspan = 2)
label = Label(root, text = "Enter the code", font = ("Arial",12)).grid(row  = 2, column = 0)
unlockCode = Entry(root, width = 30)
unlockCode.grid(row = 2, column = 1,padx = 10, pady = 10)
exitButton = Button(root, text = "Exit", width = 15, command = exit).grid(row = 3, column = 0,padx = 10, pady = 10)
enterButton = Button(root, text = "Enter the VAULT", width = 15, command = unlock).grid(row = 3, column = 1,padx = 10, pady = 10)

该代码目前并没有做太多的事情,但这是我正在处理的事情。当我运行程序时,它会显示一张保险箱的图片(很棒),当我点击按钮时,它会移动到下一个表单。

image of safe working

在新表单上,图像、标签和按钮不显示,但是,当图像代码被删除时,一切都可以正常工作。

最初我考虑将根表单放在函数中,但是,每当我将此代码放在函数中时,它都无法加载图像(啊哈)。这些图像不能放在函数中吗?

【问题讨论】:

  • 也许你得到了错误,它不能显示表格。您是否在控制台中运行它以查看是否收到错误消息?
  • 顺便说一句:当您执行var = Widget(...).grid() 时,它会将None 分配给var,因为grid()(和pack())返回None,而不是小部件。如果您稍后需要var 来访问小部件,那么您必须在两行代码中完成它`var = Widget(...)var.grid()。如果您以后不需要使用var,那么您可以在没有var 的情况下创建小部件。这意味着Widget(...).grid()
  • tkinter 应该只使用一个Tk() - 创建主窗口。您应该使用Toplevel() 创建的其他窗口
  • 顺便说一句:你忘了root.mainloop()
  • @furas 我不确定你的意思?其中很多都是自学的,因此我可能养成了很多坏习惯。您将如何创建一个新窗口?

标签: python forms image file tkinter


【解决方案1】:

您需要保留对照片的引用。

imageInside = PhotoImage(file = "images/inside.gif")
imageLabel = Label(inside,image = imageInside)
imageLabel.grid(row =1, columnspan = 2)
imageLabel.photo_ref = imageInside # keep a reference!

【讨论】:

  • 感谢您的帮助,但没有奏效。它回来了: imageLabel = Label(inside,image = imageInside) File "C:\Program Files\Python36\lib\tkinter_init_.py", line 2760, in init Widget.__init__(self, master, 'label', cnf, kw) 文件“C:\Program Files\Python36\lib\tkinter_init_.py”,第 2293 行,在 init (widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: image "pyimage2" doesn't exist
  • @NeosNokia 您应该将其编辑为您的问题。
【解决方案2】:

感谢您的帮助。

我可以确认这是可行的,这是代码。

from tkinter import *
from tkinter import messagebox

def unlock():
    root.withdraw()


    def logout():
        inside.destroy()
        root.deiconify()

    #######################
    ###                 ###
    ###  unlock Form    ###
    ###                 ###
    #######################


    inside = Toplevel()
    inside.geometry("576x576")
    inside.title("SAFE CRACKER")
    # LABELS, Textboxes and Buttons
    imageInside = PhotoImage(file = "images/inside.gif")
    imageLabel = Label(inside,image = imageInside)
    imageLabel.grid(row =1, columnspan = 2)
    imageLabel.photo_ref = imageInside
    label = Label(inside, text="Safe Cracker", font = ("Arial",16)).grid(row = 0, columnspan = 2)
    exitButton = Button(inside, text = "Exit", width = 15, command = logout )
    exitButton.grid(row = 3, column = 0,padx = 10, pady = 10)



#######################
###                 ###
###    Main Form    ###
###                 ###
#######################


root = Tk()
root.geometry("430x450")
root.title("SAFE CRACKER")
# LABELS, Textboxes and Buttons
label = Label(root, text="Safe Cracker", font = ("Arial",16)).grid(row = 0, columnspan = 2)
imageSafe = PhotoImage(file = "images/safe.gif")
imageLabel = Label(root,image = imageSafe).grid(row =1, columnspan = 2)
label = Label(root, text = "Enter the code", font = ("Arial",12)).grid(row  = 2, column = 0)
unlockCode = Entry(root, width = 30)
unlockCode.grid(row = 2, column = 1,padx = 10, pady = 10)
exitButton = Button(root, text = "Exit", width = 15, command = exit).grid(row = 3, column = 0,padx = 10, pady = 10)
enterButton = Button(root, text = "Enter the VAULT", width = 15, command = unlock).grid(row = 3, column = 1,padx = 10, pady = 10)

【讨论】:

    猜你喜欢
    • 2017-08-26
    • 2016-12-16
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多