如何知道在 Tkinter.Frame 中有多少行/列

LRD*_*RDX 2 python tkinter

预赛

在我的Tkinter应用程序中,我经常需要将列Tkinter.Framegrid_columnconfigure方法对齐。我使用简单的统一方法在框架中放置小部件:

  1. 我有从类继承的定制框架Tkinter.Frame
  2. 我将小部件放置在与grid管理器一起定制的框架中,并且每列都具有相同的权重

代码

定制框架

这是我定制的框架之一的代码。SFrame只是一个Tkinter.Frame带有样式的类包装器:

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 )
Run Code Online (Sandbox Code Playgroud)

使用示例

这是我如何使用它和结果(SButton是一个Tkinter.Button带有样式的类包装器):

#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 )
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

问题

我希望每帧的列具有相同的权重。如您所见,我AlignColumns为此目的使用了该方法。而且它不像我想要的那样通用,因为我必须知道我在框架中使用的确切列数。我想知道是否有办法知道已经有多少列Tkinter.Frame

Bry*_*ley 6

给定任何具有由 管理的子部件的小部件grid,您可以调用grid_size该小部件以获取包含列数和行数的 2 元组。