When a Python GUI application is designed with Tkinter, widgets (including buttons) require programming so that the underlying application logic can respond to mouse clicks and other actions. Additionally, in order for them to work as intended, widgets need to be positioned intuitively for the user.
Tkinter has three built-in layout managers that use geometric methods to position buttons in an application frame: pack, grid, and place.
- The pack manager organizes widgets in horizontal and vertical boxes that are limited to left, right, top, bottom positions offset from each other.
- The grid manager locates widgets in a two dimensional grid using row and column relative coordinates.
- The place manager places widgets in a two dimensional grid using x and y absolute coordinates.
How to Position Buttons With Place
place() has two options you can use: x and y
- The x variable aligns buttons horizontally.
- The y variable aligns buttons vertically.
In this example, place() is used to position buttons based on x,y coordinates in a frame:
import tkinter master=tkinter.Tk() master.title("place() method") master.geometry("450x350") button1=tkinter.Button(master, text="button1") button1.place(x=25, y=100) button2=tkinter.Button(master, text="button2") button2.place(x=100, y=25) button3=tkinter.Button(master, text="button3") button3.place(x=175, y=100) master.mainloop()
Learn more about Tkinter and how to install it here.
If you want to take advantage of the latest version of Tkinter, you’ll need to install a version of Python that supports Tcl/Tk 8.5 or greater. This will provide you with the Ttk (Tile extension integrated into Tk), which is required in order to run the current Tk widget set.
There are two other ways of installing a button in Tkinter – find them here.
Other Python Packages For Data Science, Web Development, Machine Learning, Code Quality And Security
ActivePython includes over 400 of the most popular Python packages. We’ve built the hard-to-build packages so you don’t have to waste time on configuration…get started right away! Learn more here.
Related Links
Frequently Asked Questions
How do you use Tkinter to change the position of a button in Python?
Tkinter button position can be changed using any one of three built-in layout managers that use geometric methods to position buttons in an application frame:
- The pack manager organizes widgets in horizontal and vertical boxes that are limited to left, right, top, bottom positions offset from each other.
- The grid manager locates widgets in a two dimensional grid using row and column relative coordinates.
- The place manager places widgets in a two dimensional grid using x and y absolute coordinates.
Learn more about how to position buttons in Tkinter with pack or with grid.
What is the use of the place () function in Tkinter for button positioning?
Tkinter is the most popular way to create graphical user interfaces in Python. Tkinter’s place function lets you very easily manipulate the placement of Tkinter button positions and other widgets in a two dimensional grid using x and y absolute coordinates.
For a more general overview, refer to How To Position Buttons in Tkinter.
How do you use padx and pady in Tkinter to position a button?
When working with Tkinter button positioning using the pack() function, you will likely want to use padding options to better position the button (or other widget) in relation to the left, right, top, bottom sides of the widget. There are four padding options you can use:
- padx, which pads externally along the x axis.
- pady, which pads externally along the y axis.
- ipadx, which pads internally along the x axis.
- ipady, which pads internally along the y axis.
For more information, refer to How To Use Pack in Tkinter.
How do I center a frame in tkinter?
The simplest way to center a frame using Tkinter is to use the place() function, which allows you to use relative or absolute positioning. For example, you could use relative x and y values of 1 and 1, along with an anchor value of c (center).
For a more general overview, refer to How To Position Buttons in Tkinter.