如何在Tkinter中使用Button设置功能参数?

时间:2013-01-13 20:34:07

标签: python function button parameters tkinter

我想创建一个函数,它将从Button点击它的参数。 例如:

from Tkinter import *

def func(b):
    number = 2*b
    print number
    return

root=Tk()

# By clicking this button I want to set b = 1 and call func

b1 = Button(root,...)
b1.pack()

# By clicking this button I want to set b = 2 and call func

b2 = Button(root,...)
b2.pack()

root.mainloop()

所以点击b1后,“number”应为2,点击b2后,“number”应为4。

我希望我能很好地解释我的问题。

感谢您的回答

mountDoom

1 个答案:

答案 0 :(得分:4)

这是一种方式

from sys import stderr
from Tkinter import *

def func(b):
    number = 2*b
    stderr.write('number=%d\n'%number)
    return

root=Tk()

# By clicking this button I want to set b = 1 and call func

b1 = Button(root,command=lambda : func(1))
b1.pack()

# By clicking this button I want to set b = 2 and call func

b2 = Button(root,command=lambda : func(2))
b2.pack()

root.mainloop()