将gif图像放入tkinter wind

2024-06-01 22:09:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在一个新的tkinter窗口中插入一个gif图像,当一个按钮被点击时,我一直得到这个错误

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\idlelib\run.py", line 119, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\queue.py", line 172, in get
raise Empty
queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:/Users/Afro/Desktop/mff.py", line 8, in sex
canvas = tkinter.Label(wind,image = photo)
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2605, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2138, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist

这是密码。图像和位置确实存在。在

^{pr2}$

我做错什么了?在


Tags: inpyselfinittkinterliblocalline
1条回答
网友
1楼 · 发布于 2024-06-01 22:09:59

您正在尝试创建Tk窗口的两个实例。你做不到。如果您想要第二个窗口或弹出窗口,您应该使用Toplevel()小部件。在

而且,self在这个上下文中没有任何含义。使用小部件的image属性更好to keep a reference。在

import tkinter

ssw = tkinter.Tk()

def six():
    toplvl = tkinter.Toplevel() #created Toplevel widger
    photo = tkinter.PhotoImage(file = 'American-Crime-Story-1.gif')
    lbl = tkinter.Label(toplvl ,image = photo)
    lbl.image = photo #keeping a reference in this line
    lbl.grid(row=0, column=0)

def base():
    la = tkinter.Button(ssw,text = 'yes',command=six)
    la.grid(row=0, column=0) #specifying row and column values is much better

base()

ssw.mainloop()

相关问题 更多 >