Python Programming For Engineers - Part 4: Graphical User Interfaces II
Python Programming For Engineers - Part 4: Graphical User Interfaces II
Python Programming
for Engineers - Part 4:
Graphical User Interfaces II
by
Abstract
Python is a widely used, free, open source, high-level, general purpose computer programming
language. Python drives some of the internet’s most popular websites such as Google, Youtube
and Instagram. Python can be used to perform complex mathematical calculations, handle big
data, build web apps and desktop applications, and manage databases.
This course is the fourth of a series on Python programming. This course presents techniques to
build graphical user interfaces (GUI) in Python. A GUI application or app is an interface that
enables a user to interact with a computer program or an electronic device, in certain designed
ways, through visual indications and graphical elements. This course presents the details of
Python tkinter widgets used to build Python GUI applications such as messageboxes, slider and
scrollbar widgets, as well as widgets for creating additional GUI windows and widgets for
organizing other widgets on sub panes of the main GUI window. This course also presents
specialized widgets from other Python and tkinter modules and function libraries such as
comboboxes and dialog widgets. This course is tailored to practicing engineers. Practical
examples from situations encountered by practicing engineers and scientists are used to illustrate
and demonstrate the concepts and methods learned in this course.
On completion of this course, participants will be capable of applying the methods and
techniques learned in a desktop application that can be used to manage large data sets and
automate complex, repetitive, and tedious engineering calculations and algorithms. Participants
will be able to identify professional situations in which programming will be of a strategic
advantage to them in their fields of specialty, and to their organizations. Programming continues
to be an increasingly relevant and advantageous skill for engineers competing in a global
marketplace in the computer age.
There are no required pre-requisites for this course. However, it will be helpful to understand the
fundamentals of the Python programming language in general, as presented in the earlier parts of
this course series.
TABLE OF CONTENTS
Abstract ........................................................................................................................................... ii
List of Figures ................................................................................................................................. v
List of Tables ................................................................................................................................. vi
1. INTRODUCTION ...................................................................................................................... 1
1.1 Python ................................................................................................................................... 1
1.2 Graphical User Interface (GUI) ............................................................................................ 2
1.3 Python GUIs.......................................................................................................................... 2
2. PYTHON TKINTER .................................................................................................................... 4
2.1 tkinter .................................................................................................................................... 4
2.2 tkinter Widgets ...................................................................................................................... 9
3. THE tkMESSAGEBOX WIDGET ........................................................................................... 11
3.1 tkMessageBox ..................................................................................................................... 11
3.2 FunctionName ..................................................................................................................... 11
3.3 Options ................................................................................................................................ 12
3.4 tkMessagBox Example ....................................................................................................... 12
4. THE SCALE WIDGET ............................................................................................................ 24
4.1 Scale .................................................................................................................................... 24
4.2 Scale widget methods ......................................................................................................... 26
4.3 Scale Example ..................................................................................................................... 26
5. THE SCROLLBAR WIDGET ................................................................................................. 31
5.1 Scrollbar .............................................................................................................................. 31
5.2 Scrollbar widget methods ................................................................................................... 32
5.3 Scrollbar Example ............................................................................................................... 33
6. THE TOPLEVEL WIDGET ..................................................................................................... 36
6.1 Toplevel .............................................................................................................................. 36
6.2 Toplevel widget methods .................................................................................................... 36
6.3 Toplevel Example ............................................................................................................... 38
List of Figures
List of Tables
1. INTRODUCTION
1.1 Python
Python can be used to perform complex mathematical and engineering calculations, and to
handle big data. Python can be used for building GUIs and desktop applications. Python can be
used on a server for web development and to build web apps. Python can be used to connect to
database systems and can read and modify files. Since Python runs on an interpreter system, the
code is executed rapidly which enables quick prototyping or production-ready software
development.
As a high-level language, Python has a simpler syntax similar to the English language. The
syntax of Python enables code to be written with fewer lines than some other programming
languages. Python is increasingly popular as a first programming language for beginners.
As a result of its user-friendliness and versatility, Python is the programming language driving
some of the internet’s most popular websites, namely:
• Google
• Youtube
• Quora
• Dropbox
• Yahoo!
• Yahoo Maps
• Reddit
• Bitly
• Instagram
• Spotify
• SurveyMonkey
• Pintrest
• Eventbrite
• Firefox
• and many others
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 1 of 116
365.pdf
A graphical user interface or GUI (pronounced goo-ee) is an interface that enables a user to
interact with a computer program or an electronic device through visual indications and
graphical elements (also called objects or controls) such as a click button, checkbox, textbox,
drop-down menu, image, scrollbar, animation etc., etc.
Prior to the invention of GUIs, interaction with a computer was by text-based commands
whereby a user would type instructions into a command line.
A GUI provides a computer environment that is simple and easy to use, thus enabling
significantly higher productivity and accessibility even for an untrained user. A well-designed
GUI will enable a non-expert user to navigate through the system with ease, and the user does
not have to know or memorize any special codes or commands whatsoever. All user interaction
with the GUI is through a human interface device such as a keyboard, mouse, touchscreen etc.
Among the many attractive features of Python are the options to develop GUIs. It can be argued
that without the capability to build GUIs, Python may never have reached the level of popularity
it has attained to date, and we may have never heard of YouTube, Instagram and other popular
sites and applications driven by Python.
Python GUIs are built from modules (or function libraries) that ship with Python or may be
downloaded for free. Some of the more popular packages include:
tkinter : This is an interface to the tk GUI toolkit that ships with Python.
wxPython : This is an open-source interface for wxWindows.
JPython : This is a port for Java which gives scripts written in Python seamless access to the
Java GUI capabilities on your local machine.
In this course series, all Python GUIs shall be developed using tkinter.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 2 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 3 of 116
365.pdf
2. PYTHON TKINTER
2.1 tkinter
tkinter (pronounced tee-kay-inter) is a built-in module that contains functions and methods for
creating Python GUIs. The name tkinter derives from “tk interface”, the interface to the tk GUI
tools.
The general steps to create a Python GUI using tkinter are as follows:
(Note: Throughout this course, it cannot be over-emphasized that when typing or replicating the
Python codes please remember to pay attention to spacing, alignment, indentations, margins etc.
Remember that Python commands are case-sensitive. When modifying existing scripts, please
pay particular attention to where exactly within the script the new codes and commands are
being inserted and follow suit accordingly.)
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 4 of 116
365.pdf
(Note: For Python 2 users, the call is Tkinter, whereas for Python 3 and above users the call is
tkinter.)
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 5 of 116
365.pdf
Look around your monitor display and locate the GUI window.
(You may have to minimize some other open applications or drag them out of the way to see the
Tk window).
Note that without the mainloop( ) method, the window would show but then disappear. The
mainloop( ) method “reopens” the window continuously, obviously at speeds faster than the
human eye can perceive, and this continues infinitely or until the user clicks on the “X” on the
window to terminate the mainloop( ).
We shall re-write the code incorporating popular naming conventions and common strategies to
streamline the code. We shall also modify some features or attributes of the GUI window.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 6 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 7 of 116
365.pdf
Later in this series, we shall look at other ways that attributes can be added or modified.
Obviously, the next question is how to add controls – buttons, text, textboxes, checkboxes,
scrollbars etc., etc., to the root window.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 8 of 116
365.pdf
Widget Description
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 9 of 116
365.pdf
Each widget has properties (or attributes) called options that can be manipulated. Some of the
commonly manipulated options include the
• color
• font
• dimensions
• relief
• anchors
• bitmaps
• cursors
• and many others
• tkMessageBox
• Scale
• Scrollbar
• Toplevel
• PanedWindow
The other tkinter widgets are presented in a previous part of this course series.
Other widgets from other Python and tkinter modules and function libraries discussed in this
course are:
• Combobox
• simpledialog
• tkFileDialog
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 10 of 116
365.pdf
3.1 tkMessageBox
The tkMessageBox is used to display a message box (or pop up box) to the user.
tkMessageBox.FunctionName ( < title > , < massage > [ , < options > ] )
where
FunctionName is the specific type of message box
< title > is the title caption displayed in the header bar of the widget
< message > is the main text message displayed on the message box
< options > optional attributes that are set to customize the message box
As with any kind of pop up box, a tkMessageBox is typically called via a function in the main
program if the user interacts with the program in a certain way.
3.2 FunctionName
This is the type of the message box. When the message box opens, an icon of the relevant type is
displayed on the message box. The available options are summarized in Table 3.1
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 11 of 116
365.pdf
FunctionName Description
3.3 Options
The options fall under default and parent. The default option is used to specify the default
button of the message box such as RETRY, IGNORE, ABORT, etc. The parent option is used to
specify the main GUI window on top of which the message box is to be displayed.
In this exercise we shall update the company account log-in developed in Chapter 7 of part 3 of
this course series. We shall add some of the tkMessageBoxes to the app.
Open your company account log-in file and conduct the following updates.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 12 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 13 of 116
365.pdf
Scroll up to the function called login( ) which the Checkbutton command option is set to.
Conduct the following updates.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 14 of 116
365.pdf
Now, indent your entire current code for the login( ) function.
Wrap your indented current code for the login( ) function inside the following if statement.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 15 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 16 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 17 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 18 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 19 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 20 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 21 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 22 of 116
365.pdf
Exercise complete!
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 23 of 116
365.pdf
4.1 Scale
The Scale (or Slider) widget gives a graphical slider object that enables a user to select a value
from a range.
< variable > = Scale ( < master > , < option > = < value> , < option> = < value> , … )
where
< variable > is a variable name that the widget is assigned to
< master > is the name of the variable the main GUI window has been assigned to, or the full
reference to the main window
< option > is an attribute
< value > is the specific value of the attribute
In addition to the applicable widget options presented in Chapter 3 of part 3 of this course series,
other Scale widget options are summarized in Table 4.1 below.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 24 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 25 of 116
365.pdf
Method Description
In this exercise we shall develop a simple industrial temperature monitor. The user will use the
slider to monitor the temperature of the system, and then press a Button to display the value an
Entry widget.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 26 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 27 of 116
365.pdf
At the very top of the script, add the following code that defines the function for the Button.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 28 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 29 of 116
365.pdf
Successful completion!
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 30 of 116
365.pdf
5.1 Scrollbar
The Scrollbar widget is used to give the capability of vertical sliding (or scrolling) to other
widgets - Text, Canvas, and Listbox.
< variable > = Scrollbar ( < master > , < option > = < value> , < option> = < value> , … )
where
< variable > is a variable name that the widget is assigned to
< master > is the name of the variable the main GUI window has been assigned to, or the full
reference to the main window
< option > is an attribute
< value > is the specific value of the attribute
In addition to the applicable widget options presented in Chapter 3 of part 3 of this course series,
other Scrollbar widget options are summarized in Table 5.1 below.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 31 of 116
365.pdf
jump controls what happens when the slider is default of 0 causes the
dragged command callback to be
called,
with value of 1 the callback
is called after the user
releases the mouse
orient specifies the orientation of the scrollbar HORIZONTAL,
(but not the scrolling) VERTICAL
repeatdelay specifies how long (in milliseconds) the default = 300
button must be held down before the slider
moves repeatedly in that direction
Method Description
get ( <options> ) returns (p, q) where p is the current position of the left
or top edge of the slider for a horizontal and vertical
scrollbar respectively, q is the right or bottom edge of
the slider
set ( <first> , <last> ) to connect the scrollbar to some other widget, set that
widget’s xscrollcommand or yscrollcommand to the
scrollbar’s set( ) method
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 32 of 116
365.pdf
In this example we shall modify application we built in Chapter 9 of part 3 of this course series.
Specifically, we shall modify the Listbox widget and add a vertical scrollbar to it.
Open your code file for the application we developed in Chapter 9 of part 3 of this course series.
Conduct the following updates.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 33 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 34 of 116
365.pdf
Successful completion!
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 35 of 116
365.pdf
6.1 Toplevel
A Toplevel widget is used as a container widget in a similar fashion as using a Frame, however
the display opens in a separate top-level window. Thus, the Toplevel widget is typically used to
display additional application windows, dialogs, and other pop ups.
< variable > = Toplevel (< option > = < value> , < option> = < value> , … )
where
< variable > is a variable name that the widget is assigned to
< option > is an attribute
< value > is the specific value of the attribute
Method Description
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 36 of 116
365.pdf
Method Description
iconify ( ) turns the window into an icon but does not destroy it,
the window is reopened by using the deiconfy method
maxsize(<width>, <height>) sets the maximum size of the window
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 37 of 116
365.pdf
A Toplevel window may be created anywhere in the code of the root. However, in most cases a
Toplevel window opens after the user interacts with some widget or the other on the root
window. Therefore, typically, a Toplevel window is implemented through the function assigned
to the command option of the relevant widget.
In this exercise we shall upgrade the project management portal we developed in Chapter 10 of
part 3 of this course series that uses the Spinbox. Specifically, we shall add Toplevel windows
that open based on what access level is selected by the user.
Open your code file for your project management portal with Spinbutton. Within the login( )
function conduct the following updates.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 38 of 116
365.pdf
Continue as follows.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 39 of 116
365.pdf
Now, we scroll to the top of our script to add code for the window functions.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 40 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 41 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 42 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 43 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 44 of 116
365.pdf
Successful completion!
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 45 of 116
365.pdf
7.1 PanedWindow
A PanedWindow is a container widget that may have any number of panes. The panes are
“child” widgets of the main PanedWindow and may be arranged horizontally or vertically. Each
pair of panes is separated by a moveable separator (or sash) that enables a user to resize a pane as
desired.
< var > = PanedWindow ( < option > = <value> , <option> = < value> , … )
where
< var > is a variable name that the widget is assigned to
< option > is an attribute
< value > is the specific value of the attribute
where
< child n > is a variable name the nth child pane is assigned to
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 46 of 116
365.pdf
In addition to the applicable widget options presented in Chapter 3 of part 3 of this course series,
other PanedWindow widget options are summarized in Table 7.1 below.
handlepad default of 8
handlesize default of 8
sashcursor
sashpad
sashwidth default is 2
showhandle
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 47 of 116
365.pdf
Method Description
sash_dragto(<index>, <x>, <y>) drags the sash to a new position, relative to the mark
In this example we shall create a new version of the industrial temperature tool we developed in
Chapter 4 that incorporates PanedWindows.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 48 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 49 of 116
365.pdf
Continue as follows.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 50 of 116
365.pdf
At the top of the script add the code for the temp_fn function that the Button command option is
set to.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 51 of 116
365.pdf
child window
sash
child window
sash
child window
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 52 of 116
365.pdf
Successful completion!
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 53 of 116
365.pdf
8.1 Combobox
A Combobox is a combination of an Entry widget and drop-down menu. When the user is not
interacting with the widget, only one item, the current Entry content, is visible. When the user
clicks on the drop-down arrow button, the other menu items become visible and the user may
make a selection. A selection is made by clicking on the item. The item then replaces the existing
item in the Entry and the menu closes.
The tkinter Combobox widget is in the ttk module of tkinter. The ttk module must be imported in
order to use the widget. The syntax to create a Combobox is of the form,
< variable > = Combobox ( < master>, values = [<item>, <item>, <item>,…] {, < options >})
where
< variable > is a variable name that the widget is assigned to
< master > is the name of the variable the main GUI window has been assigned to, or the full
reference to the main window
values is an option (a variable) set to a Python list - a comma-separated list of items enclosed in
straight brackets ( [ ] ), these are the items that appear in the drop-down list
< options > are optional, attributes of the widget
The general widget options as well as those specific to the Entry widget, the Listbox widget, and
the Menu are generally applicable to the Combobox widget. Some of the commonly applied
Combobox widget options are summarized in Table 8.1.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 54 of 116
365.pdf
Some of the commonly used Combobox methods are summarized in Table 8.2.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 55 of 116
365.pdf
Method Description
Open your code file that you worked on in Chapter 6 (Toplevels), and conduct the following
updates.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 56 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 57 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 58 of 116
365.pdf
And now update the Button command option login( ) function’s if statement.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 59 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 60 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 61 of 116
365.pdf
In the following example we shall have a pop up appear once an item is selected from the
Combobox list.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 62 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 63 of 116
365.pdf
At the top of your code window add the code to define the callback function, as follows.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 64 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 65 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 66 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 67 of 116
365.pdf
Successful completion!
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 68 of 116
365.pdf
9.1 simpledialog
A simpledialog widget is a pop up that enables an executing program to interact with the user. A
simpledialog widget consists of a text field (entry widget) and buttons such as on OK button and
a Cancel button. When the simpledialog widget pops up, the program execution pauses, and the
user may supply some input data to the program through the simpledialog widget’s text field.
Upon supplying the data and clicking the OK button, the data is passed to the simpledialog
widget’s control variable which is then processed by the program. If the Cancel button is clicked
on the data input process is aborted, the widget’s control variable does not change its value and
no new data is supplied to the program.
The tkinter simpledialog widget is in the simpledialog module of tkinter. The simpledialog
module must be imported in order to use the widget. The syntax for the creation of a
simpledialog is of the form,
< var > = simpledialog.FunctionName ( < title > , < prompt > [ , < options > ] )
where
FunctionName is the specific type of simpledialog widget
< title > is the title caption displayed in the header bar of the widget
< prompt > is the main text message displayed on the dialog box
< options > optional attributes that are set to customize the dialog box
As with any kind of pop up box, a simpledialog is typically called via a function in the main
program if the user interacts with the program in a certain way.
9.2 FunctionName
This is the type of the simpledialog. When the simpledialog opens, an icon of the relevant type is
displayed on the widget. The available options are summarized in Table 9.1
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 69 of 116
365.pdf
FunctionName Description
In this example we shall use simpledialogs to implement a login process where a username and
password must be correctly entered to gain access to an account.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 70 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 71 of 116
365.pdf
At the top of your code window replicate the following code for the signin( ) function.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 72 of 116
365.pdf
At the top of your code window replicate the following code for the open_win( ) function.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 73 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 74 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 75 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 76 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 77 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 78 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 79 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 80 of 116
365.pdf
Let us add a rule that the user has three (3) to correctly enter the sign in formation, if not, the
system shall shut down.
Replicate the following code to implement the rule.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 81 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 82 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 83 of 116
365.pdf
Successful completion!
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 84 of 116
365.pdf
10.1 tkFileDialog
The tkFileDialog module provides functions that enable the user to open the computer’s
operating system’s file management application and browse or navigate through the folders to
select a file to open or to save into a new file (Save As).
The syntax for the tkFileDialog function that enables a user to get an existing file name to open
the file is of the form
where
< options > optional attributes that are set to customize the dialog box
The syntax for the tkFileDialog function that enables a user to get a new file name to save-as is
of the form
where
< options > optional attributes that are set to customize the dialog box
It must be noted that the above tkFileDialog commands do not actually save or load the file.
They simply enable the user to select or enter a file name. Once a file name is selected or
entered, the file may be opened, closed, saved, etc., using the relevant file handling command(s).
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 85 of 116
365.pdf
In this exercise we shall create an app through which a user can enter a project report. The report
can then be saved as a file of several filetypes to any folder the user chooses, using a
tkFileDialog. The app will also have functionality to create copies of the report that can be edited
and augmented while the original report remains intact. Finally, the app will enable the user to
browse through folders and select a file and delete it.
In order to implement the functionality, we shall use a Text widget for typing in the report. Three
(3) Button widgets shall be added. One Button will be used for creating the report file from the
Text widget data. One Button will be used to make copies of the report file. The third Button will
provide the functionality to browse folders and select a file for deletion.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 86 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 87 of 116
365.pdf
Replicate the following code to define the writefunc( ) function for the WRITE Button.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 88 of 116
365.pdf
Replicate the following code to define the copyfunc( ) function for the COPY Button.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 89 of 116
365.pdf
Replicate the following code to define the deletefunc( ) function for the DELETE Button.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 90 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 91 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 92 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 93 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 94 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 95 of 116
365.pdf
Use your computer’s file explorer to navigate to the folder you saved your report to and confirm
the file has been created.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 96 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 97 of 116
365.pdf
Click on COPY.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 98 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 99 of 116
365.pdf
Click on Save.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 100 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 101 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 102 of 116
365.pdf
Click on DELETE.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 103 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 104 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 105 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 106 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 107 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 108 of 116
365.pdf
Update your deletefunc( ) function code as shown below to implement an error handler for the
scenario that the user abruptly cancels out of the file deletion process.
Note the use of the indenting to implement the error hander. Essentially, the existing code is
“wrapped” in a try … except structure.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 109 of 116
365.pdf
The tkMessageBox added under the except clause shall appear to the user and the built-in
exception raised by the Python interpreter will be suppressed from the user’s view.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 110 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 111 of 116
365.pdf
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 112 of 116
365.pdf
11.1 Exceptions
Domain knowledge and experience must be used to design and implement appropriate error
handlers. The programmer should test multiple scenarios that a non-expert end-user may
encounter and design the error handlers to guide and assist the end-user accordingly while
precluding to the maximum extent possible, built-in exceptions being raised to the end-user.
It cannot be overemphasized that all Python scripts should be meticulously reviewed, thoroughly
scrutinized, and frequently tested as they are being developed. It is the programmer’s
responsibility to frequently test the code and address problems as they arise, and to verify or
otherwise that the scripts execute as intended (validation). Test your codes and scripts frequently,
block by block, line by line, using the IDLE (Python GUI) or the File Editor or any other
preferred Python tool. A piecemeal approach to writing and testing code is strongly preferred
rather than writing the entire script before testing it. In the latter scenario, it will be significantly
more difficult to identify and isolate the relevant problems.
There is currently an abundance of help information on Python and tkinter programming on the
World Wide Web. These include official (peer-reviewed) and unofficial sources, websites,
academic reports, professional presentations, tutorial videos (YouTube, etc.), user groups, online
forums, downloadable code snippets, etc., etc. Typing any Python or tkinter topic in a search
engine will typically yield tens if not hundreds of results. It is still strongly recommended,
regardless of the source of any contributory or relevant help information, that all codes being
developed be tested and validated thoroughly before deployment.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 113 of 116
365.pdf
12. CONCLUSION
Python is an interpreted, high-level, general purpose programming language. Python is a free and
open source and can be used to build a wide range of desktop and web-based applications. This
course has presented an overview of the Python tkinter libraries for developing graphical user
interfaces (GUI). This course presented fundamental concepts, principles, and programming
structures of the Python tkinter GUI programming.
In this course the following Python tkinter widgets were presented in detail: tkMessageBox,
Scale, Scrollbar, Toplevel, and PanedWindow. Other widgets from other Python and tkinter
modules and function libraries that were discussed in this course are: Combobox, simpledialog
and tkFileDialog. Practical examples from situations encountered by a practicing engineer or
scientist were used to illustrate and demonstrate the concepts and methods learned in this class.
This course has prepared participants to now develop their own applications driven by Python.
This course has enabled participants to identify situations where computer programming is
relevant and will be of advantage to the practicing professional competing in the global
marketplace.
Practitioners are strongly encouraged to look for situations in their domains of expertise where
computer programming solutions are applicable and will be of benefit to their work and their
organizations.
All programming requires a careful and meticulous approach and can only be mastered and
retained by practice and repetition.
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 114 of 116
365.pdf
REFERENCES
Python Software Foundation. (2019). Python Software Foundation. Retrieved July 2019, from
Python Software Foundation: http://www.python.org/psf/
Python Tutorial: A Tutorial. (2019). Retrieved September 2019, from Tutorials, Python Courses:
Online and On Site: https://www.python-course.eu/python_tkinter.php
tutorialspoint. (2019). Python - GUI Programming (Tkinter). Retrieved August 2019, from
tutorialspoint.com: https://www.tutorialspoint.com/python/python_gui_programming
www.SunCam.com Copyright 2019 Kwabena Ofosu, Ph.D., P.E., PTOE Page 115 of 116