我想通過一個參數傳遞給一個按鈕,單擊func並遇到問題。Python/ttk/tKinter - 用按鈕單擊func傳遞參數?
總之,我試圖讓按鈕按下來彈出askColor()
方法,並返回該顏色值作爲相關文本框的背景顏色。
它的功能是如此synaesthets可以將一個顏色與一個字母/數字關聯並記錄結果的顏色列表。
具體線路:
self.boxA = Text(self.mainframe, state='normal', width=3, height=1, wrap='word', background=self.AVal).grid(column=2, row=2, padx=4)
self.boxB = Text(self.mainframe, state='normal', width=3, height=1, wrap='word', background=self.AVal).grid(column=3, row=2, padx=4)
self.boxC = Text(self.mainframe, state='normal', width=3, height=1, wrap='word', background=self.AVal).grid(column=4, row=2, padx=4)
self.ABlob = ttk.Button(self.mainframe, text="A",style= 'mainSmall.TButton', command= lambda: self.getColour(self.boxA)).grid(column=2, row=3)
self.BBlob = ttk.Button(self.mainframe, text="B",style= 'mainSmall.TButton', command= lambda: self.getColour(self.boxB)).grid(column=3, row=3)
self.CBlob = ttk.Button(self.mainframe, text="C",style= 'mainSmall.TButton', command= lambda: self.getColour(self.boxC)).grid(column=4, row=3)
和:
def getColour(self,glyphRef):
(triple, hexstr) = askcolor()
if hexstr:
glyphRef.config(bg=hexstr)
的問題是,我似乎不能在我想的方式來引用self.ABlob
- 返回式None
。我試過在button click func中包含一個pack.forget
命令,但這也行不通。
我認爲你是對的,我正在吠叫錯誤的樹。刪除了我的答案。 – tacaswell
啊!好。謝謝,我會看看這種方法,謝謝。 –
工作就像一個魅力,我學到了一些關於'嵌套'命令。欣賞它。感謝您的時間。 –