2013-01-13 29 views
0

我想創建一個函數,它將從Button單擊中獲取它的參數。 例如:如何用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後,「數字」應該是2,點擊B2之後,「數字」應該是4

我希望我解釋我的問題很好。

感謝答案

mountDoom

回答

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() 
+0

非常感謝您!它工作,因爲我想它的工作:) – user1967718

相關問題