我无法为我的生活解决为什么这不起作用。我正在使用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)
目前代码并没有做太多,但这是我正在努力的事情。当我运行该程序时,它将显示一个安全图片(很棒),当我点击按钮时,它会移动到下一个表格。
在新表格上,图像,标签和按钮不会显示,但是,当图像代码被删除时,它们全部都可以正常工作。
最初我想过将根表单放在一个函数中,但是,每当我将这段代码放在一个函数中时,它都无法加载图像(ahhhh)。这些图像可以放在功能中吗?
答案 0 :(得分: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!
答案 1 :(得分:0)
感谢您的帮助。
我可以确认这是有效的,这是代码。
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)