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