我怎样才能让 udate_idletasks 在 Mac 上工作
How can I make udate_idletasks work on Mac
我写了一个带动画的 tkinter 脚本,在 Xubuntu 上运行良好,但是当我 运行 在 Mac 上运行时,动画不起作用。这是一个演示问题的小脚本:
import tkinter as tk
from time import sleep
root = tk.Tk()
canvas = tk.Canvas(root, height=200, width = 200)
canvas.pack()
this = canvas.create_rectangle(25,25, 75, 75, fill='blue')
that = canvas.create_rectangle(125, 125, 175, 175, fill = 'red')
def blink(event):
global this, that
for _ in range(9):
canvas.itemconfigure(this, fill='red')
canvas.itemconfigure(that, fill = 'blue')
canvas.update_idletasks()
sleep(.4)
this, that = that, this
canvas.bind('<ButtonRelease-1>', blink)
root.mainloop()
这会在 canvas 上绘制一个红色方块和一个蓝色方块。当用户单击 canvas 时,方块重复切换颜色。在 Xubuntu 上,它按预期工作。
在 Mac 上,当我单击 canvas 时,我得到旋转的沙滩球,几秒钟后,我们看到方块已经切换颜色,因为它们切换颜色的次数为奇数代码中的次数。
在我看来 update_idletasks 没有用。有什么办法可以解决这个问题吗?我在大苏尔 运行宁 python 3.9.5 和 Tk 8.6。
我发现使用 update
而不是 update_idletasks
在两个平台上都适用。不过,据我了解,后者更为可取。例如,请参阅 的已接受答案。这解决了我眼前的问题,但有谁知道 update_idletasks
是否曾在 Mac 上工作过?
我认为您可以做的是避免执行会阻塞 mainloop
的任务,在本例中为 time.sleep()
。因此,您的代码可以通过使用 after
模拟 for
循环来重新制作,我看不出有什么可以阻止来自 运行 OS 独立的通用代码:
count = 0 # Think of this as the `_` in for _ in range(9)
def blink(event=None):
global this, that, count
if count < 9: # Basically repeats everytime `count` is less than 9, like a for loop
canvas.itemconfigure(this, fill='red')
canvas.itemconfigure(that, fill='blue')
this, that = that, this
count += 1 # Increase count
root.after(400,blink) # Repeat this code block every 400 ms or 0.4 seconds
else:
count = 0 # After it is 9, set it to 0 for the next click to be processed
我写了一个带动画的 tkinter 脚本,在 Xubuntu 上运行良好,但是当我 运行 在 Mac 上运行时,动画不起作用。这是一个演示问题的小脚本:
import tkinter as tk
from time import sleep
root = tk.Tk()
canvas = tk.Canvas(root, height=200, width = 200)
canvas.pack()
this = canvas.create_rectangle(25,25, 75, 75, fill='blue')
that = canvas.create_rectangle(125, 125, 175, 175, fill = 'red')
def blink(event):
global this, that
for _ in range(9):
canvas.itemconfigure(this, fill='red')
canvas.itemconfigure(that, fill = 'blue')
canvas.update_idletasks()
sleep(.4)
this, that = that, this
canvas.bind('<ButtonRelease-1>', blink)
root.mainloop()
这会在 canvas 上绘制一个红色方块和一个蓝色方块。当用户单击 canvas 时,方块重复切换颜色。在 Xubuntu 上,它按预期工作。
在 Mac 上,当我单击 canvas 时,我得到旋转的沙滩球,几秒钟后,我们看到方块已经切换颜色,因为它们切换颜色的次数为奇数代码中的次数。
在我看来 update_idletasks 没有用。有什么办法可以解决这个问题吗?我在大苏尔 运行宁 python 3.9.5 和 Tk 8.6。
我发现使用 update
而不是 update_idletasks
在两个平台上都适用。不过,据我了解,后者更为可取。例如,请参阅 update_idletasks
是否曾在 Mac 上工作过?
我认为您可以做的是避免执行会阻塞 mainloop
的任务,在本例中为 time.sleep()
。因此,您的代码可以通过使用 after
模拟 for
循环来重新制作,我看不出有什么可以阻止来自 运行 OS 独立的通用代码:
count = 0 # Think of this as the `_` in for _ in range(9)
def blink(event=None):
global this, that, count
if count < 9: # Basically repeats everytime `count` is less than 9, like a for loop
canvas.itemconfigure(this, fill='red')
canvas.itemconfigure(that, fill='blue')
this, that = that, this
count += 1 # Increase count
root.after(400,blink) # Repeat this code block every 400 ms or 0.4 seconds
else:
count = 0 # After it is 9, set it to 0 for the next click to be processed