Python 如何知道一个Tkinter.Frame中有多少行/列 预备赛
在我的Python 如何知道一个Tkinter.Frame中有多少行/列 预备赛,python,tkinter,Python,Tkinter,在我的Tkinter应用程序中,我经常需要将Tkinter.Frame的列与grid\u column configure方法对齐。我使用简单的统一方法在框架中放置小部件: 我有从Tkinter.frame类继承的定制框架类 我使用网格管理器将小部件放置在定制的框架中,每一列都具有相同的权重 代码 定做框架 这是我的一个定制框架的代码SFrame只是Tkinter.Frame的类包装器,其样式如下: class GroupFrame( SFrame ) : """ Thi
Tkinter
应用程序中,我经常需要将Tkinter.Frame
的列与grid\u column configure
方法对齐。我使用简单的统一方法在框架中放置小部件:
Tkinter.frame
类继承的定制框架类网格管理器将小部件放置在定制的框架中,每一列都具有相同的权重
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 )
#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 )
问题
我希望每一帧的柱子都有相同的重量。如您所见,我为此使用了
AlignColumns
方法。它不像我希望的那样通用,因为我必须知道我在框架中使用的列的确切数量。我想知道是否有办法知道在Tkinter.Frame
?给定任何由grid
管理子部件的小部件,您可以在该小部件上调用grid\u size
以获取列数和行数的2元组。这正是我需要的,谢谢。不知道我怎么会错过。