如何更新python tkinter窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27123676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to update python tkinter window
提问by Ramon Geessink
I'm currently trying to update a TKinter window every second. So the idea is that is should open a window, let python updates the field, show updated window. The situation right now is that the second window is only showing when i close the first one. I'm guessing this has something to do with mainloop(). I looked into .update() and .update_idletasks() but I can't figure out how to implement it. TKinter is used to show a field with houses in it. So in general is should do this:
我目前正在尝试每秒更新一个 TKinter 窗口。所以这个想法是应该打开一个窗口,让python更新字段,显示更新的窗口。现在的情况是第二个窗口仅在我关闭第一个窗口时显示。我猜这与 mainloop() 有关。我查看了 .update() 和 .update_idletasks() 但我不知道如何实现它。TKinter 用于显示带有房屋的字段。所以一般来说应该这样做:
- Generate house location (already implemented)
- Show field with houses in it (already implemented)
- Generate new location of houses (already implemented)
- Show updated field
- 生成房屋位置(已实现)
- 显示带有房屋的字段(已实现)
- 生成房屋的新位置(已实施)
- 显示更新的字段
This is my current code. I'm not sure if the update function is necessary.
这是我目前的代码。我不确定是否需要更新功能。
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
Thanks in advance!
提前致谢!
回答by fhdrsdg
Your update field is only executed when you close the window because tkinter's mainloop keeps running until the window is closed. Because of this mainloop, using a (long) while loop in tkinter is a bad idea, because the while loop locks up the tkinter mainloop.
您的更新字段仅在您关闭窗口时执行,因为 tkinter 的主循环会一直运行直到窗口关闭。由于这个主循环,在 tkinter 中使用(长)while 循环是一个坏主意,因为 while 循环锁定了 tkinter 主循环。
To do something multiple times without blocking the mainloop, use the aftermethod. This calls a function after a certain amount of time. You can call it once before entering your mainloop and then call it again in your update function, so that the update function calls itself. Take a look at this small example which uses afterto execute an update function every second:
要在不阻塞主循环的情况下多次执行某事,请使用该after方法。这会在一定时间后调用一个函数。您可以在进入主循环之前调用一次,然后在更新函数中再次调用它,以便更新函数调用自身。看看这个after用来每秒执行一次更新函数的小例子:
import Tkinter as tk
import random
def update():
l.config(text=str(random.random()))
root.after(1000, update)
root = tk.Tk()
l = tk.Label(text='0')
l.pack()
root.after(1000, update)
root.mainloop()
回答by guest
you could use the .updateto the window. That would make it update the window and allow everything to be on it.
你可以使用.update到窗口。这将使其更新窗口并允许所有内容都在其上。

