【问题标题】:Passing Argument to function python将参数传递给函数python
【发布时间】:2015-01-23 03:09:49
【问题描述】:
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()     

为什么要在点击按钮前执行函数?? */

【问题讨论】:

  • 那不是有效的 Python。检查注释语法。
  • 我编辑代码现在可以回答我吗?
  • 请不要大喊大叫。
  • 为什么要在点击按钮前执行函数?
  • 你认为执行了什么函数?为什么你认为它应该被执行?你了解 Tkinter 的工作人员吗?

标签: python python-2.7 user-interface tkinter


【解决方案1】:

您应该将函数引用传递给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"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多