Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tkinter GUI更新问题(Python)_Python_User Interface_Tkinter - Fatal编程技术网

Tkinter GUI更新问题(Python)

Tkinter GUI更新问题(Python),python,user-interface,tkinter,Python,User Interface,Tkinter,我目前在Tkinter中的GUI有问题。我编造了一个例子来说明我的观点: import tkinter as tk import time bob = tk.Tk() bob.geometry("100x70") bob.resizable(0, 0) ## Initialises the window and makes it look pretty label = tk.Label(bob, text = "Test") label.pack() label.place(x=40,y=

我目前在Tkinter中的GUI有问题。我编造了一个例子来说明我的观点:

import tkinter as tk
import time

bob = tk.Tk()
bob.geometry("100x70")
bob.resizable(0, 0)
## Initialises the window and makes it look pretty


label = tk.Label(bob, text = "Test")
label.pack()
label.place(x=40,y=25)
## Initalises a label and makes it look pretty as well

def some_task():
    print("Hi")
    label.config(text = "Not a test anymore")
    time.sleep(5)
    print("Bye")
如果您运行此代码,您会注意到,即使在程序等待然后打印Bye之前更新了标签,GUI也是最后更新的。这对我来说是个问题,因为我的程序需要在屏幕上向用户显示一些正在发生的事情

有人知道这是为什么吗?有办法解决这个问题吗


提前谢谢

一般来说,要在tkinter中使用计时器,应该使用bob.after方法。例如,按如下方式更改代码:

import tkinter as tk
import time

bob = tk.Tk()
bob.geometry("200x70")
bob.resizable(0, 0)
## Initialises the window and makes it look pretty


label = tk.Label(bob, text = "Test")
label.pack()
label.place(x=40,y=25)
## Initalises a label and makes it look pretty as well

def some_task():
    print("Hi")
    label.config(text = "Not a test anymore")
#    time.sleep(5)          # <-- comment out this
    print("Bye")

bob.after(5000, some_task)  # <-- use after to call some_task() after 5s.

bob.mainloop()    
将导致以下操作: 1.显示标签包含文本的Tk窗口。
2.5秒过去,标签更新为不再是测试

是否为完整示例?好像少了什么东西。例如,您根本没有在此处调用某个任务。