2. PYTHON PROGRAMMING TOPICS
I • Introduction to Python Programming
II • Python Basics
III • Controlling the Program Flow
IV • Program Components: Functions, Classes, Packages, and Modules
V • Sequences (List and Tuples), and Dictionaries
VI • Object-Based Programming: Classes and Objects
VII • Customizing Classes and Operator Overloading
VIII • Object-Oriented Programming: Inheritance and Polymorphism
IX • Randomization Algorithms
X • Exception Handling and Assertions
XI • String Manipulation and Regular Expressions
XII • File Handling and Processing
XIII • GUI Programming Using Tkinter
8. PYTHON GUI: TKINTER = Tcl + Tk
Tcl: Tool Command Language
invented in 1990
used for web and desktop applications, networking,
administration, scripted applications, embedded system,
rapid prototyping and testing.
9. PYTHON GUI: TKINTER = Tcl + Tk
Tcl: Tool Command Language
Interfacing with other Languages
q Tcl/C++
q Tcl/Java
10. PYTHON GUI: TKINTER = Tcl + Tk
Tcl: Tool Command Language
allows extension packages, which provide additional functionality,
such as a GUI, terminal-based application automation,
database access, etc.
q Tcl/Tk
q Tcl/Expect
11. PYTHON GUI: TKINTER = Tcl + Tk
Tk: ToolKit
open-source, cross platform widget toolkit for building GUI
other common widgets are:
* Java AWT and Java Swing (OpenOffice)
* Qt (Google Earth, Skype, Linux KDE windows)
* GTK+ (Linux Gnome windows)
* Cocoa (Mac OS)
* Silverlight (modern MS Windows NT)
12. PYTHON GUI: TKINTER = Tcl + Tk
Tk: ToolKit
created originally for Tcl
other language bindings also exist:
* Python/Tk = Tkinter
* Perl/Tk
* Ruby/Tk
13. PYTHON GUI: TKINTER = Tcl + Tk
Tkinter: Tk Interface
* wrapper for Tcl to access Tk
* Python and Tcl could co-exist in a Python file.
* Python interpreter also contains Tcl and Tk interpreter
17. PYTHON GUI: TKINTER = Tcl + Tk
Tk
A GUI toolkit provided as a library of C routines.
This library manages and manipulates the windows and
handles the GUI events and user interaction.
Tcl
The (mostly hidden) language used by Tk and, hence, used
by Tkinter to communicate with the Tk toolkit.
Tkinter
The Python Tk interface. A Python module that provides a
collection of Python classes and methods for accessing the Tk
toolkit from within Python.
18. PYTHON GUI: TKINTER
Widget
A user interface element
(text box, combo box, or top-level window).
On Windows, the common terminology is control or window.
19. PYTHON GUI: Pros
Brevity
Python programs using Tkinter can be very brief, partly because
of the power of Python, but also due to Tk.
Reasonable default values are defined for many options used in
creating a widget, and packing it
(i.e., placing and displaying).
20. PYTHON GUI: Pros
Cross platform
Tk provides widgets on Windows, Macs, and most Unix
implementations with very little platform-specific
dependence.
Some newer GUI frameworks are achieving a degree of
platform independence, but it will be some time before
they match Tk's in this respect.
22. PYTHON GUI: Pros
Extensibility
Many extensions of Tk exist, and more are being frequently
distributed on the Web.
Any extension is immediately accessible from Tkinter,
if not through an extension to Tkinter,
then at least through Tkinter's access to the Tcl language.
23. PYTHON GUI: Cons
Speed
Most calls to Tkinter are formatted as a Tcl command
(a string) and interpreted by Tcl from where the actual Tk
calls are made.
This theoretical slowdown caused by the successive
execution of two interpreted languages is rarely seen in
practice and most real-world applications spend little time
communicating between the various levels of Python, Tcl, and
Tk.
24. PYTHON GUI: Cons
Tcl
Python purists often balk at the need to install another
scripting language in order to perform GUI tasks.
There is periodic interest in removing the need for Tcl
by using Tk's C-language API directly, although no such
attempt has ever succeeded.
72. use the eval() function for evaluating arithmetic operations
involving string parameters containing numbers
>>print eval("3 + 4")
#prints the answer: 7
PYTHON GUI: Calculator
73. Python Mega Widgets (Pmw)
toolkit that provides high-level GUI components from the
combinations of Tkinter widgets
* Menubar, ScrolledText, etc.
PYTHON GUI: Pmw
74. Download Pmw and extract the contents of src folder to a folder
e.g. extract to: C:Pyhon27src
PYTHON GUI: Pmw
Run the setup.py executable file in Command Prompt
(install is a command-line parameter of setup.py ):
75. It means it could not find a python.exe file to run setup.py,
hence you must add the Path of python interpreter/executable file
to the System Path in the Environment variables.
For example add:
;C:Pyhon27
to the list of Path of your operating system’s Environment Variables.
PYTHON GUI: Pmw
76. Detailed Step for Adding Python to Environment Variables:
right-click Computer > Properties > Advanced System Settings
> Advanced > Environment Variables
double-click the Path in the list of System Variables
append the location of python.exe (usually located in C:Python27)
;C:Python27
Click OK to all window dialog options.
Then, be sure to restart the command prompt!!!
PYTHON GUI: Pmw
86. PYTHON GUI: Other Tk functionalities
1. File Dialog for file handling
2. Closing inner and main windows
3. Drawing on Canvas widget (lines, text, ovals, etc)
4. Sliding/Adjusting values using the Scale widget
5. Animating Shapes/Blinking Rectangle
105. PYTHON GUI: Sine Curve
# plot a function like y = A*sin(x) + C
from Tkinter import *
import math
class SineCurve(Frame):
def __init__(self):
...
SineCurve().mainloop()
119. REFERENCES
q Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).
q Grayson - Python and Tkinter Programming (2000).
q Disclaimer: Most of the images/information used here have no proper source citation, and I do not
claim ownership of these either. I don’t want to reinvent the wheel, and I just want to reuse and
reintegrate materials that I think are useful or cool, then present them in another light, form, or
perspective. Moreover, the images/information here are mainly used for illustration/educational
purposes only, in the spirit of openness of data, spreading light, and empowering people with
knowledge. J