2013-07-11 64 views
0

我特地用防空火炮把按鈕對象列表中的問題。
我不知道如果我想要實現是否真能實現,但在這裏它是..Python的Tkinter的,按鈕上市

工作樣本:

from tkinter import * 

class Main(Frame): 
    def __init__(self, master = None): 
     Frame.__init__(self, master) 
     self.pack(fill = 'both', expand = True) 

     # General purpose variables  ---------- 
     self.isButtonSelected = False 

     self.isButton_A_Selected = False # controll boolean 
     self.isButton_B_Selected = False 
     self.isButton_C_Selected = False 
     self.isButton_D_Selected = False 

     # Layers      ---------- 
     self.background = Frame(self) 

     # Buttons      ---------- 
     self.button_A = Button(self.background, text = "A", bg = 'white', command = self.selectButton_A) 
     self.button_B = Button(self.background, text = "B", bg = 'white', command = self.selectButton_B) 
     self.button_C = Button(self.background, text = "C", bg = 'white', command = self.selectButton_C) 
     self.button_D = Button(self.background, text = "D", bg = 'white', command = self.selectButton_D) 

     # Packs       ---------- 
     #layers 
     self.background.pack(fill = 'both', expand = True) 
     #buttons 
     btnPackPrefix = {'side' : 'left', 'fill' : 'both', 'expand' : 'True'} 
     self.button_A.pack(btnPackPrefix) 
     self.button_B.pack(btnPackPrefix) 
     self.button_C.pack(btnPackPrefix) 
     self.button_D.pack(btnPackPrefix) 

    def selectButton_A(self): 
     '''The idea here is similar to radiobuttons. 
      Click to select. Click same to deselect. 
      Click other to deselect old and select new. 
      Only one button can be selected at a time. 
       -repeated for all similar methods.''' 

     if self.isButtonSelected == False:   # click-select 
      self.button_A.config(bg = 'green') 
      self.isButtonSelected = True 
      self.isButton_A_Selected = True 
     else:          
      if self.isButton_A_Selected:    # click-deselect 
       self.resetButtons() 
       self.isButtonSelected = False 
      else:          # deselect other and select this 
       self.resetButtons() 
       self.button_A.config(bg = 'green') 
       self.isButtonSelected = True 
       self.isButton_A_Selected = True 

    def selectButton_B(self): 
     if self.isButtonSelected == False: 
      self.button_B.config(bg = 'green') 
      self.isButtonSelected = True 
      self.isButton_B_Selected = True 
     else: 
      if self.isButton_B_Selected: 
       self.resetButtons() 
       self.isButtonSelected = False 
      else: 
       self.resetButtons() 
       self.button_B.config(bg = 'green') 
       self.isButtonSelected = True 
       self.isButton_B_Selected = True 

    def selectButton_C(self): 
     if self.isButtonSelected == False: 
      self.button_C.config(bg = 'green') 
      self.isButtonSelected = True 
      self.isButton_C_Selected = True 
     else: 
      if self.isButton_C_Selected: 
       self.resetButtons() 
       self.isButtonSelected = False 
      else: 
       self.resetButtons() 
       self.button_C.config(bg = 'green') 
       self.isButtonSelected = True 
       self.isButton_C_Selected = True 

    def selectButton_D(self): 
     if self.isButtonSelected == False: 
      self.button_D.config(bg = 'green') 
      self.isButtonSelected = True 
      self.isButton_D_Selected = True 
     else: 
      if self.isButton_D_Selected: 
       self.resetButtons() 
       self.isButtonSelected = False 
      else: 
       self.resetButtons() 
       self.button_D.config(bg = 'green') 
       self.isButtonSelected = True 
       self.isButton_D_Selected = True 

    def resetButtons(self): 
     '''this pretty much resets all buttons and controll booleans 
      to their default state''' 
     #A 
     self.button_A.config(bg = 'white') # paint button white 
     self.isButton_A_Selected = False # controll boolean reset 
     #B 
     self.button_B.config(bg = 'white') 
     self.isButton_B_Selected = False 
     #C 
     self.button_C.config(bg = 'white') 
     self.isButton_C_Selected = False 
     #D 
     self.button_D.config(bg = 'white') 
     self.isButton_D_Selected = False 


def run_Application(): 
    app = Main() 
    app.master.geometry('200x50') 
    app.mainloop() 

run_Application() 

正如你注意到沒有,我的代碼有很多的文本─粘貼的細節在每一個細節都改變了。
所以我需要做它在某種程度上更短..我需要1個功能,因爲它們是相似的,用於控制所有這些行動

我想出這個。

不工作樣本:

from tkinter import * 

class Main(Frame): 
    def __init__(self, master = None): 
     Frame.__init__(self, master) 
     self.pack(fill = 'both', expand = True) 

     # General purpose variables  ---------- 
     self.isButtonSelected = False 

     self.isButton_A_Selected = False # controll boolean 
     self.isButton_B_Selected = False 
     self.isButton_C_Selected = False 
     self.isButton_D_Selected = False 

     # Layers      ---------- 
     self.background = Frame(self) 

     # Buttons      ---------- 
     self.button_A = Button(self.background, text = "A", bg = 'white', command = self.selectButton(0)) # apparently this doesn't work... 
     self.button_B = Button(self.background, text = "B", bg = 'white', command = self.selectButton(1)) 
     self.button_C = Button(self.background, text = "C", bg = 'white', command = self.selectButton(2)) 
     self.button_D = Button(self.background, text = "D", bg = 'white', command = self.selectButton(3)) 

     # Packs       ---------- 
     #layers 
     self.background.pack(fill = 'both', expand = True) 
     #buttons 
     btnPackPrefix = {'side' : 'left', 'fill' : 'both', 'expand' : 'True'} 
     self.button_A.pack(btnPackPrefix) 
     self.button_B.pack(btnPackPrefix) 
     self.button_C.pack(btnPackPrefix) 
     self.button_D.pack(btnPackPrefix) 

    def selectButton(self, i = None): 
     '''The idea here is similar to radiobuttons. 
      Click to select. Click same to deselect. 
      Click other to deselect old and select new. 
      Only one button can be selected at a time. 
       -do that only on objects 
       at [i]. 'i' is given from the button command''' 

     global buttons, btn_bools 

     buttons = [self.button_A, self.button_B, 
        self.button_C, self.button_D] # list buttons. # Raises error. It seems that Button objects cannot be listed 

     btn_bools = [self.isButton_A_Selected, self.isButton_B_Selected, 
        self.isButton_C_Selected, self.isButton_D_Selected] # list contoll booleans 

     if self.isButtonSelected == False:   # click-select 
      self.buttons[i].config(bg = 'green') 
      self.isButtonSelected = True 
      self.btn_bools[i] = True 
     else:          
      if self.isButton_A_Selected:    # click-deselect 
       self.resetButtons() 
       self.isButtonSelected = False 
      else:          # deselect other and select this 
       self.resetButtons() 
       self.buttons[i].config(bg = 'green') 
       self.isButtonSelected = True 
       self.btn_bools[i] = True 

    def resetButtons(self): 
     '''iterate through all buttons/controll booleans 
      and reset to their default state''' 

     for b in range(len(buttons)): 
      buttons[b].config(bg = 'white') 

     for bb in range(len(btn_bools)): 
      btn_bools[bb] = False 



def run_Application(): 
    app = Main() 
    app.master.geometry('200x50') 
    app.mainloop() 

run_Application() 

我試圖測試與包含一個Int屬性和
它完美地工作「簡單的」類類似的方法!

從錯誤報告,我得到上市的按鈕是不是一個好主意,如果不是不可能的。

我的搜索谷歌很多任何例子或類似的問題,但我沒有
得到任何好的結果。

我還搜索了Python中的指針實現,並且
感到困惑。我試圖看看是否可以保存一個指向按鈕的變量,但我認爲結果是一樣的。列表讀取它的對象作爲按鈕,並且
產生錯誤。

所以,我的問題是:
1.這可以以某種方式完成嗎? (顯著短路第一個代碼示例。)
2.關於上市按鈕或部件一般:如果是可以做到的,如何?如果不是,爲什麼?
3.在第二個代碼示例中,我需要使用我的按鈕命令來包含我的函數需要的'i'參數
。這是如何輸入的?似乎只是添加(i)不起作用。

在此先感謝!

回答

1

看這行代碼:

self.button_A = Button(..., command = self.selectButton(0)) 

這是怎麼回事?是什麼self.selectButton(0)辦?你是調用函數self.selectButton(0),然後你使用該函數的結果作爲command屬性的值。這幾乎肯定不是你想要的。

你需要做的,而不是什麼是沿着這線的東西:

self.button_A = Button(..., command = lambda: self.selectButton(0)) 

這推遲調用self.selectButton直到按鈕被實際點擊。現在,這將允許創建的按鈕,你可以,如果你喜歡它們分配到一個列表:

self.buttons = [self.button_A, self.button_B, self.button_C, self.button_D] 
+0

哦,我明白了..所以,問題是,Python中試圖_before_按鈕被定義來執行功能。我還沒有使用_lambdas_,所以我想是時候開始閱讀了!非常感謝!現在它工作得很好! –