Python tkinter画布更新ImageTk.PhotoImage()

Python tkinter画布更新ImageTk.PhotoImage(),python,tkinter,python-imaging-library,tkinter-canvas,Python,Tkinter,Python Imaging Library,Tkinter Canvas,这是我问题的补充,我也通读了一遍,但我不知道全局变量是否与我的问题完美匹配 我非常喜欢Marcin在我的旧线程中提出的解决方案,但是我确实包括了线程选项,因为我有时调用画布中需要很多时间(分钟…)的函数,并且我被告知当画布中的这些函数需要很多时间时,应该这样做 这是我到目前为止的代码: class MyTkApp(Thread): def __init__(self): Thread.__init__(self) def run(self): self.root=tkinter

这是我问题的补充,我也通读了一遍,但我不知道全局变量是否与我的问题完美匹配

我非常喜欢Marcin在我的旧线程中提出的解决方案,但是我确实包括了线程选项,因为我有时调用画布中需要很多时间(分钟…)的函数,并且我被告知当画布中的这些函数需要很多时间时,应该这样做

这是我到目前为止的代码:

class MyTkApp(Thread):
def __init__(self):
    Thread.__init__(self)

def run(self):
    self.root=tkinter.Tk()
    self.s = StringVar()
    self.s.set('Foo')

    self.path = StringVar()
    self.path.set('1.jpg')

    canvas = Canvas(self.root, width=300, height=300)
    canvas.pack()

    im = Image.open(self.path.get()) 
    canvas.image = ImageTk.PhotoImage(im)

    l = tkinter.Label(self.root,textvariable=self.s)
    l.pack()
    self.root.mainloop()


app = MyTkApp()
app.start()
当我运行它时,一切正常,直到我包括:

app.s.set('test') # <--- works
app.path.set('2.jpg') # <--- does nothing :(

app.s.set('test')#app.s.set('test')#我真的不明白你想告诉我什么。。。当我从Foo->test调用
app.s.set('test')
时,我的代码会更改标签。但是当我调用
app.path.set('2.jpg')
时,画布中的图片没有改变。这回答了你的问题吗,还是你的意思是什么?据我所知,你在编辑我的帖子时删除了
canvas.image=ImageTk.PhotoImage(im)
,即
canvas.create_image(0,0,image=canvas.image)
。我也试过你的代码,但没有成功。你能告诉我你为什么这么做,你想展示什么吗?