Python Tkinter,如何同步多个Flash小部件?
这是我在这里的第一个问题 我创建了多个自定义小部件(基于画布),它们具有相同的闪烁方法。 当我单击它们时(使用bind()),它们应该闪烁(使用after()回调),直到我再次单击它们为止 这是可行的,但他们的闪烁是异步的,因为它开始时,我点击一个小部件 我想同步他们的闪烁,我不知道该用什么 我考虑过一个主闪烁函数,它会向所有小部件发送一个事件,但这意味着每个小部件都有一个双重绑定,我不确定这是否可行 下面是一个简化的示例:Python Tkinter,如何同步多个Flash小部件?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,这是我在这里的第一个问题 我创建了多个自定义小部件(基于画布),它们具有相同的闪烁方法。 当我单击它们时(使用bind()),它们应该闪烁(使用after()回调),直到我再次单击它们为止 这是可行的,但他们的闪烁是异步的,因为它开始时,我点击一个小部件 我想同步他们的闪烁,我不知道该用什么 我考虑过一个主闪烁函数,它会向所有小部件发送一个事件,但这意味着每个小部件都有一个双重绑定,我不确定这是否可行 下面是一个简化的示例: import tkinter as tk def flash(bu
import tkinter as tk
def flash(button):
current_color = button.cget("background")
next_color = "green" if current_color == "red" else "red"
button.config(background=next_color)
root.after(1000, flash, button)
root = tk.Tk()
Button1 = tk.Button(root, text="Button1", background="green")
Button2 = tk.Button(root, text="Button2", background="green")
Button1.pack()
Button2.pack()
Button1.bind("<ButtonPress-1>", lambda event: flash(Button1))
Button2.bind("<ButtonPress-1>", lambda event: flash(Button2))
root.mainloop()
将tkinter作为tk导入
def闪光灯(按钮):
当前颜色=按钮.cget(“背景”)
下一个颜色=绿色,如果当前颜色=红色,否则为红色
button.config(背景=下一个颜色)
根。后(1000,闪烁,按钮)
root=tk.tk()
Button1=tk.按钮(根,text=“Button1”,background=“green”)
Button2=tk.Button(根,text=“Button2”,background=“绿色”)
按钮1.pack()
按钮2.pack()
Button1.bind(“,lambda事件:flash(Button1))
Button2.bind(“,lambda事件:flash(Button2))
root.mainloop()
你可以试着做这样的事情:
import tkinter as tk
import time
class Blinker:
def flash(self, button):
current_color = button.cget("background")
next_color = "green" if current_color == "red" else "red"
button.config(background=next_color)
def ch(self, i):
self.b[i] = not self.b[i]
def __init__(self):
self.root = tk.Tk()
self.b = [False, False]
Button1 = tk.Button(self.root, text="Button1", background="green")
Button2 = tk.Button(self.root, text="Button2", background="green")
Button1.pack()
Button2.pack()
Button1.bind("<ButtonPress-1>", lambda event: self.ch(0))
Button2.bind("<ButtonPress-1>", lambda event: self.ch(1))
while 1:
if self.b[0]:
self.flash(Button1)
if self.b[1]:
self.flash(Button2)
t = time.time()
while time.time()-t<1:
self.root.update()
Blinker()
将tkinter作为tk导入
导入时间
类闪烁器:
def闪存(自身,按钮):
当前颜色=按钮.cget(“背景”)
下一个颜色=绿色,如果当前颜色=红色,否则为红色
button.config(背景=下一个颜色)
def ch(自我,i):
self.b[i]=非self.b[i]
定义初始化(自):
self.root=tk.tk()
self.b=[假,假]
Button1=tk.Button(self.root,text=“Button1”,background=“green”)
Button2=tk.Button(self.root,text=“Button2”,background=“绿色”)
按钮1.pack()
按钮2.pack()
Button1.bind(“,lambda事件:self.ch(0))
Button2.bind(“,lambda事件:self.ch(1))
而1:
如果self.b[0]:
自动闪光(按钮1)
如果self.b[1]:
自动闪光(按钮2)
t=time.time()
while time.time()-t您可以将闪烁按钮存储在集合中(此处为集合,作为跟踪当前闪烁按钮的闭包),并使用此闭包同步闪烁。我添加了回调清除,以避免有许多重叠的闪烁命令
(我还使用了前景
I/o背景
颜色使其在我的系统上工作。)
始终运行flash()
的一个实例:它切换列表中每个按钮的颜色-最初为空。单个按钮的绑定只是从列表中添加或删除按钮。
import tkinter as tk
def flash(button, buttons=set(), _after=[]):
if button not in buttons:
buttons.add(button)
current_color = button.cget("foreground")
next_color = "blue" if current_color == "red" else "red"
for button in buttons:
button.config(foreground=next_color)
for after_id in _after:
root.after_cancel(after_id)
if buttons:
_after.append(root.after(1000, flash, button))
if __name__ == '__main__':
root = tk.Tk()
Button1 = tk.Button(root, text="Button1", foreground="blue")
Button2 = tk.Button(root, text="Button2", foreground="blue")
Button1.pack()
Button2.pack()
Button1.bind("<ButtonPress-1>", lambda event: flash(Button1))
Button2.bind("<ButtonPress-1>", lambda event: flash(Button2))
root.mainloop()
def flash(button, callback=False, buttons=set(), _after=[]):
if callback:
pass
elif button not in buttons:
buttons.add(button)
else:
buttons.remove(button)
current_color = button.cget("foreground")
next_color = "blue" if current_color == "red" else "red"
for button in buttons:
button.config(foreground=next_color)
for after_id in _after:
root.after_cancel(after_id)
if buttons:
_after.append(root.after(1000, flash, button, True))