在按下 Tkinter 按鈕后,我利用 lambda 函式呼叫了兩個不同的函式(read_input_data(),它從 GUI 讀取資料 a 和 b,并運行,它獲取資料 a 和 b 并進行一些計算)
start_button = tk.Button(text = 'Start', command = lambda:[ [a,b] = read_input_data(), run(a,b)], bg = 'firebrick', fg = 'white', font = ('helvetica', 9, 'bold'))
但是,它回傳語法錯誤。如果我從 read_input_data() 中洗掉輸出 [a,b],則語法將正確,但我的代碼將無法作業,因為我需要 a 和 b 來執行第二個函式 run(a,b)。
uj5u.com熱心網友回復:
Lambda 只是一種簡寫符號,在功能上與使用 定義的普通函式沒有太大區別def,除了只允許單個運算式
x = lambda a, b: a b
真的相當于
def x(a, b):
return a b
同樣,您嘗試做的事情與做的事情沒有什么不同:
def x():
a, b = read_input_data()
run(a, b)
start_button = tk.Button(text = 'Start', command = x, bg = 'firebrick', fg = 'white', font = ('helvetica', 9, 'bold'))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/437487.html
標籤:Python python-3.x tkinter tkinter 按钮
