top of page
Writer's pictureCodersarts AI

Python Tkinter widgets | How to create python GUI using Tkinter widgets


Python Tkinter widgets:


Before start tkinter widgets first we know about some geometry manager which is used to create a widgets.

Grid() and pack() Geometry manager in python:

The Grid geometry manager puts the widgets in a 2-dimensional table. The master widget is split into a number of rows and columns, and each “cell” in the resulting table can hold a widget.

The grid manager is the most flexible of the geometry managers in Tkinter. If you don’t want to learn how and when to use all three managers, you should at least make sure to learn this one.

The grid manager is especially convenient to use when designing dialog boxes.

Never mix grid() and pack() in the same master window.

Patterns:

Using the grid manager is easy. Just create the widgets, and use the grid method to tell the manager in which row and column to place them. You don’t have to specify the size of the grid beforehand; the manager automatically determines that from the widgets in it.

Label(master, text="First").grid(row=0)

Label(master, text="Second").grid(row=1)

e1 = Entry(master)

e2 = Entry(master)

e1.grid(row=0, column=1)

e2.grid(row=1, column=1)

Sticky:

Defines how to expand the widget if the resulting cell is larger than the widget itself. This can be any combination of the constants S, N, E, and W, or NW, NE, SW, and SE.

For example, W (west) means that the widget should be aligned to the left cell border. W+E means that the widget should be stretched horizontally to fill the whole cell. W+E+N+S means that the widget should be expanded in both directions. Default is to center the widget in the cell.

Label(master, text="First").grid(row=0, sticky=W)

Label(master, text="Second").grid(row=1, sticky=W)

e1 = Entry(master)

e2 = Entry(master)

e1.grid(row=0, column=1)

e2.grid(row=1, column=1)

You can also have the widgets span more than one cell. The columnspan option is used to let a widget span more than one column, and the rowspan option lets it span more than one row. The following code creates the layout shown in the previous section:

label1.grid(sticky=E)

label2.grid(sticky=E)

entry1.grid(row=0, column=1)

entry2.grid(row=1, column=1)

checkbutton.grid(columnspan=2, sticky=W)

image.grid(row=0,column=2,columnspan=2,rowspan=2 , sticky=W+E+N+S, padx=5, pady=5)

button1.grid(row=2, column=2)

button2.grid(row=2, column=3)

padx: Optional horizontal padding to place around the widget in a cell. Default is 0.

pady: Optional vertical padding to place around the widget in a cell. Default is 0.

in: Place widget inside to the given widget. You can only place a widget inside its parent, or in any decendant of its parent. If this option is not given, it defaults to the parent.Note that in is a reserved word in Python. To use it as a keyword option, append an underscore (in_).

row=

Insert the widget at this row. Row numbers start with 0. If omitted, defaults to the first empty row in the grid.

rowspan=

If given, indicates that the widget cell should span multiple rows. Default is 1.

In our next blogs we start the complete description about the tkinter widgets.

If any query or a problem regarding python programming or tkinter widgets you can directly contact through given link, click here to reach out the codersarts expert team.

4 views0 comments
bottom of page