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:
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.
b1.grid(column=0, row=0)
twice? Anyway, this works like intended. thanks. –
Pyrrolidine 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)
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
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.
© 2022 - 2024 — McMap. All rights reserved.