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

4

11

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

Pyrrolidine answered 1/8, 2018 at 10:31 Comment(2)
Please provide code, so we can see where you can fix it. Probably a minimal GUI with two buttons extracted from you code should be enough.Electronics
You can put a single horizontal box, and then fill it with your buttons. The default is probably a vertical box. (Sorry I have not used tk for a long time, so don't remember the names.)Iz
B
22

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.

Bloodcurdling answered 1/8, 2018 at 10:35 Comment(2)
Is there a reason why you put b1.grid(column=0, row=0) twice? Anyway, this works like intended. thanks.Pyrrolidine
I'll add a tick in 8 minutes, i cannot do that now :)Pyrrolidine
A
2

Simply use this to make the y coordinates the same and change the x coordinate:

from tkinter import * 
root = Tk()

Button(root, text='Submit', width=10, bg='blue', fg='white', 
command=database).place(x=70, y=130)

For the second button:

buttonSignIn = Button(root, text="Sign in", width=10, bg='black', 
fg='white', command=new_winF).place(x=30, y=130)
Ardra answered 1/8, 2018 at 10:39 Comment(1)
This works too, but @Reblochon Masque made it easier, as I am a beginner to GUI programming, but thank you :)Pyrrolidine
C
0

I had the same problem once, and found this: two "simple" ways to move widgets around a GUI area, are

i) Using the ".grid" attribute (see example below):

MyButton_FilePath = Button(
                           master  = gui,
                           text    = 'Open',
                           command = funcion_openfile_findpath,
                           fg      = 'Black', font = ('Arial bold',11)
                          )
MyButton_FilePath.grid(row = 0, column = 2, padx = 4, pady = 4)

ii) Or using the attribute ".place":

MyButton_FilePath = Button(
                           master  = gui,
                           text    = 'Open',
                           command = funcion_openfile_findpath,
                           fg      = 'Black', font = ('Arial bold',11)
                          )
MyButton_FilePath.place(x=300, y=400)

Note that I have separated the "Button" object into two lines - as it is considered to be a better practice whenever placing/gridding widgets...

Hope I have helped.

Try both ways and see which one fits better your wishes! :)

Cheers, Marcos Moro, PhD

Caputto answered 21/4, 2022 at 14:47 Comment(1)
Your customtkinter wasn't back in future.Kal
P
0

The solution that I have come up with uses the newer Custom TK inter module within a canvas. The buttons can be on the same row as another button if you use the sticky tag in the grid position.

 CTkLabel(button_canvas, text="Gain", font=("Courier", 14)).grid(row=6, column=0, pady=10, padx=15)
 CTkButton(button_canvas, text="^", command=self.gain_up, width=30).grid(row=6, column=1, padx=15, sticky="W")
 CTkButton(button_canvas, text="v", command=self.gain_down, width=30).grid(row=6, column=1, padx=15)

Works a treat.

Pathfinder answered 22/6, 2023 at 10:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.