Python _tkinter.TclError:命令名“无效”;。9284912“;
我想在b类中使用create_映像,但它不起作用,当我关闭python 3程序时,它显示了一个错误:_tkinter.TclError:无效的命令名“.51752240”Python _tkinter.TclError:命令名“无效”;。9284912“;,python,tkinter,Python,Tkinter,我想在b类中使用create_映像,但它不起作用,当我关闭python 3程序时,它显示了一个错误:_tkinter.TclError:无效的命令名“.51752240” 感谢任何人谁可以回答和帮助这一点 最初运行此程序时,仅执行b.\uuuu init\uuuu()的第一行;该函数包含一个无限循环,形式为self.root.mainloop()。关闭窗口后,主循环最终退出,执行继续进行G.draw()——这将失败,因为您尝试绘制的画布不再存在。您需要将mainloop()调用移动到代码中的稍后
感谢任何人谁可以回答和帮助这一点 最初运行此程序时,仅执行
b.\uuuu init\uuuu()
的第一行;该函数包含一个无限循环,形式为self.root.mainloop()
。关闭窗口后,主循环最终退出,执行继续进行G.draw()
——这将失败,因为您尝试绘制的画布不再存在。您需要将mainloop()调用移动到代码中的稍后一点-可能是b.\uuu init\uuu()
,或者是文件末尾的顶层。我认为PhotoImage
不支持JPG,是吗?另外,self.root
未定义。要么在任何地方更改为root
,要么将root=tkinter.Tk()
更改为self.root=tkinter.Tk()
非常感谢,我再试了一次,它不支持JPG。也许我在某个地方更改了它的扩展名。但我认为带有root的代码被定义为self.root。
import tkinter
class GUI:
root=tkinter.Tk()
def __init__(self):
self.canvas = tkinter.Canvas(self.root, width=1024, height=960, bg="White")
self.canvas.pack()
self.t=tkinter.PhotoImage(file='hj.jpg')
# self.canvas.create_image(100,100,image=self.t)
self.root.mainloop()
def draw(self,x,y):
self.canvas.create_image(x,y,image=self.t)
class b:
def __init__(self):
G=GUI()
G.draw(100,200)
b()