Python Tkinter Tutorial
Table of Contents
In this tutorial, we will learn How to use Tkinter for GUI of any application.
Python Tkinter Tutorial provides many libraries for GUI development. Out of those, Tkinter is the most widely used library. You don’t need to install this library separately, it comes embedded with a python interpreter. So when you install python it automatically gets installed.
How to start with Python Tkinter Tutorial:
First of all, if you are using any other interpreter rather than Python then you need to check whether Tkinter is installed or not.
If all this runs successfully then it is ready to use otherwise you have to install it first.
Run the following code:
from tkinter import *
from tkinter import ttk
The first line imports the Tkinter library for our application. Second-line import the ttk module for using themed widget sets. The third line creates a root or parent window which will contain all other widgets needed for the application. The fourth line calls the method ‘mainloop()’ which will run the application and it will run infinitely.
Why do we import ttk separately?
By seeing the code one can think although we have imported all the modules using “*”, why are we importing ttk separately? Because when you see it’s code for init.py, you will notice that ttk will not get imported to your code by wildcard import. So this is the reason we have to import ttk explicitly for using it in our code.
Note: You can also use the widget of Tkinter but ttk offers more widgets with improved features. So it’s better to use widgets of the ttk module.
Managing Geometry in Tkinter:
There are three methods available for managing geometry in tkinter.
- Pack Geometry Manager
- Place Geometry Manager
- Grid Geometry Manager
Pack Geometry Manager:
By using a pack geometry manager, we have to specify the position of widgets by using alignments such as left, right, top and bottom, etc.
Place Geometry Manager:
In this geometry manager position of widgets is specified by specifying its absolute position in pixels as we in HTML.
Grid Geometry Manager:
In this geometry manager, the whole window get divided in the form of a 2D array and the position of each widget is specified by its corresponding row and column.
Event Handling in Python Tkinter Tutorial:
If we are developing a GUI then we also need to handle its corresponding events. So there are multiple events are for each widget.
|When Event Occurs||Event Handler Code|
Some commonly used Widgets:
The label is a Tkinter widget and it is also available in the ttk module. We can use labels for specifying some textual information or we can also insert images in a label.
label=ttk.Label(root,text=’ I am a Label’) # root decides the parent window in which it will be placed
label.pack() #by using pack we are calling it on its parent window that is the root
The button is one of the widely used widgets. We can use it for firing some events on its click event.
button=ttk.Button(root,text=’Click Me’)# root decides the parent window in which it will be placed
button.pack() #by using pack we are calling it on its parent window that is the root
3. Radio Button
We can use radio button for selecting a single value from multiple choices available. for using a radio button first of all we have to create a StringVar() that will be used getting and setting the value of radion button
city.get() # it will output the value of selected choice
4. CheckButton (CheckBox)
CheckButton is the same as the checkbox available in HTML.
ComboBox is used for creating a dropdown list and by using this we can select one or multiple values.
month.get() #display the selected value from combobox
This widget can be used for getting a single line input from user such as comments, suggestions, etc.
Frames are one of the most useful widgets because these are helpful in geometry management.
frame.pack() #default frame size is zero
frame.config(width=100,height=50) # these are given in pixels
So these are some commonly used widgets in tkinter. Rather than these, there are multiple widgets available for use such as PanedWindow, Top-Level Window, Text, SpinBox, ProgressBar, etc. For More Information follow this page Python Tkinter Tutorial and for Python Tutorial Follow this page Python Tutorial.