10

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

E_net4
  • 27,810
  • 13
  • 101
  • 139
Remigiusz Schoida
  • 332
  • 1
  • 2
  • 10
  • 1
    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. – kabanus Aug 01 '18 at 10:32
  • 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.) – ctrl-alt-delor Aug 01 '18 at 10:39

4 Answers4

20

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.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
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)
hjpoe
  • 96
  • 10
bugfreerammohan
  • 1,471
  • 1
  • 7
  • 22
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

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.