2011-04-30 6 views
2

Tkinter 버튼이 두 개의 함수를 호출하도록 만들 수 있습니까?tkinter는 두 함수를 호출합니다.

from Tkinter import * 

admin = Tk() 
def o(): 
    print '1' 

def t(): 
    print '2' 
button = Button(admin, text='Press', command=o, command=t) 
button.pack() 

답변

7

모두 호출하는 새로운 기능을 확인 어쩌면?이 같은 몇 가지 :

def sequence(*functions): 
    def func(*args, **kwargs): 
     return_value = None 
     for function in functions: 
      return_value = function(*args, **kwargs) 
     return return_value 
    return func 

: 또는

def o_and_t(): 
    o() 
    t() 
button = Button(admin, text='Press', command=o_and_t) 

을, 당신은이 재미 작은 기능을 사용할 수 있습니다 그러면 다음과 같이 사용할 수 있습니다 :

button = Button(admin, text='Press', command=sequence(o, t)) 
+0

와우 감사를 다시 정의 할 필요없이 다른 명령으로, 여러 기능을 실행할 수 있습니다. 나는 지금 어리 석다. –

+1

커맨드 시퀀스 기능을 정말 좋아합니다. 좋은! – Alan

1

당신이 시도하는 구문은 불행하게도 존재하지 않습니다. 당신이해야 할 일은 두 함수를 실행하는 래퍼 함수를 ​​만드는 것입니다. 게으른 해결책은 될 것 같은 뭔가 :

def multifunction(*args): 
    for function in args: 
     function(s) 

cb = lambda: multifunction(o, t) 
button = Button(admin, text='Press', command=cb) 
0

내가 틀렸다,하지만 여러 기능을 조작 할 수있는 버튼을 필요할 때마다, 내가 한 번

button = Button(admin, text = 'Press', command = o) 

버튼을 설정 한 다음 추가를 할 경우에 저를 수정 스크립트에 .configure()

button.configure(command = t) 

추가를 사용하여 다른 기능은,이

과 같을 것이다
from Tkinter import * 

    admin = Tk() 
    def o(): 
    print '1' 

    def t(): 
    print '2' 
    button = Button(admin, text='Press', command=o) 
    button.configure(command = t) 
    button.pack() 

이 잘 기능과 admin.destroy 또는 전역 변수를 사용하거나 아무것도에게

관련 문제