Skip to main content
 首页 » 编程设计

python延迟函数调用

2025年05月04日10yyy_WW

<分区>

我想延迟一个函数调用。 (或者在我看来:python 以错误的顺序执行函数)。在下面的示例中,我可以编写两个函数 bf1 和 bf2 而不是 bf(arg),它确实按预期工作:只要按下按钮,就会调用该函数。但是如果我在函数中包含参数,函数调用只执行一次。返回函数本身不会改变行为。

能否请您看一下,并提示我我的逻辑或对 python 的理解哪里有问题。

from tkinter import * 
def bf(str): 
    print(str) 
    # return print(str)  #alternative(same behaviour) 
 
main=Tk(screenName='screen',baseName='base') 
button1=Button(master=main,text='b1',command=bf("b1")) 
button2=Button(master=main,text='b2',command=bf("b2")) # runs once (and runs here) 
button1.pack() 
button2.pack() 
mainloop() 
print('end') 

--google 和 stackoverflow 搜索只返回特定时间间隔的延迟函数调用之类的东西这不是我要搜索的内容。 :-(