Python Tkinter grid-geometry sticky setting not effective(?)

前端 未结 1 420
借酒劲吻你
借酒劲吻你 2021-02-10 14:37

I\'m designing simple entry dialog in Python/Tkinter using the grid geometry, and getting some unexpected behavior. When I start out with this code:



        
1条回答
  •  生来不讨喜
    2021-02-10 15:16

    When you make the entry widget bigger, Tkinter has to allocate extra space to either column 0 or column 1. It does this according to the "weight" each column has.

    Because you haven't explicitly set a weight, all columns get the default weight of zero. In your case, however, something has to grow. Tkinter chooses the first column if no columns have weight. This explains why entID gets pushed over -- the first column is made wider to accommodate the widget entry widget.

    If you give column 1 a weight of 1, any extra space will be allocated to that column, leaving column 0 to be just wide enough to hold whatever is constraining that column (ie: any widgets that don't span columns). You do this with the grid_columnconfigure command.

    For example:

    winAddNew.grid_columnconfigure(1, weight=1)
    

    Note that weight can be any positive integer; it represents a ratio. So, for example, if you set column 0 to 1 and column 1 to 2, column 1 will be given twice the amount of extra space that column 0 is given.

    Tkinter does a poor job of documenting this behavior. The definitive documentation is in the Tcl/Tk man pages. Specifically, see Grid Algorithm in the grid man page.

    0 讨论(0)
提交回复
热议问题