Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The "best" geometry manager depends on what you want to do, no geometry manager is best for all situations. For what you're trying to do in this specific case, pack is probably the better choice. Also note that there may be more than one "best" in an application. It's quite normal to use both grid and pack in different parts of your applications. I rarely ever create a GUI where I don't use both grid and pack. And rarely, I also use place. </p> <p>pack excels at placing things in single rows and single columns. Toolbars, for example, are a perfect case for using pack since you want all your buttons aligned to the left. grid, as it name implies, is best when you want fixed rows and columns. place is useful in those rare cases where neither grid nor pack will do, since it allows you to place widgets at precise fixed or relative locations. </p> <p>My advice is to divide your application widgets into groups, and use the right geometry manager for each group. In your case you have two logical groups: the toolbar along the top, and a text-widget-and-scrollbar combination in the bottom. So, start with two frames. Since the toolbar is on top and the text widget is below, pack works best. </p> <pre><code>toolbar = tk.Frame(...) main = tk.Frame(...) toolbar.pack(side="top", fill="x", expand=False) main.pack(side="bottom", fill="both", expand=True) </code></pre> <p>The above now gives you two areas that are easy to modify independently. </p> <p>For the toolbar, pack is again most natural. In this case, however, you want the buttons along the left instead of along the top or bottom:</p> <pre><code>b1 = tk.Button(toolbar, ...) b2 = tk.Button(toolbar, ...) b1.pack(side="left") b2.pack(side="left") ... </code></pre> <p>Finally, the bottom area. Your example code doesn't show scrollbars, but I assume at some point you'll want to add them. grid works well if you're using both horizontal and vertical scrollbars. I would lay them out like this:</p> <pre><code>hsb = tk.Scrollbar(main, ..., orient="horizontal", ...) vsb = tk.Scrollbar(main, ..., orient="vertical", ...) text = tk.Text(main, ...) vsb.grid(row=0, column=1, sticky="ns") hsb.grid(row=1, column=0, sticky="ew") text.grid(row=0, column=0, sticky="nsew") main.grid_rowconfigure(0, weight=1) main.grid_columnconfigure(0, weight=1) </code></pre> <p>That last part is important -- you always need to give a weight to at least one row and one column. This tells grid which rows and columns should grow and shrink when the window is resized. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload