Python tkinter不在窗体上显示图像
我一辈子也弄不明白这为什么行不通。我使用TKinter在Python中显示图像。到目前为止,我编写的代码如下所示:Python tkinter不在窗体上显示图像,python,forms,image,file,tkinter,Python,Forms,Image,File,Tkinter,我一辈子也弄不明白这为什么行不通。我使用TKinter在Python中显示图像。到目前为止,我编写的代码如下所示: from tkinter import * from tkinter import messagebox def unlock(): root.withdraw() def logout(): inside.destroy() root.deiconify() ####################### #
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)
该代码目前并没有太大作用,但我正在研究它。当我运行该程序时,它将显示一个保险柜(很棒)的图片,当我单击按钮时,它将移到下一个窗体
在新表单上,图像、标签和按钮不会显示,但是,当图像代码被删除时,所有操作都很顺利
起初,我考虑将根表单放在函数中,但是,每当我将此代码放在函数中时,它都无法加载映像(ahhh)。这些图像不能放在函数中吗?您需要保留对照片的引用
imageInside = PhotoImage(file = "images/inside.gif")
imageLabel = Label(inside,image = imageInside)
imageLabel.grid(row =1, columnspan = 2)
imageLabel.photo_ref = imageInside # keep a reference!
谢谢你的帮助 我可以确认这是有效的,这是代码
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)
可能您出错了,它无法显示表单。您是否在控制台中运行它以查看是否收到错误消息?顺便说一句:当您执行
var=Widget(…).grid()
时,它会将None
分配给var
,因为grid()
(和pack()
)返回None
,而不是Widget。如果以后需要var
访问小部件,则必须使用两行代码`var=widget(…)
和var.grid()
。如果您以后不需要使用var
,那么您可以在不使用var
的情况下创建小部件。这意味着Widget(…).grid()
tkinter应该只使用一个Tk()
-来创建主窗口。您应该使用Toplevel()
BTW创建的其他窗口:您忘记了root.mainloop()
@furas我不知道您的意思是什么?很多都是自学的,所以我可能养成了很多坏习惯。你将如何创建一个新窗口?比你的帮助,但没有工作。它返回了以下内容:imageLabel=Label(内部,image=imageInside)文件“C:\Program Files\Python36\lib\tkinter\u init\uuu0.py”,第2760行,在init小部件中。uuu init\u0(self,master,'Label',cnf,kw)文件“C:\Program Files\Python36\lib\tkinter\u init\u0.py”,第2293行,在init(widgetName,self.+extra+self)选项(cnf))\u tkinter.TclError:图像“pyimage2”没有exist@NeosNokia你应该把它编辑成你的问题。