2013-01-13 45 views
1

我想通過一個參數傳遞給一個按鈕,單擊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命令,但這也行不通。

回答

3

你的問題的主要部分似乎是:

的問題是,我似乎無法在我 正在嘗試的方式引用self.ABlob - 返回式無

當你做x=ClassA(...).func(...)時,x包含調用func的結果。因此,當你做self.ABlob = ttk.Button(...).grid(...)時,self.ABlob中存儲的內容是None,因爲這是網格函數返回的內容。

如果你想存儲到按鈕的引用,您需要創建按鈕,然後調用網格作爲兩個獨立的步驟:

self.ABlob = ttk.Button(...) 
self.ABlob.grid(...) 

個人而言,我認爲這是一個最好的做法,尤其是當你」重新使用網格。通過將所有網格語句放在一個塊中,可以更容易地查看佈局和現貨缺陷:

self.ABlob.grid(row=3, column=2) 
self.BBlob.grid(row=3, column=3) 
self.CBlob.grid(row=3, column=4) 
+1

我認爲你是對的,我正在吠叫錯誤的樹。刪除了我的答案。 – tacaswell

+0

啊!好。謝謝,我會看看這種方法,謝謝。 –

+0

工作就像一個魅力,我學到了一些關於'嵌套'命令。欣賞它。感謝您的時間。 –