When to use pack or grid layouts in tkinter?

后端 未结 3 1105
灰色年华
灰色年华 2020-12-29 20:53

Are there any best practice tips regarding when one should use pack vs. grid for their layouts?

From what I\'ve been reading via google, the concencus seems to be th

3条回答
  •  暖寄归人
    2020-12-29 21:22

    Neither is intrinsically better than the other. Each have strengths and weaknesses. Learn what those are and the choice of which to use becomes obvious.

    grid is considerably easier to use if you need to lay things out in a grid. pack is generally easier to use if all you need to do is put some widgets in a single row or single column. There's a whole lot of gray area in-between where neither is necessarily better than the other.

    The other thing to consider is what you said in your question: if you want to show and hide widgets at run-time, grid is probably the best choice because of the grid_remove method which remembers the values of all of the configured attributes in case you want to re-add the widget.

    Personally, my first choice is always to use pack because I first learned Tk back when there was no grid command. If I can't do it easily in pack, or if I'm very clearly laying things out in a grid, I'll use grid.

提交回复
热议问题