Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know how many rows/columns are taken in a Tkinter.Frame

Tags:

python

tkinter

Preliminaries

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:

  1. I have custom-made frame-classes inherited from the Tkinter.Frame class
  2. I place widgets in custom-made frame with the grid manager and every column has the same weight

Code

Custom-made frame

Here 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 )

Usage example

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 )

enter image description here

Problem

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?

like image 811
LRDPRDX Avatar asked Oct 20 '25 04:10

LRDPRDX


1 Answers

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.

like image 127
Bryan Oakley Avatar answered Oct 21 '25 17:10

Bryan Oakley