Describe the purpose and function of a Graphical User Interface (GUI).
Identify and explain the role of different GUI elements (e.g., Frame, Label, Entry, Button) in a tkinter application.
Use the tkinter library to create basic GUI elements in Python.
Implement Frame, Label, Entry, and Button widgets in a tkinter application.
Utilize StringVar and DoubleVar to manage data input and retrieval in a GUI.
Explain the role of event handling in GUI applications (specifically, how buttons trigger functions).
Construct a GUI application that collects data from a user.
(Implied but could be added) Understand how GUI elements can be used in conjunction with data handling (e.g., serialisation, as suggested by the intro)
This lesson focuses on building a Graphical User Interface (GUI) to collect data, specifically employee information, and then save that data. It uses Python's tkinter library to create the GUI elements such as labels, entry fields, and buttons. The lesson will guide you through the process of setting up the GUI, explaining the purpose of different widgets and how they interact. You'll learn how to create entry fields for data input, labels to guide the user, and buttons to trigger actions like saving data to a list or a file. The lesson also touches on the use of StringVar and DoubleVar to manage data within the GUI, as well as how to link button actions to specific functions.
This lesson concentrates of building a GUI form that will collect payroll data from a user. The data collected will be the employee's name, their IRD number, the hours they worked and their hourly rate.
For this to take place controls will be added to the form. In Python parlance these controls are referred to as widgets. The widgets to be added are:
Two StringVar() classes and two doubleVar() classes for transferring data between the widgets and the Python code
Four labels to guide the user
Four entry fields to collect the data
Two buttons to save the data
Now let us start examining the code.
Importing libraries
Listing 2-1
import tkinter as tkfrom tkinter import *
This section of the code imports the resources required for our program. Line1 imports the module tkinter. This module contains all the required classes for building a GUI form.
Line 2 imports other modules from tkinter that are also requred for our form.
At line 8 an object of the class Tk() is created and a pointer to it is stored in root. This is to be the overall controller of our GUI element.
At line 6 an object of the class Frame is created. This is controlled by the Tk() object. Its width and height parameters specify the width and height of the form in pixels.
At line 7 the pack() method is used to position the Frame object.
Before we discuss lines 9 - 12 we need to digress a little and discuss some of Python's data types and how they relate to GUI forms.
Here is a list of the most common Python data types:
int: integer numbers
float: real numbers
str: text
bool: Boolean values (True or False)
list
tuple
dict
...and the list goes on.
In a Python form we are provided with widgets for entering data directly from the keyboard. These widgets are objects of the class Entry. This class has only one data type: text. Whether you type in letters or numbers, they are all stored as text. To complicate things further text data itself differently in Entry objects than in Python.
To solve this issue we have a number of classes that act as intermediaries between the Entry objects and Python. These classes are StringVar() and DoubleVar(). The former is used for text data and the latter for numbers. These classes are used to bridge the gap between the text data in the Entry objects and the Python data types.
At lines 9 - 12 we create four objects of the classes StringVar() and DoubleVar(). These objects are used to bridge the gap between the Entry objects and Python.
nameLabel=tk.Label(master=frame1,text='Full Name',width=15, anchor=E) nameLabel.place(x = 40, y = 10) irdLabel=tk.Label(master=frame1,text='IRD Number',width=15, anchor=E) irdLabel.place(x = 40, y = 45) hoursLabel=tk.Label(master=frame1,text='Hours Worked',width=15, anchor=E) hoursLabel.place(x = 40, y = 80) rateLabel=tk.Label(master=frame1,text='Hourly Rate',width=15, anchor=E) rateLabel.place(x = 40, y = 115)
In the above block we are creating four Label widgets. As you can see the creation of each Label widget takes 2 lines. The first line of the first widget is reproduced below.
nameLabel =: This is an assignment statement. It means that the variable nameLabel will hold a reference to the object created on the right side of the equals sign.
tk.Label(...): This part calls the constructor of the Label class from the tkinter module (which we've aliased as tk). The Label class is used to create a text label widget in a Tkinter GUI. The parentheses (...) contain the arguments that are passed to the Label constructor, which customize the label's appearance and behavior.
master=frame1: master is a keyword argument that specifies the parent widget of the nameLabel. frame1 is assumed to be a previously created Frame widget. This means that the nameLabel will be placed inside the frame1 frame. A frame is used to group related widgets together.
text='Full Name': text is a keyword argument that sets the text displayed in the nameLabel. In this case, the label will display the string "Full Name".
width=15: width is a keyword argument that specifies the width of the nameLabel. The width is measured in text units, which is roughly equivalent to the width of the character '0' (zero) in the label's font. Setting width=15 means the label will be approximately 15 characters wide. This is used to make sure the label is a consistent size.
anchor=tk.E: anchor is a keyword argument that controls the placement of the text within the nameLabel widget. tk.E is a constant from the tkinter module that represents the East direction. This means that the text "Full Name" will be anchored to the right side of the label's available space.
What we have stated above also applies to lines 19 - 25
txtName=tk.Entry(master=frame1,relief=RAISED, textvariable=varName,width=15,bg="#ffffff", bd=1) txtName.place(x = 180, y = 10) txtIRD=tk.Entry(master=frame1,relief=RAISED, textvariable=varIRD,width=15,bg="#ffffff", bd=1) txtIRD.place(x = 180, y = 45) txtHours=tk.Entry(master=frame1,relief=RAISED, textvariable=varHours,width=15,bg="#ffffff", bd=1) txtHours.place(x = 180, y = 80) txtRate=tk.Entry(master=frame1,relief=RAISED, textvariable=varRate,width=15,bg="#ffffff", bd=1) txtRate.place(x = 180, y = 115)
txtName =: This is an assignment statement. It assigns the object created by the tk.Entry() constructor to the variable txtName.
tk.Entry(...): calls the constructor of the Entry class from the tkinter module.
The Entry class creates a single-line text input field.
The parentheses (...) contain the arguments that customize the entry widget.
master=frame1: master specifies the parent widget of the txtName entry. It places the txtName entry inside the frame1 frame.
relief=RAISED: relief sets the 3D border style of the entry widget. RAISED creates a raised border effect, making the entry appear to protrude slightly from the background.
textvariable=varName: textvariable links the entry widget to a Tkinter variable, in this case, varName. varName is assumed to be a previously created StringVar object. This link is crucial because:
Changes in the varName variable will automatically update the text displayed in the entry.
Changes made by the user in the entry will automatically update the varName variable.
This creates a two-way connection.
width=15: width sets the width of the entry widget in character units (approximately the width of the '0' character). This limits the number of visible characters in the entry.
bg="#ffffff": bg sets the background color of the entry widget.
bd=1: bd sets the border width of the entry widget in pixels. 1 creates a 1-pixel-wide border around the entry.
What we have stated above also applies to lines 29 - 37
Buttons are widgets that we are all familiar with. We click on one of them and something happens: a new window pops up, data is saved to a file or the current application is terminated. Here we have two buttons: one to add our newly created objects to a list and a second one for serialising the contents of that list to a file.
At line 33 we create a button object. Like all the other widgets the button is placed in the frame1 frame. The text, 'Save to list' specifies what appears on the button. The width parameter uses character width to specify the width of the button. The command parameter specifies which of the two functions spanninng lines is to run when the button is clicked. The lambda keword specifies that the function will run only when the button is clicked. The attachment to lambda is the name of the function and the parameters that are to be passed to it. In this case the parameters will be the values of the employee name, IRD number, hours worked and hourly rate.These values are extracted from the StringVar and DoubleVar objects that are linked to the Entry objects.
Line 34 positions the buttons on the form, using pixels as measuring units.
The code at lines 36 and 37 is is explained in the same way.
The function saveToList() is the function that runs when the button labelled "Save to List" is clicked. We shall be using this function later when we expand our program to save data to a list. Here,however, we are using it print the values entered by the user as a demonstration that the form we have just built is working correctly. We pass it the values entered by the user for the employee's name, their IRD number, hours worked and hourly rate. The functions simply prints those values.
The function saveToFile() hardly needs and explanation.
At line 47 the heading on the window is set to "Payroll"
At line 48 we have the mainloop() function. This simply allows the form to stay on the screen until the user deliverately closes it. Without it each ine of code would be executed once and then stop.
The code spanning lines 1 - 48 is one single function named "createPayroll()". As a function it can't run unless it is called. This call is made at line 50.
The file is a lesson focused on creating Graphical User Interfaces (GUIs) in Python, specifically for data input. It's designed to teach the basics of building a GUI using the tkinter library.
Key Concepts and Skills Covered:
GUI Creation with tkinter:
The lesson guides the user through the process of creating a basic GUI window and adding elements to it.
Widgets:
It explains the use of various tkinter widgets, including:
Frame: To organize and group other widgets.
Label: To display text for the user.
Entry: To allow users to input text.
Button: To trigger actions when clicked.
Data Handling:
The lesson demonstrates how to use StringVar and DoubleVar to manage data associated with Entry widgets, bridging the gap between tkinter's text-based data and Python's data types.
Layout Management:
It shows how to use the place() method to position widgets within the GUI.
Event Handling:
The lesson introduces the concept of event handling by linking button clicks to functions that are executed when the button is pressed.
Application Context:
The example used in the lesson is building a simple payroll application, where the GUI is used to collect employee data (name, IRD number, hours worked, hourly rate).
NCEA Compatibility:
The file notes that the lesson is compatible with specific requirements for the NCEA (National Certificate of Educational Achievement) in New Zealand, specifically relating to GUI programming and data collection.
In essence, the file provides a hands-on introduction to building a basic GUI application in Python using tkinter, focusing on the fundamental elements needed for data input.