我正在开发我的第一个窗口应用程序,现在我遇到了一个问题。我调用一个带参数的函数,根据参数,标签中应该显示不同的图像。但相反,它只是一个灰屏,按钮上也没有图像,但按钮正常执行其功能。在一切正常之前,我不知道该怎么做。提前致谢
from tkinter import *
def eclick(event):
root.destroy()
def edscr():
e_sc_1 = PhotoImage(file='e_scr_1.png')
e_scr_1 = Label(root, image=e_sc_1)
e_scr_1.place(x=0, y=0)
e_but = PhotoImage(file='but_e.png')
but_e = Button(root, width=477, height=16, image=e_but)
but_e.place(x=130, y=350)
but_e.bind('<Button-1>', eclick)
root = Tk()
edscr()
root.mainloop()
正如@jfs评论 的“应该保留图像的链接”,所以在这个答案中,整个对象都
Tk放在class示例代码中,而图像本身放在一个变量中,self.image即 您将图像分配给类块:为了使用其他类型的扩展,请使用
PIL示例库:您也可以将图像放在函数中的全局变量中,但最好通过
class