解決Tkinter中button按鈕未按卻主動執(zhí)行command函數(shù)的問題
在使用Tkinter做界面時,遇到這樣一個問題:
程序剛運(yùn)行,尚未按下按鈕,但按鈕的響應(yīng)函數(shù)卻已經(jīng)運(yùn)行了
例如下面的程序:
from Tkinter import * class App: def __init__(self,master): frame = Frame(master) frame.pack() Button(frame,text='1', command = self.click_button(1)).grid(row=0,column=0) Button(frame,text='2', command = self.click_button(2)).grid(row=0,column=1) Button(frame,text='3', command = self.click_button(1)).grid(row=0,column=2) Button(frame,text='4', command = self.click_button(2)).grid(row=1,column=0) Button(frame,text='5', command = self.click_button(1)).grid(row=1,column=1) Button(frame,text='6', command = self.click_button(2)).grid(row=1,column=2) def click_button(self,n): print 'you clicked :',n root=Tk() app=App(root) root.mainloop()
程序剛一運(yùn)行,就出現(xiàn)下面情況:

六個按鈕都沒有按下,但是command函數(shù)卻已經(jīng)運(yùn)行了
后來通過網(wǎng)上查找,發(fā)現(xiàn)問題原因是command函數(shù)帶有參數(shù)造成的
tkinter要求由按鈕(或者其它的插件)觸發(fā)的控制器函數(shù)不能含有參數(shù)
若要給函數(shù)傳遞參數(shù),需要在函數(shù)前添加lambda。
原程序可改為:
from Tkinter import * class App: def __init__(self,master): frame = Frame(master) frame.pack() Button(frame,text='1', command = lambda: self.click_button(1)).grid(row=0,column=0) Button(frame,text='2', command = lambda: self.click_button(2)).grid(row=0,column=1) Button(frame,text='3', command = lambda: self.click_button(1)).grid(row=0,column=2) Button(frame,text='4', command = lambda: self.click_button(2)).grid(row=1,column=0) Button(frame,text='5', command = lambda: self.click_button(1)).grid(row=1,column=1) Button(frame,text='6', command = lambda: self.click_button(2)).grid(row=1,column=2) def click_button(self,n): print 'you clicked :',n root=Tk() app=App(root) root.mainloop()
補(bǔ)充:Tkinter Button按鈕組件調(diào)用一個傳入?yún)?shù)的函數(shù)
這里我們要使用python的lambda函數(shù),lambda是創(chuàng)建一個匿名函數(shù),冒號前是傳入?yún)?shù),后面是一個處理傳入?yún)?shù)的單行表達(dá)式。
調(diào)用lambda函數(shù)返回表達(dá)式的結(jié)果。
首先讓我們創(chuàng)建一個函數(shù)fun(x):
def fun(x):
print x
隨后讓我們創(chuàng)建一個Button:(這里省略了調(diào)用Tkinter的一系列代碼,只寫重要部分)
Button(root, text='Button', command=lambda :fun(x))
下面讓我們創(chuàng)建一個變量x=1:
x = 1
最后點(diǎn)擊這個Button,就會打印出 1了。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)調(diào)用攝像頭并拍照發(fā)郵箱
這篇文章主要介紹了python實(shí)現(xiàn)調(diào)用攝像頭并拍照發(fā)郵箱的程序,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04
python opencv人臉識別考勤系統(tǒng)的完整源碼
這篇文章主要介紹了python opencv人臉識別考勤系統(tǒng)的完整源碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
一篇文章帶你深入學(xué)習(xí)Python函數(shù)
這篇文章主要帶大家深入學(xué)習(xí)Python函數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01
使用用Pyspark和GraphX實(shí)現(xiàn)解析復(fù)雜網(wǎng)絡(luò)數(shù)據(jù)
GraphX是Spark提供的圖計(jì)算API,它提供了一套強(qiáng)大的工具,這篇文章將詳細(xì)為大家介紹如何在Python?/?pyspark環(huán)境中使用graphx進(jìn)行圖計(jì)算,感興趣的可以了解下2024-01-01
詳解Appium+Python之生成html測試報(bào)告
這篇文章主要介紹了詳解Appium+Python之生成html測試報(bào)告,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
python+OpenCV實(shí)現(xiàn)圖像拼接
這篇文章主要為大家詳細(xì)介紹了python+OpenCV實(shí)現(xiàn)圖像拼接,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03

