将参数传递给函数python

2024-03-28 09:22:54 发布

您现在位置:Python中文网/ 问答频道 /正文

from Tkinter import *

class Program:    
    def __init__(self):
        b = Button(text="click me", command=self.callback("1"))
        b.pack()

    def callback(self,s):
        print "clicked!"

program = Program()

mainloop()     

为什么单击按钮之前要执行函数??*/你知道吗


Tags: textfromimportselfinittkinterdefcallback
1条回答
网友
1楼 · 发布于 2024-03-28 09:22:54

应该将函数引用传递给command参数。否则,您将就地执行该函数。你知道吗

b = Button(text="click me", command=self.callback)
# Or if you want to pass parameters
b = Button(text="click me", command=lambda: self.callback("1"))

相关问题 更多 >