473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How I can use the command correctly in checkbutton?

17
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!
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from tkMessageBox import *
  3.  
  4. class BoatManager( Frame ):
  5.  
  6.    def __init__( self, cols, rows ):
  7.  
  8.       Frame.__init__( self )
  9.       self.cols = cols
  10.       self.rows = rows
  11.       self.master.title( "Demo" )
  12.  
  13.       # main frame fills entire container, expands if necessary
  14.       self.master.rowconfigure( 0, weight = 1 )
  15.       self.master.columnconfigure( 0, weight = 1 )
  16.       self.grid( sticky = W+E+N+S )
  17.  
  18. ##      self.buttonChecked = BooleanVar()
  19.  
  20.       tempbutton = Checkbutton()
  21.       self.Button_Name = [[tempbutton]*self.rows]*self.cols
  22.  
  23.       tempvar = BooleanVar()
  24.       self.buttonChecked = [[tempvar]*self.rows]*self.cols
  25.  
  26.       for boat_num in range(self.cols):
  27.          for boat_slot in range(self.rows):
  28.             if boat_slot == 0 and boat_num == 0:
  29.                self.Button_Name[boat_num][boat_slot] = Checkbutton(self, text = 'All Boats_Slots', padx=5, pady=5, relief = SUNKEN, width = 15,
  30.                                   variable = self.buttonChecked[boat_num][boat_slot], command = self.select_all_boats)
  31.             elif boat_slot == 0:
  32.                self.Button_Name[boat_num][boat_slot] = Checkbutton(self, text = 'Boat_' + str(boat_num), padx=5, pady=5, relief = SUNKEN, width = 15,
  33.                                   variable = self.buttonChecked[boat_num][boat_slot], command = self.select_full_column)
  34.             elif boat_num == 0:
  35.                self.Button_Name[boat_num][boat_slot] = Checkbutton(self, text = 'Slot_' + str(boat_slot), padx=5, pady=5, relief = SUNKEN, width = 15,
  36.                                   variable = self.buttonChecked[boat_num][boat_slot], command = self.select_full_row)
  37.             else:
  38.                self.Button_Name[boat_num][boat_slot] = Checkbutton(self, text = str(boat_num) + '_' + str(boat_slot), padx=5, pady=5, relief = SUNKEN,
  39.                                   variable = self.buttonChecked[boat_num][boat_slot], width = 15, command = self.select_current_slot)
  40.             self.Button_Name[boat_num][boat_slot].grid(row = boat_slot+1, rowspan = 1, column = boat_num + 2, columnspan = 1)
  41.  
  42.  
  43.    def select_all_boats(self):
  44.       for boat_num in xrange(self.cols):
  45.          for boat_slot in xrange(self.rows):
  46.             if boat_slot != 0 and boat_num != 0:
  47.                self.Button_Name[boat_num][boat_slot].toggle()
  48.  
  49. ##   def select_full_column(self, column_num):
  50. ##      for boat_slot in xrange(25):
  51. ##         if boat_slot != 0:
  52. ##            self.Button_Name[column_num][boat_slot].toggle()
  53.  
  54.    def select_full_column(self):
  55.       print "I am here"
  56.       pass
  57.  
  58. ##   def select_full_row(self, row_num):
  59. ##      for boat_num in xrange(5):
  60. ##         if boat_num != 0:
  61. ##            self.Button_Name[boat_num][row_num].toggle()
  62.  
  63.    def select_full_row(self):
  64.       pass
  65.  
  66.    def select_current_slot(self):
  67.       pass
  68.  
  69. ##if __name__ == "__main__":
  70. ##   BoatManager(5,25).mainloop()
  71.  
  72. BoatManager(5,25).mainloop()
  73.  
Jul 28 '10 #1

✓ answered by bvdet

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.
Expand|Select|Wrap|Line Numbers
  1.         self.Button_Name = [[Checkbutton() for i in range(self.rows)] for j in range(self.cols)]
  2.  
  3.         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.
Expand|Select|Wrap|Line Numbers
  1.         for boat_num in range(self.cols):
  2.             for boat_slot in range(self.rows):
  3.  
  4.                 def handler_col(i=boat_num):
  5.                     return self.select_full_column(i)
  6.                 def handler_row(i=boat_slot):
  7.                     return self.select_full_row(i)
Assign the appropriate handler function:
Expand|Select|Wrap|Line Numbers
  1.                 elif boat_slot == 0:
  2.                     self.Button_Name[boat_num][boat_slot] = Checkbutton(self,
  3.                                                                        text = 'Boat_' + str(boat_num),
  4.                                                                        padx=5, pady=5,
  5.                                                                        relief = SUNKEN,
  6.                                                                        width = 15,
  7.                                                                        variable = self.buttonChecked[boat_num][boat_slot],
  8.                                                                        command = handler_col)
  9.                 elif boat_num == 0:
  10.                     self.Button_Name[boat_num][boat_slot] = Checkbutton(self,
  11.                                                                        text = 'Slot_' + str(boat_slot),
  12.                                                                        padx=5, pady=5,
  13.                                                                        relief = SUNKEN,
  14.                                                                        width = 15,
  15.                                                                        variable = self.buttonChecked[boat_num][boat_slot],
  16.                                                                        command = handler_row)
The row and column toggle functions:
Expand|Select|Wrap|Line Numbers
  1.     def select_full_column(self, i):
  2.         for j in range(1, self.rows):
  3.             self.Button_Name[i][j].toggle()
  4.  
  5.     def select_full_row(self, i):
  6.         for j in range(1, self.cols):
  7.             self.Button_Name[j][i].toggle()

2 1888
bvdet
2,851 Expert Mod 2GB
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.
Expand|Select|Wrap|Line Numbers
  1.         self.Button_Name = [[Checkbutton() for i in range(self.rows)] for j in range(self.cols)]
  2.  
  3.         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.
Expand|Select|Wrap|Line Numbers
  1.         for boat_num in range(self.cols):
  2.             for boat_slot in range(self.rows):
  3.  
  4.                 def handler_col(i=boat_num):
  5.                     return self.select_full_column(i)
  6.                 def handler_row(i=boat_slot):
  7.                     return self.select_full_row(i)
Assign the appropriate handler function:
Expand|Select|Wrap|Line Numbers
  1.                 elif boat_slot == 0:
  2.                     self.Button_Name[boat_num][boat_slot] = Checkbutton(self,
  3.                                                                        text = 'Boat_' + str(boat_num),
  4.                                                                        padx=5, pady=5,
  5.                                                                        relief = SUNKEN,
  6.                                                                        width = 15,
  7.                                                                        variable = self.buttonChecked[boat_num][boat_slot],
  8.                                                                        command = handler_col)
  9.                 elif boat_num == 0:
  10.                     self.Button_Name[boat_num][boat_slot] = Checkbutton(self,
  11.                                                                        text = 'Slot_' + str(boat_slot),
  12.                                                                        padx=5, pady=5,
  13.                                                                        relief = SUNKEN,
  14.                                                                        width = 15,
  15.                                                                        variable = self.buttonChecked[boat_num][boat_slot],
  16.                                                                        command = handler_row)
The row and column toggle functions:
Expand|Select|Wrap|Line Numbers
  1.     def select_full_column(self, i):
  2.         for j in range(1, self.rows):
  3.             self.Button_Name[i][j].toggle()
  4.  
  5.     def select_full_row(self, i):
  6.         for j in range(1, self.cols):
  7.             self.Button_Name[j][i].toggle()
Jul 28 '10 #2
Haiyan
17
Your suggestions work great! I merged your changes in and it worked exactly what I want. Thanks a lot for your help!
Jul 28 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Askari | last post by:
Hi, How do for do a "select()" on a CheckButton in a menu (make with add_checkbutton(....) )? I can modify title, state, etc but not the "check state". :-( Askari
1
by: Elaine Jackson | last post by:
My CheckButton variables seem always to be true, whether the box is checked or not, which makes them considerably less useful. Following is a little script that illustrates the difficulty. Any help...
3
by: Tuvas | last post by:
I want to have a checkbutton that when it is pushed will do a function depending on if it was pushed before or not. Ei: b=checkbutton(command=check) b.grid(row=0,column=0) def check(): if...
1
by: Knepper, Michelle | last post by:
Hi out there, I'm a first-time user of the "Copy ... From..." command, and I'm trying to load a table from a text flat file. http://www.postgresql.org/docs/7.4/static/sql-copy.html I don't...
2
by: Muleskinner | last post by:
Hi group, One simple question: How to create a checkbutton in vb.net? In vb6.0 it was easily done by changing the style of the checkbox control, but how to do it in vb.net... Regards Thomas
3
by: Thomas Jansson | last post by:
Dear all I am writing a program with tkinter where I have to create a lot of checkbuttons. They should have the same format but should have different names. My intention is to run the functions...
16
by: Giovanni D'Ascola | last post by:
Hi. I noticed that <input type="reset"actually don't enable checkbutton which are checked by default after they have been disabled by javascript. It's a bug?
3
by: jayro | last post by:
Hi folks, I got a checkbutton with a large label which is being wrapped into a few lines. Due to the height of the label, the checkbutton aligns vertically ending up set on the center of its...
0
by: dudeja.rajat | last post by:
Hi, I've a checkbutton in my GUI application which I want to work as: 1. it should be un-ticked by default, 2. should display a label in Gui, by default, 3. when user ticks the check button...
0
by: Gabriel Genellina | last post by:
En Thu, 21 Aug 2008 05:07:45 -0300, <dudeja.rajat@gmail.comescribi�: Use the grid_forget method to hide the label. Based on your posted example: from Tkinter import * def onclick(): if...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.