2016-10-06 122 views
-1

我的一門課程對tkinter進行了通過引用,它超出了介紹課​​程的範圍。它激起了我的好奇心,我想更深入地挖掘它。Python - tkinter - 動態複選框

我有基本的東西 - 我可以讓複選框出現在我想要的地方,但我想讓它更加動態/ pythonic。

第1列 - 應始終顯示所有複選框。這按預期工作。 第2列 - 只顯示第一個複選框,如果點擊它,則會顯示附加的4個複選框。當單擊這4個複選框中的任何一個時,它應該在UI底部的文本框中顯示一條消息。

當我點擊第二欄目前焦點課程的複選框時,他們什麼都不做。此外,通常隱藏的ABC 702複選框似乎在選中核心課程複選框時隨機出現。我無法弄清楚爲什麼。

from tkinter import * 

    class Application(Frame): 
     def __init__(self, master): 
      Frame.__init__(self, master) 
      self.grid() 
      self.create_widgets() 
      # self.grid() 
      # self.create_widgets() 

     def create_widgets(self): 
      Label(self, text="How many of the core courses have you completed so far?" 
       ).grid(row=1, column=0, sticky=W) 

      self.checkBoxA = BooleanVar() 
      Checkbutton(self, 
         text="ABC 501", 
         variable=self.checkBoxA, 
         command=self.update_text 
         ).grid(row=2, column=0, sticky=W) 

      self.checkBoxB = BooleanVar() 
      Checkbutton(self, 
         text="ABC 502", 
         variable=self.checkBoxB, 
         command=self.update_text 
         ).grid(row=3, column=0, sticky=W) 

      self.checkBoxC = BooleanVar() 
      Checkbutton(self, 
         text="ABC 601", 
         variable=self.checkBoxC, 
         command=self.update_text 
         ).grid(row=4, column=0, sticky=W) 
      self.checkBoxD = BooleanVar() 
      Checkbutton(self, 
         text="ABC 602", 
         variable=self.checkBoxD, 
         command=self.update_text 
         ).grid(row=5, column=0, sticky=W) 
      self.checkBoxE = BooleanVar() 
      Checkbutton(self, 
         text="ABC 603", 
         variable=self.checkBoxE, 
         command=self.update_text 
         ).grid(row=6, column=0, sticky=W) 
      self.checkBoxF = BooleanVar() 
      Checkbutton(self, 
         text="ABC 701", 
         variable=self.checkBoxF, 
         command=self.update_text 
         ).grid(row=7, column=0, sticky=W) 

      self.results_txt = Text(self, width=80, height=10, wrap=WORD) 
      self.results_txt.grid(row=8, column=0, columnspan=3) 

      Label(self, text="Which focus are you enrolled in?" 
       ).grid(row=1, column=1, sticky=W) 

      self.checkBoxF1 = BooleanVar() 
      Checkbutton(self, 
         text="Focus #1", 
         variable=self.checkBoxF1, 
         command=self.update_text 
         ).grid(row=2, column=1, sticky=W) 

      # if self.checkBoxF1.get(): 
      #  self.checkBoxF1A = BooleanVar() 
      #  Checkbutton(self, 
      #     text="ABC 503", 
      #     variable=self.checkBoxF1A, 
      #     command=self.update_text 
      #    ).grid(row=3, column=1, sticky=W) 


     def update_text(self): 
      courses = "" 
      counter = 0 
      focusCounter = 0 

     ##The checkboxes for the focus courses do not display text at the bottom 
      ##The ABC 702 checkbox randomly appears when clicking core courses 
      ##Is this related to reusing self vs creating another attribute? 
      if self.checkBoxF1.get(): 
       self.checkBoxF1A = BooleanVar() 
       Checkbutton(self, 
          text="ABC 503", 
          variable=self.checkBoxF1A, 
          command=self.update_text 
          ).grid(row=3, column=1, sticky=W) 

       self.checkBoxF1B = BooleanVar() 
       Checkbutton(self, 
          text="ABC 504", 
          variable=self.checkBoxF1B, 
          command=self.update_text 
          ).grid(row=4, column=1, sticky=W) 

       self.checkBoxF1C = BooleanVar() 
       Checkbutton(self, 
          text="ABC 505", 
          variable=self.checkBoxF1C, 
          command=self.update_text 
          ).grid(row=5, column=1, sticky=W) 

      self.checkBoxF1D = BooleanVar() 
      Checkbutton(self, 
         text="ABC 702", 
         variable=self.checkBoxF1D, 
         command=self.update_text 
         ).grid(row=6, column=1, sticky=W) 

      if self.checkBoxA.get(): 
       courses += "Introductory class \n" 
       counter += 1 

      if self.checkBoxB.get(): 
       courses += "Next level class description \n" 
       counter += 1 

      if self.checkBoxC.get(): 
       courses += "Core class #3 \n" 
       counter += 1 

      if self.checkBoxD.get(): 
       courses += "Another core class \n" 
       counter += 1 

      if self.checkBoxE.get(): 
       courses += "A higher level class \n" 
       counter += 1 

      if self.checkBoxF.get(): 
       courses += "An advanced class \n" 
       counter += 1 

      if self.checkBoxF1A.get(): 
       specialty = "Focused class #1" 
       focusCounter += 1 

      if self.checkBoxF1B.get(): 
       specialty = "Focused class #2" 
       focusCounter += 1 

      if self.checkBoxF1C.get(): 
       specialty = "Focused class #3" 
       focusCounter += 1 

      if self.checkBoxF1D.get(): 
       specialty = "Focused class #4" 
       focusCounter += 1 

      self.results_txt.delete(0.0, END) 
      self.results_txt.insert(0.0, courses) 
      if focusCounter > 0: #this means the user selected at least 1 focus course 
       self.results_txt.insert(0.0, specialty) 
      if counter == 6: 
       congrats = ("\n Congratulations on completing all of your core courses! \n \n") 
       self.results_txt.insert(0.0, congrats) 
      # print(focusCounter) 

    root = Tk() 
    root.title("This is where the title goes") 
    app = Application(root) 
    root.mainloop() 

回答

0

如果先點擊複選框Focus #1話,就說明新的複選框和他們的工作預期

但是,如果您單擊其他複選框爲先,然後你會得到錯誤信息

AttributeError: 'Application' object has no attribute 'checkBoxF1A'

問題是因爲在update_text()中,即使它們還不存在,也可以使用checkBoxF1A,checkBoxF1B等。

if self.checkBoxF1A.get(): 
    specialty = "Focused class #1" 
    focusCounter += 1 

創建self.checkBoxF1A = BooleanVar()create_widgets() - 不是在if self.checkBoxF1.get(),你不會有問題。

,你甚至可以在create_widgets()

self.checkbutton_F1 = Checkbuttons(...) 

創建Checkbuttons沒有grid()if self.checkBoxF1A.get():使用self.checkbutton_F1.grid(...)顯示控件和(在else:self.checkbutton_F1.grid_forget()將其隱藏。