INTRODUCTION :
Tkinter is known as the most popular GUI(Graphical User Interface) library in Python. It gives us an object- oriented interface to the TK GUI toolkit .
Here I will explain the proper usage of this library and it's features.
Remarks :The capitalization of the tkinter module is different between Python 2 and 3.
For Python 2 use the following syntax.
from Tkinter import* # Capitalized
For Python 3 use the following syntax.
from tkinter import* #lowercase
the code that works with both Python and 3, you can either do this try:
from Tkinter import *
except ImportError:
from tkinter import *
or
from sys import version_info
if version_info.major == 2:
from Tkinter import *
elif version_info.major == 3:
from tkinter import *
Fundamentals of tkinter :
Execution of Application
- Import the tkinter module
- create the GUI application main Window
- Add widgets
- Enter the main event loop (This will open and run the application until it is stopped by the window being closed or calling exiting functions from callbacks)
First Window using tkinter:
import tkinter
window =tkinter .Tk()
window.title("Welcome") #to rename the title of the window
label =tkinter.Label(window , text= "Hello World!").pack() #pack is used to show the object in the window
window.mainloop() #main event loop
Comments