Python provides the standard library for creating the graphical user interface for desktop based applications.
Developing desktop based applications with python Tkinter is not a complex task. An empty Tkinter top-level window can be created by using the following steps.
import the Tkinter module.
Create the main application window.
Add the widgets like labels, buttons, frames, etc. to the window.
Call the main event loop so that the actions can take place on the user's computer screen.
Example
# !/usr/bin/python3
from tkinter import *
#creating the application main window.
top = Tk()
#Entering the event main loop
top.mainloop()
Output:
Tkinter widgets
There are various widgets like button, canvas, checkbutton, entry,
etc. that are used to build the python GUI applications.
This module is used to display the message-box in the desktop based applications.
Python Tkinter Geometry
The Tkinter geometry specifies the method by using which, the widgets are represented on display. The python Tkinter provides the following geometry methods.
The pack() method
The grid() method
The place() method
Let's discuss each one of them in detail.
Python Tkinter pack() method
The pack() widget is used to organize widget in the block. The positions widgets added to the python application using the pack() method can be controlled by using the various options specified in the method call.
However, the controls are less and widgets are generally added in the less organized manner.
The syntax to use the pack() is given below.
syntax
widget.pack(options)
A list of possible options that can be passed in pack() is given below.
expand: If the expand is set to true, the widget expands to fill any space.
Fill: By default, the fill is set to NONE. However, we can set it to X or Y to determine whether the widget contains any extra space.
size: it represents the side of the parent to which the widget is to be placed on the window.
Example
# !/usr/bin/python3
from tkinter import *
parent = Tk()
redbutton = Button(parent, text = "Red", fg = "red")
redbutton.pack( side = LEFT)
greenbutton = Button(parent, text = "Black", fg = "black")
greenbutton.pack( side = RIGHT )
bluebutton = Button(parent, text = "Blue", fg = "blue")
bluebutton.pack( side = TOP )
blackbutton = Button(parent, text = "Green", fg = "red")
blackbutton.pack( side = BOTTOM)
parent.mainloop()
Output:
Python Tkinter grid() method
The grid() geometry manager organizes the widgets in the tabular form. We can specify the rows and columns as the options in the method call. We can also specify the column span (width) or rowspan(height) of a widget.
This is a more organized way to place the widgets to the python application. The syntax to use the grid() is given below.
Syntax
widget.grid(options)
A list of possible options that can be passed inside the grid() method is given below.
Column
The column number in which the widget is to be placed. The leftmost column is represented by 0.
Columnspan
The width of the widget. It represents the number of columns up to which, the column is expanded.
ipadx, ipady
It represents the number of pixels to pad the widget inside the widget's border.
padx, pady
It represents the number of pixels to pad the widget outside the widget's border.
row
The row number in which the widget is to be placed. The topmost row is represented by 0.
rowspan
The height of the widget, i.e. the number of the row up to which the widget is expanded.
Sticky
If the cell is larger than a widget, then sticky is used to specify the position of the widget inside the cell. It may be the concatenation of the sticky letters representing the position of the widget. It may be N, E, W, S, NE, NW, NS, EW, ES.
Example
# !/usr/bin/python3
from tkinter import *
parent = Tk()
name = Label(parent,text = "Name").grid(row = 0, column = 0)