我有一個問題在tkinter環境(樹莓上的蟒蛇3.2)設置文件名。要指定我的意思,我會用我的代碼:Tkinter文件名與StringVar
from tkinter import Tk, Canvas, StringVar
from PIL import ImageTk, Image
from threading import Thread
class proc(Thread):
def __init__(self):
Thread.__init__(self)
def run(self):
self.root=tkinter.Tk()
self.labelstring = StringVar()
self.labelstring.set('Foo')
self.path = StringVar()
self.path.set('cold.jpg')
canvas = Canvas(self.root, width=888, height=600)
canvas.pack()
im = Image.open(self.path) #<-- does not work
canvas.image = ImageTk.PhotoImage(im)
canvas.create_image(0, 0, image=canvas.image, anchor='nw')
label = tkinter.Label(self.root,textvariable=self.labelstring)
label.pack()
self.root.mainloop()
app = proc()
app.start()
for i in range(0, 10):
time.sleep(5)
proc.labelstring.set(i)
,我改變labelstring.set(i)
工作正常,但什麼行不通通過path.set('image.jpg')
發送文件名標籤的一部分。我知道,文件類型不是這樣的路徑,它是一個tkinter.StringVar對象...我沒有找到一個好方法使它成爲一個路徑變量。
在一天
im = Image.open(self.path)
canvas.image = ImageTk.PhotoImage(im)
canvas.create_image(0, 0, image=canvas.image, anchor='nw')
的到底能不能調用先前定義self.path.set('image.jpg')
。我想也許有一個xy圖片列表,並做path.set(piclist[i])
更改tkinter.canvas中的圖像。
什麼是「不工作」是什麼意思,在「什麼是行不通的發送文件名」的句子。你有錯誤嗎?發送了錯誤的東西嗎?什麼都沒有發送? –