62
Introduction of Tkinter
Developed based on Tkinter, our client program carries
graphical interfaces. Tkinter
is a GUI widget set for Python. We can develop application programs with graphical
interfaces fast by Python language based on it. It is quite easy to use Tkinter. All
you have to do is to
import the module into Python.
To create and run a GUI program, take the following steps:
a)
Import the Tkinter module (by
import Tkinter
or
from Tkinter import *
).
b)
Create a top window object to contain the whole GUI program.
c)
Create the GUI module needed on the object and enable the functions.
d)
Connect the GUI modules with the code at the system back-end.
e)
Enter the main event loop.
Take
a simple GUI program:
Create a file
Tk_test.py
under the path
/home
:
touch Tk_test.py
Add executable privilege to the file:
chmod +x Tk_test.py
Open the file:
vim Tk_test.py
Type in the following code:
#!/usr/bin/env python
from
Tkinter
import
*
top
=
Tk
()
# Create a top window
top
.
title
(
'Sunfounder.com'
)
label
=
Label
(
top
,
text
=
'Hello Geeks !'
,
fg
=
'blue'
)
# Create a label and set its foreground color as blue
label
.
pack
()
# layout
top
.
mainloop
()
# main loop
Save the code and exit.
Run
./Tk_test.py
Then the following picture will appear on your screen:
Click
to close the program.