Tkinter Package:
Python provides the standard library Tkinter for creating the graphical user interface for
desktop based applications.
An empty Tkinter top-level window can be created by using the following steps.
1. import the tkinter module.
2. Create the main application window.
3. Add the widgets like labels, buttons, frames, etc. to the window.
4. Call the main event loop so that the actions can take place on the user's computer screen.
Example
from tkinter import *
#creating the application main window.
top = Tk()
#Entering the event main loop
top.mainloop()
Tkinter widgets
There are various widgets like button, canvas, checkbutton, entry, etc. that are used to build
the python GUI applications.
Button: It is used to add buttons to your application
Entry: It is used to input single line text entry from user
Radiobutton: It is used to implement one-of-many selection as it allows only
one option to be selected
Text: Create a multiline text input with advanced editing capabilities.
Canvas: Draw shapes, lines, text, and images.
CXheckbutton: Create checkboxes for boolean options.
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.
1. The pack(): The Pack geometry manager packs widgets in rows or columns.
2. The grid() : 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.
3. The place() : It allows you explicitly set the position and size of a window,
either in absolute terms, or relative to another window.
Let's discuss each one of them in detail.
pack() method
The pack() widget is used to organize widget in the block.
The widgets positions can be controlled by using the various options specified in the
method call using the pack() method.
The syntax to use the pack() is given below.
Syntax:
widget.pack(options)
Example1:
from tkinter import *
parent = Tk()
rb = Button(parent, text = "Red", fg = "red")
rb.pack( side = LEFT)
gb = Button(parent, text = "Black", fg = "black")
gb.pack( side = RIGHT )
parent.mainloop()
OUTPUT:
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.
Syntax
widget.grid(options)
Example:
from tkinter import *
parent = Tk()
name = Label(parent,text = "Name").grid(row = 0, column = 0)
e1 = Entry(parent).grid(row = 0, column = 1)
password = Label(parent,text = "Password").grid(row = 1, column = 0)
e2 = Entry(parent).grid(row = 1, column = 1)
submit = Button(parent, text = "Submit").grid(row = 4, column = 0)
parent.mainloop()
OUTPUT:
ab: 5,18,28,32,33,48,22-16,34,65