python 你好,如何将两个延迟的函数绑定到一个Button上?

wkyowqbh  于 2023-03-16  发布在  Python
关注(0)|答案(1)|浏览(109)
from tkinter import *

def update():
    myEntry.config(bg="red")
def update2():
    myEntry.config(bg="blue")

root = Tk()
myEntry = Entry(root)
myEntry.pack()
myButton = Button(text="test", command=lambda: [myEntry.after(2000, update), myEntry.after(2000, update2)])
myButton.pack()

根主循环()
我试着将两个延迟的函数绑定到一个按钮上,但只有第二个起作用:(

xu3bshqb

xu3bshqb1#

创建一个函数,从这个函数调用延迟的函数,我假设你实际上希望第二个函数在第一个函数之后2秒发生,所以我调整了调用after的时间。

def run_test():
    myEntry.after(2000, update)
    myEntry.after(4000, update2)
...
myButton = Button(text="test", command=run_test)

虽然可以使用lambda,但它会使代码更难阅读和调试,因此我总是建议尽可能使用一个专用函数进行回调。

相关问题