问题描述
限时送ChatGPT账号..我正在尝试让 Tkinter 显示繁忙的光标.不幸的是,我一定遗漏了一些非常明显的东西.以下是一个非常简单的程序,可以重现我的问题:
I'm trying to make Tkinter show a busy cursor. Unfortunately, I must be missing something terribly obvious. Following is a very simple program that reproduces my problem:
from Tkinter import *
import time
def do_something():
print "starting"
window.config(cursor="wait")
time.sleep(5)
window.config(cursor="")
print "done"
return
root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Do Something", command=do_something)
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
root.mainloop()
我没有看到光标有任何变化
I'm not seeing any change in the cursor
推荐答案
让 do_something
像这样:
def do_something():
print "starting"
root.config(cursor="wait")
root.update()
time.sleep(5)
root.config(cursor="")
print "done"
基本上,我做了三件事:
Basically, I did three things:
用 root
替换 window
(因为 window
未定义,root
是窗口的句柄).
Replaced window
with root
(since window
isn't defined and root
is the window's handle).
在配置光标的行之后添加了 root.update()
.
Added root.update()
just after the line that configures the cursor.
删除了不必要的return
(这不会导致任何错误,但为什么会出现错误?).
Removed the unnecessary return
(this didn't cause any errors, but why have it?).
这篇关于Python:Tkinter -- 如何让光标显示忙碌状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论