如何使用以下代码使图像出现在 python 中的此按钮上?
How do I make an image appear on this button in python with the following code?
所以我需要在我在 python 中创建的按钮上获取我的图像。但它弹出错误 (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage2" doesn't exist
。另外,我已经将程序的主要 window 放在一个函数中。然后我做到了,当 python 完成该功能后,它会打开这个启动程序 window,您可以在其中登录并访问 LifeLoginWindow
。但是当我摆脱这个启动并调用 LifeLoginWindow
函数时,它运行完美并且我可以看到图像。所以我不知道发生了什么。这是我的代码:
from tkinter import*
def hello():
print("Works, and hello!")
def LifeLoginWindow():
window = Tk()
TrendsButton = PhotoImage(file = "Trends_Button.png")
TrendsButton_label = Button(SideBar, image = TrendsButton, command = hello)
TrendsButton_label.pack(side=TOP)
SideBar.pack(side = LEFT, fill = Y)
window.mainloop()
StartUp = Tk()
def LoginFunction():
StartUp.destroy
LifeLoginWindow()
StartUpIcon = PhotoImage(file = "Life Login Image Resized.png")
StartUpIcon_label = Label(StartUp, image = StartUpIcon)
StartUpIcon_label.grid(row = 0, column = 2)
LoginButt = Button(StartUp, text = "Login", command = LoginFunction)
LoginButt.grid(row = 3, column = 2)
print("_Loaded Start Up...")
StartUp.mainloop()
您需要保留参考:
When a PhotoImage object is garbage-collected by Python (e.g. when you return from a function which stored an image in a local variable), the image is cleared even if it’s being displayed by a Tkinter widget.
To avoid this, the program must keep an extra reference to the image object. A simple way to do this is to assign the image to a widget attribute, like this:
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
有关 Tkinter PhotoImage 的更多信息,请参阅 this page。
所以我需要在我在 python 中创建的按钮上获取我的图像。但它弹出错误 (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage2" doesn't exist
。另外,我已经将程序的主要 window 放在一个函数中。然后我做到了,当 python 完成该功能后,它会打开这个启动程序 window,您可以在其中登录并访问 LifeLoginWindow
。但是当我摆脱这个启动并调用 LifeLoginWindow
函数时,它运行完美并且我可以看到图像。所以我不知道发生了什么。这是我的代码:
from tkinter import*
def hello():
print("Works, and hello!")
def LifeLoginWindow():
window = Tk()
TrendsButton = PhotoImage(file = "Trends_Button.png")
TrendsButton_label = Button(SideBar, image = TrendsButton, command = hello)
TrendsButton_label.pack(side=TOP)
SideBar.pack(side = LEFT, fill = Y)
window.mainloop()
StartUp = Tk()
def LoginFunction():
StartUp.destroy
LifeLoginWindow()
StartUpIcon = PhotoImage(file = "Life Login Image Resized.png")
StartUpIcon_label = Label(StartUp, image = StartUpIcon)
StartUpIcon_label.grid(row = 0, column = 2)
LoginButt = Button(StartUp, text = "Login", command = LoginFunction)
LoginButt.grid(row = 3, column = 2)
print("_Loaded Start Up...")
StartUp.mainloop()
您需要保留参考:
When a PhotoImage object is garbage-collected by Python (e.g. when you return from a function which stored an image in a local variable), the image is cleared even if it’s being displayed by a Tkinter widget.
To avoid this, the program must keep an extra reference to the image object. A simple way to do this is to assign the image to a widget attribute, like this:
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
有关 Tkinter PhotoImage 的更多信息,请参阅 this page。