我正在嘗試每秒更新一次TKinter窗口。所以想法是應該打開一個窗口,讓Python更新字段,顯示更新的窗口。現在的情況是,第二個窗口只在我關閉第一個窗口時才顯示。我猜這與mainloop()有關。我看着.update()和.update_idletasks(),但我無法弄清楚如何實現它。 TKinter用於顯示一個有房屋的場地。所以一般是應該這樣做:如何更新python tkinter窗口
- 生成的房子位置(已實施),在它的房子(已實現)
- 生成房屋(已經實現)
- 展會的新位置
- 秀場更新字段
這是我當前的代碼。我不確定是否需要更新功能。
class Plot(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
def createWidgets(self, list_houses):
houses = list_houses
self.w = tk.Canvas(self, width = field.width*SIZE, height = field.height*SIZE, bg = '#E4E4E4')
...
self.w.grid()
def update(self):
?
#GENERATES FIELD AND HOUSES
...
#PRINT FIRST WINDOW
plot = Plot()
plot.createWidgets(houses) <- PUT HOUSES IN TK INTER
plot.master.title('map of houses')
plot.mainloop()
# UPDATE FIELD <- THIS PART IS ONLY EXECUTED WHEN I CLOSE THE FIRST WINDOW, WHY?
i = 0
while i < 2:
update = field.update_houses(houses) <- GENERATES NEW LOCATION OF HOUSES
#PRINT UPDATED WINDOW, IT SHOULD BE PRINTED IN THE SAME WINDOW!
plot = Plot()
plot.createWidgets(houses) <- PUT HOUSES IN TKINTER
plot.master.title('map of houses')
i += 1
在此先感謝!