2013-01-13 1 views
0

버튼을 클릭하여 매개 변수를 가져 오는 함수를 만들고 싶습니다. 예를 들어 :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을 클릭 한 후, "번호"이되어야하며, 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

관련 문제