Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to position several widgets side by side, on one line, with tkinter?

By default, after making a tkinter button, it automatically puts the next one on the other line.
How do I stop this from happening?
I want to do something like this:
enter image description here

like image 989
Remigiusz Schoida Avatar asked Aug 01 '18 10:08

Remigiusz Schoida


1 Answers

You must use one of the geometry managers for that:

here with grid:

import tkinter as tk

root = tk.Tk()

b1 = tk.Button(root, text='b1')
b2 = tk.Button(root, text='b2')
b1.grid(column=0, row=0)   # grid dynamically divides the space in a grid
b2.grid(column=1, row=0)   # and arranges widgets accordingly
root.mainloop()

there using pack:

import tkinter as tk

root = tk.Tk()

b1 = tk.Button(root, text='b1')
b2 = tk.Button(root, text='b2')
b1.pack(side=tk.LEFT)      # pack starts packing widgets on the left 
b2.pack(side=tk.LEFT)      # and keeps packing them to the next place available on the left
root.mainloop()

The remaining geometry manager is place, but its use is sometimes complicated when resizing of the GUI occurs.

like image 103
Reblochon Masque Avatar answered Dec 30 '22 00:12

Reblochon Masque