如何在checkbutton中正确使用命令?

尊敬的专家们:
我正在尝试编写一个槽矩阵管理器,它由4列和24行(总共96个槽)组成。所以我放了一个5列25行槽的复选按钮矩阵。
选中/取消选中复选按钮[0][0]将切换所有96个插槽。
选中/取消选中复选按钮[1][0]将切换第1列中的所有24个插槽。
选中/取消选中复选按钮[0][1]将切换行1中的所有4个插槽。
选中/取消选中复选按钮[1][1]将仅切换第1行第1列中的插槽。
以此类推。
剧本是我写的,但并不像我预想的那样好。选中/取消选中任何复选按钮将选中/取消选中所有复选按钮。看起来我没有在CheckButton中正确使用命令参数。你能帮帮我吗?
以下是我的剧本,非常感谢你的帮助!

选择 | 换行 | 行号
  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.  
# 回答1


您只有一个用于按钮对象的变量和一个用于所有按钮的变量。每个位置需要一个按钮,每个按钮需要一个变量。

选择 | 换行 | 行号
  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)]

您必须有一种"冻结"行号和列号的方法,才能知道要打开或关闭哪一列或哪一行。

选择 | 换行 | 行号
  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)

分配适当的处理程序函数:

选择 | 换行 | 行号
  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)

行和列切换的功能如下:

选择 | 换行 | 行号
  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


你的建议很管用!我将您的更改合并到中,它的效果完全符合我的要求。非常感谢你的帮助!

标签: python

添加新评论