How I can use the command correctly in checkbutton?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Haiyan
    New Member
    • Jul 2010
    • 17

    How I can use the command correctly in checkbutton?

    Dear experts:

    I am trying to write one slot matrix manager which consists of 4 columns and 24 rows (total 96 slots). So I put one checkbutton matrix with 5 columns and 25 rows slot.
    The check/uncheck of checkbutton[0][0] will toggle all 96 slots.
    The check/uncheck of checkbutton[1][0] will toggle all 24 slots in column 1.
    The check/uncheck of checkbutton[0][1] will toggle all 4 slots in row 1.
    The check/uncheck of checkbutton[1][1] will toggle only slot in row 1 column 1.
    and so on...

    I wrote the script but it doesn't work as I expected. Any check/uncheck in any checkbuttons will select/deselect all checkbuttons. Looks like I didn't use the command argument correctly in checkbutton. Could you please help me out?

    Following is my script and thanks a lot for your help!
    Code:
    from Tkinter import *
    from tkMessageBox import *
    
    class BoatManager( Frame ):
    
       def __init__( self, cols, rows ):
    
          Frame.__init__( self )
          self.cols = cols
          self.rows = rows
          self.master.title( "Demo" )
    
          # main frame fills entire container, expands if necessary
          self.master.rowconfigure( 0, weight = 1 )
          self.master.columnconfigure( 0, weight = 1 )
          self.grid( sticky = W+E+N+S )
    
    ##      self.buttonChecked = BooleanVar()
    
          tempbutton = Checkbutton()
          self.Button_Name = [[tempbutton]*self.rows]*self.cols
    
          tempvar = BooleanVar()
          self.buttonChecked = [[tempvar]*self.rows]*self.cols
    
          for boat_num in range(self.cols):
             for boat_slot in range(self.rows):
                if boat_slot == 0 and boat_num == 0:
                   self.Button_Name[boat_num][boat_slot] = Checkbutton(self, text = 'All Boats_Slots', padx=5, pady=5, relief = SUNKEN, width = 15,
                                      variable = self.buttonChecked[boat_num][boat_slot], command = self.select_all_boats)
                elif boat_slot == 0:
                   self.Button_Name[boat_num][boat_slot] = Checkbutton(self, text = 'Boat_' + str(boat_num), padx=5, pady=5, relief = SUNKEN, width = 15,
                                      variable = self.buttonChecked[boat_num][boat_slot], command = self.select_full_column)
                elif boat_num == 0:
                   self.Button_Name[boat_num][boat_slot] = Checkbutton(self, text = 'Slot_' + str(boat_slot), padx=5, pady=5, relief = SUNKEN, width = 15,
                                      variable = self.buttonChecked[boat_num][boat_slot], command = self.select_full_row)
                else:
                   self.Button_Name[boat_num][boat_slot] = Checkbutton(self, text = str(boat_num) + '_' + str(boat_slot), padx=5, pady=5, relief = SUNKEN,
                                      variable = self.buttonChecked[boat_num][boat_slot], width = 15, command = self.select_current_slot)
                self.Button_Name[boat_num][boat_slot].grid(row = boat_slot+1, rowspan = 1, column = boat_num + 2, columnspan = 1)
    
    
       def select_all_boats(self):
          for boat_num in xrange(self.cols):
             for boat_slot in xrange(self.rows):
                if boat_slot != 0 and boat_num != 0:
                   self.Button_Name[boat_num][boat_slot].toggle()
    
    ##   def select_full_column(self, column_num):
    ##      for boat_slot in xrange(25):
    ##         if boat_slot != 0:
    ##            self.Button_Name[column_num][boat_slot].toggle()
    
       def select_full_column(self):
          print "I am here"
          pass
    
    ##   def select_full_row(self, row_num):
    ##      for boat_num in xrange(5):
    ##         if boat_num != 0:
    ##            self.Button_Name[boat_num][row_num].toggle()
    
       def select_full_row(self):
          pass
    
       def select_current_slot(self):
          pass
    
    ##if __name__ == "__main__":
    ##   BoatManager(5,25).mainloop()
    
    BoatManager(5,25).mainloop()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You only have one variable for the button objects and one variable for all the buttons. You need a button for each position and a variable for each button.
    Code:
            self.Button_Name = [[Checkbutton() for i in range(self.rows)] for j in range(self.cols)]
    
            self.buttonChecked = [[BooleanVar() for i in range(self.rows)] for j in range(self.cols)]
    You must have a way to "freeze" the row and column numbers to know which column or row to toggle on or off.
    Code:
            for boat_num in range(self.cols):
                for boat_slot in range(self.rows):
                    
                    def handler_col(i=boat_num):
                        return self.select_full_column(i)
                    def handler_row(i=boat_slot):
                        return self.select_full_row(i)
    Assign the appropriate handler function:
    Code:
                    elif boat_slot == 0:
                        self.Button_Name[boat_num][boat_slot] = Checkbutton(self,
                                                                           text = 'Boat_' + str(boat_num),
                                                                           padx=5, pady=5,
                                                                           relief = SUNKEN,
                                                                           width = 15,
                                                                           variable = self.buttonChecked[boat_num][boat_slot],
                                                                           command = handler_col)
                    elif boat_num == 0:
                        self.Button_Name[boat_num][boat_slot] = Checkbutton(self,
                                                                           text = 'Slot_' + str(boat_slot),
                                                                           padx=5, pady=5,
                                                                           relief = SUNKEN,
                                                                           width = 15,
                                                                           variable = self.buttonChecked[boat_num][boat_slot],
                                                                           command = handler_row)
    The row and column toggle functions:
    Code:
        def select_full_column(self, i):
            for j in range(1, self.rows):
                self.Button_Name[i][j].toggle()
    
        def select_full_row(self, i):
            for j in range(1, self.cols):
                self.Button_Name[j][i].toggle()

    Comment

    • Haiyan
      New Member
      • Jul 2010
      • 17

      #3
      Your suggestions work great! I merged your changes in and it worked exactly what I want. Thanks a lot for your help!

      Comment

      Working...