How do I position the buttons on a Tkinter window?


To set the position of a button, we use the place method of button widget. The place method takes the x and y coordinates of the button.

Steps −

  • Import the required libraries and create an instance of tkinter frame.

  • Set the size of the frame using win.geometry method.

  • Next, create multiple buttons and name them "Button-1", "Button-2", etc.

  • Set the position of the buttons using the place method by supplying the x and y coordinate values.

  • Finally, run the mainloop of the application window.

Example

# Import the Tkinter library
from tkinter import *
from tkinter import ttk

# Create an instance of Tkinter frame
win = Tk()

# Define the geometry
win.geometry("750x350")

# Create Buttons in the frame
button = ttk.Button(win, text="Button-1")
button.place(x=325, y=125)

button = ttk.Button(win, text="Button-2")
button.place(x=325, y=175)

button = ttk.Button(win, text="Button-3")
button.place(x=325, y=225)

#Create a Label
Label(win, text="Position the Buttons", font='Consolas 15').pack()

win.mainloop()

Output

When you execute this code, it will show the following window −

Notice that we have fixed the x variable at 325 in all the three buttons which is why the buttons are aligned. You can alter the (x, y) values in the place method to change the position of the buttons.

Updated on: 26-Oct-2021

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements