Setting the position on a button in Tkinter Python?


There are certain ways through which Tkinter widgets can be positioned inside a window. Tkinter Geometry manager has three methods, pack(), place() and grid(), through which we can set the position of the widget in an application window. Each of these ways has its own limitations and uses. To set the position of a button on a Tkinter application window, we can prefer to use the place(x-coordinates, y-coordinates) method over all the other methods. It takes the values of x and y coordinates which are required to define the position of a widget.

Example

The sample code contains a button widget that uses the place(x, y) method to position the button in the window.

# Import the Tkinter library
from tkinter import *
from tkinter import ttk
# Create an instance of Tkinter frame
win = Tk()
# Define the geometry
win.geometry("750x250")
def close_win():
   win.destroy()
# Create Buttons in the frame
button = ttk.Button(win, text="Click", command=close_win)
button.place(x=325, y=125)
#Create a Label
Label(win, text="Click the Button to Close the Window", font=('Consolas 15')).pack()
win.mainloop()

Output

Running the above code will display a window containing a button at the center position of the window.

Updated on: 22-Apr-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements