英文:
How to change the cursor during a long-running task in tkinter
问题
我的应用程序包含一些由按钮点击触发的长时间运行的任务。我希望在点击按钮时将光标设置为'等待',并在任务完成后将其重置。这似乎相当简单,但我无法使其工作。
一个示例(不是实际应用程序)来说明我的问题:
import tkinter as tk
import tkinter.ttk as ttk
import time
class App(ttk.Frame):
def __init__(self, master):
super().__init__(master)
self.button = ttk.Button(master=self.master, text='Run task', command=self.onclick_button)
self.button.grid(padx=25, pady=25)
def set_cursor_busy(self):
self.config(cursor='wait')
def reset_cursor(self):
self.config(cursor='')
def onclick_button(self):
self.set_cursor_busy()
time.sleep(5) # 模拟长时间运行的任务。
self.reset_cursor()
root = tk.Tk()
app = App(master=root)
app.mainloop()
根据我从互联网搜索和阅读众多帖子中了解到的,光标在执行返回到主循环之前不会被更新。但是到那时,任务已经完成,并且光标已经被重置为正常状态,因此看不到光标更改。我看到的选项有:
update_idletasks()
update()
after()
- 线程
- 您自己的主循环
除了第5种,我尝试实现了所有选项,但没有任何一个可以正常工作。
什么是让我的代码正常工作的最简单方法?
编辑:此帖中提到的解决方案实际上是上面提到的选项2,但在self.config(.)
行之后添加self.update()
或self.master.update()
并未解决我的问题。对于添加self.master.update_idletasks()
(选项1)也是一样的情况。
关于我的设置的其他信息:Windows 10 Enterprise上的Python 3.7.3
编辑 2:所以为了绝对明确,以下任何一种方式都不起作用:
def set_cursor_busy(self):
self.master.config(cursor='wait')
self.master.update_idletasks()
# 或:self.update_idletasks()
# 或:self.update()
# 或:self.master.update()
def reset_cursor(self):
self.master.config(cursor='')
self.master.update_idletasks()
# 或:self.update_idletasks()
# 或:self.update()
# 或:self.master.update()
编辑 3:也不起作用:
def set_cursor_busy(self):
self.master.config(cursor='watch') # 使用'watch'而不是'wait'
self.master.update_idletasks()
编辑 4:使用after()
延迟长时间运行的任务(如此帖建议的)也无济于事:
def set_cursor_busy(self):
self.master.config(cursor='watch')
def reset_cursor(self):
self.master.config(cursor='')
def onclick_button(self):
self.set_cursor_busy()
self.after(500, lambda: time.sleep(5))
# 模拟长时间运行的任务,使用time.sleep(5)
self.reset_cursor()
编辑 5:尽管它不能解决问题,但状态栏至少可以作为许多情况下的替代方法,或者甚至可能更受欢迎,因为它可以在长时间运行的任务期间更新。与光标(在Windows上的TKInter中)不同,状态栏可以在长时间运行的任务期间更新。以下是在Windows上工作的示例:
class App(ttk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.button = ttk.Button(master=self.master, text='Run task', command=self.onclick_button)
self.button.grid(padx=25, pady=25)
self.statusbar = ttk.Label(master=self.master, text='', background='white')
self.statusbar.grid(row=1000, padx=1, sticky=tk.EW)
# 使用较高的行号强制状态栏成为网格中的最后一个项目。
def onclick_button(self):
self.update_statusbar()
time.sleep(3) # 一个长时间运行的任务。
self.update_statusbar()
def update_statusbar(self):
if not self.statusbar['text']:
self.statusbar['text'] = '忙碌中...'
else:
self.statusbar['text'] = ''
self.master.update()
英文:
My app contains some long-running tasks that are triggered by button clicks. I want to set the cursor to 'wait'
when the button is clicked and reset it when the task is finished. This seems to be fairly straightforward but I can't get it working.
A sample (not the actual app) to illustrate my problem:
import tkinter as tk
import tkinter.ttk as ttk
import time
class App(ttk.Frame):
def __init__(self, master):
super().__init__(master)
self.button = ttk.Button(master=self.master, text='Run task', command=self.onclick_button)
self.button.grid(padx=25, pady=25)
def set_cursor_busy(self):
self.config(cursor='wait')
def reset_cursor(self):
self.config(cursor='')
def onclick_button(self):
self.set_cursor_busy()
time.sleep(5) # Simulate a long running task.
self.reset_cursor()
root = tk.Tk()
app = App(master=root)
app.mainloop()
From what I have learned from searching the internet and reading numerous posts, I understand that the cursor won't be updated until execution returns to the mainloop. But by that time the task has finished and the cursor has already been reset to normal, so no cursor change is visible. The options I have seen are, to use:
update_idletasks()
update()
after()
- threading
- your own mainloop
Apart from 5, I have tried to implement all options, but I can't get any of them working.
What is the simplest way to get my code working?
EDIT: The solution mentioned in this post is actually option 2 mentioned above, but adding self.update()
or self.master.update()
after the self.config(.)
lines does not resolve my issue. The same holds for adding self.master.update_idletasks()
(option 1).
Additional information about my setup: Python 3.7.3 on Windows 10 Enterprise
EDIT 2: So for absolute clarity, none of the following works:
def set_cursor_busy(self):
self.master.config(cursor='wait')
self.master.update_idletasks()
# or: self.update_idletasks()
# or: self.update()
# or: self.master.update()
def reset_cursor(self):
self.master.config(cursor='')
self.master.update_idletasks()
# or: self.update_idletasks()
# or: self.update()
# or: self.master.update()
EDIT 3: Neither does:
def set_cursor_busy(self):
self.master.config(cursor='watch') # 'watch' instead of 'wait'
self.master.update_idletasks()
EDIT 4: Delaying the long-running task using after()
(as suggested in this post) does not help either:
def set_cursor_busy(self):
self.master.config(cursor='watch')
def reset_cursor(self):
self.master.config(cursor='')
def onclick_button(self):
self.set_cursor_busy()
self.after(500, lambda: time.sleep(5))
# Simulate a long running task with time.sleep(5)
self.reset_cursor()
EDIT 5: Althoug it does not resolve the problem, a status bar could at least serve as an alternative in many cases, or may even be preferred because of the possibility to add a tailored status message. In contrast to the cursor (in TKInter on Windows), a status bar can be updated during a long-running task. Here's an example that works on Windows:
class App(ttk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.button = ttk.Button(master=self.master, text='Run task', command=self.onclick_button)
self.button.grid(padx=25, pady=25)
self.statusbar = ttk.Label(master=self.master, text='', background='white')
self.statusbar.grid(row=1000, padx=1, sticky=tk.EW)
# Use a high row number to force the statusbar to be the last item on the grid.
def onclick_button(self):
self.update_statusbar()
time.sleep(3) # A long-running task.
self.update_statusbar()
def update_statusbar(self):
if not self.statusbar['text']:
self.statusbar['text'] = 'Busy...'
else:
self.statusbar['text'] = ''
self.master.update()
答案1
得分: 1
问题: 如何在长时间运行的任务期间更改cursor=
?
参考资料
- TkCmd - cursors
- cursor not changing until after action despite update_idletasks()
- how-do-i-get-cursor-to-show-busy-status
注意: 使用.update(...
,update_idletasks(...
或.after(500, ...
来强制.mainloop()
执行时间切片没有任何区别。
- 工作中,使用
root.config(...
或self.master.config(...
root.config(cursor='watch') root.update_idletasks() ...
- 工作中,使用
self.button.config(...
self.button.config(cursor='watch') self.update_idletasks()
- 不工作:使用
self.config(...
似乎不能为Frame
对象设置cursor=
, 但不会显示错误。self.config(cursor='watch') self.update_idletasks()
使用延迟变体,使用.after(...
,与上述结果相同:
def long_running_task(self):
time.sleep(5) # 模拟长时间运行的任务。
self.button.config(cursor='no')
def onclick_button(self):
self.button.config(cursor='watch')
self.after(500, self.long_running_task)
在Linux上测试 - Python版本: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6
英文:
>Question: How to change the cursor=
during a long-running task
Reference
- TkCmd - cursors
- cursor not changing until after action despite update_idletasks()
- how-do-i-get-cursor-to-show-busy-status
>Note: It makes no differnce, using .update(...
, update_idletasks(...
or .after(500, ...
to force a timeslice to the .mainloop()
.
- Working, using
root.config(...
orself.master.config(...
root.config(cursor='watch') root.update_idletasks() ...
- Working, using
self.button.config(...
self.button.config(cursor='watch') self.update_idletasks()
- >NOT Working: using
self.config(...
It seems, man can't set acursor=
to aFrame
object, no error shown.self.config(cursor='watch') self.update_idletasks()
The delayed variant, using .after(...
, with the same results as above:
def long_running_task(self):
time.sleep(5) # Simulate a long running task.
self.button.config(cursor='no')
def onclick_button(self):
self.button.config(cursor='watch')
self.after(500, self.long_running_task)
Tested with Linux - Python: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论