In my Tkinter
application I frequently need to align columns of Tkinter.Frame
with the grid_columnconfigure
method. I use simple uniform approach to place widgets in the frame:
Tkinter.Frame
classgrid
manager and every column has the same weightHere is the code of one of my custom-made frames. SFrame
is just a class-wrapper for a Tkinter.Frame
with a style:
class GroupFrame( SFrame ) :
"""
This class represents a frame with a title at the top
"""
def __init__( self, parent, style, title, **kwargs ) :
SFrame.__init__( self, parent, style, **kwargs )
self.config( borderwidth=5, relief=tk.SUNKEN )
self.style = style
self.title = title.upper()
self.CreateFrames()
self.FillFrames()
def CreateFrames( self ) :
self.titleFrame = SFrame( self, self.style )
self.titleFrame.pack( side="top", pady=10 )
self.contentFrame = SFrame( self, self.style )
self.contentFrame.pack( side="bottom", pady=(0, 10), fill='x' )
def FillFrames( self ) :
self.titleLabel = SLabel( self.titleFrame, self.style, text=str( self.title ) )
self.titleLabel.config( font=self.style.groupTitleFont, borderwidth=3, relief=tk.SUNKEN , padx=3)
self.titleLabel.pack( pady=(1, 5) )
def AlignColumns( self, nCols ): #<----- Look at this function
for i in range( nCols ):
self.contentFrame.grid_columnconfigure( i, weight=1 )
Here is how I use it and the result (SButton
is a class-wrapper for a Tkinter.Button
with a style):
#button to go to the common settings' page: settings for all channels
self.settingsFrame = GroupFrame( self.midFrame, self.style, "SETTINGS" )
self.settingsFrame.grid( row=0, column=0, pady=10, padx=10, sticky="ew" )
self.commonButton = SButton( self.settingsFrame.contentFrame, self.style,
text="Common Settings",
command=lambda: self.controller.ShowPage( "Common" ),
height=2 )
self.commonButton.grid( row=0, column=0, padx=10, sticky="ew" )
#button to go to the individual settings' page: settings per channel
self.individButton = SButton( self.settingsFrame.contentFrame, self.style,
text="Individual Settings",
command=lambda: self.controller.ShowPage( "Individual" ),
height=2 )
self.individButton.grid( row=0, column=1, padx=10, sticky="ew" )
#button to go to the terminal page
self.terminalButton = SButton( self.settingsFrame.contentFrame, self.style,
text="Terminal",
command=lambda: self.controller.ShowPage( "Terminal" ),
height=2 )
self.terminalButton.grid( row=0, column=2, padx=10, sticky="ew" )
#Set the same weight to each column
self.settingsFrame.AlignColumns( 3 )
I wish columns of each frame had the same weight. As you can see I use the AlignColumns
method for this purpose. And it is not as generic as I want it to be because I have to know the exact number of columns I use in the frame. I wonder if there is a way to know how many columns are already taken in Tkinter.Frame
?
Given any widget that has children managed by grid
, you can call grid_size
on that widget to get back a 2-tuple with the number of columns and then the number of rows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With