Dear experts:
I am trying to add one xy-scrollbar on the canvas, then put lots of check buttons on the canvas. I got at least following 2 problems:
1.I can see the y-scroll bar but contents on canvas didn't move together when scroll y-scrollbar.
2.The x-scrollbar didn't show up as expected.
Do you have any idea? Thank you very much in advance!
Best regards,
Haiyan
Following is my code and you can run it directly to show the 2 problems above:
I am trying to add one xy-scrollbar on the canvas, then put lots of check buttons on the canvas. I got at least following 2 problems:
1.I can see the y-scroll bar but contents on canvas didn't move together when scroll y-scrollbar.
2.The x-scrollbar didn't show up as expected.
Do you have any idea? Thank you very much in advance!
Best regards,
Haiyan
Following is my code and you can run it directly to show the 2 problems above:
Code:
from Tkinter import *
from tkMessageBox import *
class BoatManager( Frame ):
def __init__( self, name, cols, rows ):
Frame.__init__( self )
self.name = name
self.cols = cols
self.rows = rows
self.master.title( "Apprentice" )
# 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 )
# Scroll bar
xscrollbar = Scrollbar(self, orient=HORIZONTAL)
xscrollbar.grid(row=1, column=0, sticky=E+W)
yscrollbar = Scrollbar(self)
yscrollbar.grid(row=0, column=1, sticky=N+S)
# create Canvas component
self.canvas = Canvas(self, bd=0, scrollregion=(0, 0, 1000, 5000),
yscrollcommand=yscrollbar.set,
xscrollcommand=xscrollbar.set)
self.canvas.grid(row=0, column=0, sticky=N+S+E+W)
xscrollbar.config(command=self.canvas.xview)
yscrollbar.config(command=self.canvas.yview)
# make the canvas expandable
self.canvas.grid_rowconfigure(0, weight=1)
self.canvas.grid_columnconfigure(0, weight=1)
# Label for name
self.Label1 = Label( self.canvas, width = 40, height = 2, text = self.name, font = "Arial 20" )
self.Label1.grid( row = 0, rowspan = 1, column = 1, columnspan =self.cols+1, sticky = W+E+N+S )
# Create one button object and one variable for all the buttons
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)]
self.buttonNameVar = [[StringVar() for i in range(self.rows)] for j in range(self.cols)]
for boat_num in xrange(self.cols):
for boat_slot in xrange(self.rows):
# Freeze the column number to toggle whole column on or off
def handler_col(i=boat_num):
return self.select_full_column(i)
# Freeze the row number to toggle whole row on or off
def handler_row(i=boat_slot):
return self.select_full_row(i)
# For button Button_Name[0][0] to control all boats/slots
if boat_slot == 0 and boat_num == 0:
self.Button_Name[boat_num][boat_slot] = Checkbutton(self.canvas,
text = 'All Boats_Slots',
padx=5, pady=5,
relief = SUNKEN,
width = 15,
bg = 'Moccasin',
variable = self.buttonChecked[boat_num][boat_slot],
command = self.select_all_boats)
# For button Button_Name[i][0], i is range(1,boat_num+1) to control all slots in boat i
elif boat_slot == 0:
self.Button_Name[boat_num][boat_slot] = Checkbutton(self.canvas,
text = 'Boat_' + str(boat_num),
padx=5, pady=5,
relief = SUNKEN,
width = 15,
bg = 'light yellow',
variable = self.buttonChecked[boat_num][boat_slot],
command = handler_col)
# For button Button_Name[0][i], i is range(1,boat_slot+1) to control all slots in row i
elif boat_num == 0:
self.Button_Name[boat_num][boat_slot] = Checkbutton(self.canvas,
text = 'Slot_' + str(boat_slot),
padx=5, pady=5,
relief = SUNKEN,
width = 15,
bg = 'light yellow',
variable = self.buttonChecked[boat_num][boat_slot],
command = handler_row)
# For single button control
else:
self.Button_Name[boat_num][boat_slot] = Checkbutton(self.canvas,
textvariable = self.buttonNameVar[boat_num][boat_slot],
padx=5, pady=5,
relief = SUNKEN,
variable = self.buttonChecked[boat_num][boat_slot],
width = 15,
command = self.select_current_slot)
# Lay out check buttons with grid manager
self.Button_Name[boat_num][boat_slot].grid(row = boat_slot+1, rowspan = 1, column = boat_num + 2, columnspan = 1)
# Full boats/slots toggle function
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()
# The column toggle function
def select_full_column(self, i):
for j in xrange(1, self.rows):
self.Button_Name[i][j].toggle()
# The row toggle function
def select_full_row(self, i):
for j in xrange(1, self.cols):
self.Button_Name[j][i].toggle()
def select_current_slot(self):
pass
boat1 = BoatManager('haiyan', 10,100)
boat1.mainloop()
Comment