Tkinter网格高度限制

时间:2014-10-24 11:45:13

标签: python tkinter

我需要创建一个包含大量行(超过2000 )的表。每行应该能够容纳小部件。但是当我尝试在 1714th 之后添加条目时,网格会停止增长,并且所有以下小部件都会相互绘制。该问题似乎与网格的高度有关,因为如果我更改条目的边框宽度,该值也将更改。

import Tkinter as tk

class Example( tk.Frame ):

    def __init__( self, root ):
        tk.Frame.__init__( self, root )
        self.canvas = tk.Canvas( root,       background = "#ffffff",  borderwidth = 0 )
        self.frame  = tk.Frame( self.canvas, background = "#ffffff" )
        self.vsb    = tk.Scrollbar( root,    orient     = "vertical",
                                             command    = self.canvas.yview )
        self.canvas.configure( yscrollcommand = self.vsb.set )

        self.vsb.pack(    side = "right", fill = "y" )
        self.canvas.pack( side = "left",  fill = "both", expand = True )
        self.canvas.create_window( ( 4, 4 ), window = self.frame, anchor = "nw",
                                             tags   = "self.frame" )

        self.frame.bind( "<Configure>", self.on_frame_configure )
        self.populate()

    def update( self ):
        self.frame.update()

    def add( self ):
        for id in xrange( 2000 ):
            entry_id = tk.Entry( self.frame, width       = 10, justify = 'center',
                                             borderwidth = "1",
                                             relief      = "solid",
                                             bg          = '#C8D4FA' )
            entry_id.grid( row = 1 + id, column = 0 )
            entry_id.insert( 1, 1 + id )

    def populate( self ):
        self.frame.grid_rowconfigure( 0, weight = 1 )
        button = tk.Button( self.frame, text = 'Click', command = self.add ).grid( row = 0, column = 0 )

    def on_frame_configure( self, event ):
        '''Reset the scroll region to encompass the inner frame'''
        self.canvas.configure(  scrollregion = self.frame.bbox( 'all' ) )
        #self.canvas.configure( scrollregion = self.canvas.grid )

def main():
    root    = tk.Tk()
    example = Example( root )
    root.mainloop()

if __name__ == '__main__':
    main()

0 个答案:

没有答案