12 Team Activity: Using Objects
Instructions
Work as a team as explained in the instructions for the lesson 2 team activity.
Problem Statement
Almost all of the programs that you wrote for this course receive input from and print results to a terminal window. However, most users prefer to interact with a program through a graphical user interface (GUI) that contains icons, text fields, drop-down lists, buttons, etc. Within a GUI, the individual components (icons, text fields, etc.) are called widgets. Most libraries for creating GUIs use object oriented programming because each widget is an object with attributes and methods.
Assignment
As a team, write a Python program named gui.py
that
gets user input from a GUI, performs a simple calculation, and
displays the result in a GUI.
Helpful Documentation
- Official documentation for the tkinter module
Steps
Do the following:
- Download these two Python files: heart_rate.py and number_entry.py and save them in the same folder where you will save your Python program.
- Open the
heart_rate.py
file in VS Code and run it. Notice how running the program opens a GUI where a user can enter his age and see heart rates for exercising. Experiment with the GUI by entering different ages, seeing the results, and clicking the "Clear" button. - Read the comments and examine the code in the
heart_rate.py
program. Notice the following:- To create its GUI, the heart_rate program uses the
tkinter
module which is imported at line 3. - The
main
function begins at line 7. - The
main
function creates atk.TK
root object and uses that object to create the main window for the program. - Beginning at line 49, the
populate_main_window
function creates labels, text entry boxes, and buttons and places them in a grid. - Inside the
populate_main_window
function, there are two nested functions namedcalculate
andclear
. Thecalculate
function is called each time the user enters a digit in the age text field. Theclear
function is called each time the user clicks the "Clear" button.
- Choose a simple calculation or formula for your program to calculate. You could choose one of these:
- Area of a circle:a = π r2
- Swing time of a pendulum:t = 2π√n9.81
- Area of a rectangle:a = wh
- Volume of a tire:v =π w2 aw a + 2,540 d10,000,000,000
- Use the
heart_rate.py
program as a reference (you could even copy and paste parts of it or all of it) and write a Python program with a GUI that calculates the formula that you chose.
Core Requirements
- Your program must include a GUI that opens when you run your program.
- The GUI must allow a user to enter input.
- When the user enters valid input, your program must compute correct results and display those results in the GUI.
Stretch Challenges
If your team finishes the core requirements in less than an hour, complete one or more of these stretch challenges. Note that the stretch challenges are optional.
- Add a "Clear" button to your GUI that clears all inputs and outputs when the user clicks it.
- Add a label that acts as a status bar at the bottom of your GUI. Your program should display an error message in the status bar when the user enters invalid input. Your program should clear the status bar when the user enters valid input.
Testing Procedure
Run your program and enter various inputs, including invalid values. Verify that your program doesn’t crash, behaves as expected, and displays correct results.
Ponder
After you finish your program, examine the code in the
populate_main_window
function and realize that you
wrote object oriented code when you used objects such as
lbl_age, lbl_width, txt_age, and
txt_width and the dot operator to call methods such as
grid
, config
, get
, and
clear
.
Sample Solution
Please work diligently with your team for the one hour meeting. After the meeting is over, please compare your solution to the sample solution. Please do not look at the sample solution until you have either finished the program or diligently worked for at least one hour. At the end of the hour, if you are still struggling to complete the assignment, you may use the sample solution to help you finish.
Submission
When you have finished the activity, please report your progress via the associated I‑Learn quiz. When asked about which of the requirements you completed, feel free to include any work done during the team meeting or after the meeting, including work done with the help of the sample solution, if necessary. In short, report on what you were able to accomplish, regardless of when you completed it or if you needed help from the sample solution.