是否有可能使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()
是否有可能使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()
使調用這兩個新的功能:
def o_and_t():
o()
t()
button = Button(admin, text='Press', command=o_and_t)
或者,你可以用這個有趣的小功能:
def sequence(*functions):
def func(*args, **kwargs):
return_value = None
for function in functions:
return_value = function(*args, **kwargs)
return return_value
return func
那麼你可以這樣使用它:
button = Button(admin, text='Press', command=sequence(o, t))
不幸的是您嘗試的語法不存在。你需要做的是製作一個運行你的兩個函數的包裝函數。懶惰的解決辦法是這樣的:
def multifunction(*args):
for function in args:
function(s)
cb = lambda: multifunction(o, t)
button = Button(admin, text='Press', command=cb)
糾正我,如果我錯了,但每當我需要一個按鈕來操作多個功能,我將建立按鈕一次
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
不使用全局變量或不必重新定義什麼
哇感謝任何其他命令。我現在感到很蠢。 – 2011-04-30 04:38:19
我真的很喜歡那個命令序列功能。太好了! – Alan 2011-04-30 06:19:24