The tkinter package (“Tk interface”) is the standard Python interface to the Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, as well as on Windows systems.
How to import tkinter:
import tkinter
or
from tkinter import *
Our First Tkinter Program (File: hello1.py)
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
# Code to add widgets will go here...
root.mainloop()
After Run the code:
python hello1.py
Output:
Explanation:
from Tkinter import *
It contains all classes, functions and other things needed to work with the Tk toolkit.
root = Tk()
To initialize Tkinter, we have to create a Tk root widget. This is an ordinary window, with a title bar and other decoration provided by your window manager.
w = Label(root, text="Hello, world!")
w.pack()
A Label widget can display either text or an icon or other image. In this case, we use the text option to specify which text to display.
root.mainloop()
The program will stay in the event loop until we close the window.