How to position buttons in Tkinter with Grid

Опубликовано: 22 Октябрь 2024
на канале: ActiveState
18,420
26

How to Position Buttons in Tkinter With Grid
Pack() has two options you can use: row and column

The row option aligns buttons horizontally.
The column option aligns buttons vertically.
In this example, grid() is used to position buttons based on row and column coordinates on a grid in a frame:

import tkinter

master=tkinter.Tk()
master.title("grid() method")
master.geometry("350x275")

button1=tkinter.Button(master, text="button1")
button1.grid(row=1,column=0)

button2=tkinter.Button(master, text="button2")
button2.grid(row=2,column=2)

button3=tkinter.Button(master, text="button3")
button3.grid(row=3,column=3)

button4=tkinter.Button(master, text="button4")
button4.grid(row=4,column=4)

button5=tkinter.Button(master, text="button5")
button5.grid(row=5,column=5)

master.mainloop()

Learn more about Tkinter and other Python packages at https://www.activestate.com/learn-python