A3 Worksheet - Joke Machine
A3 Worksheet - Joke Machine
Lesson 25 - GUIs
Joke machine
Introduction
For this mini project, you will create an app that allows the user to select a joke
type, and click the button to reveal the joke. It will look similar to the screen
below (note that this may look different for Mac users):
You will see that the file contains the solution to the previous activity. This
means you can modify and add to working code rather than starting completely
from scratch.
Step 1
Now make sure that you have included the line of code that creates the app and
the final line of code that displays the app.
Tip: Look at your last project if you are unsure of what to do.
Step 1
Take a look at the layout for the app that you are going to build. Note the
required widgets and the order in which they appear.
RadioButton
Label
Button
Step 2
Make sure that each widget has its own unique identifier.
Identifier Widget
instruction Label
joke_choice StringVar
stick_radio RadioButton
fluff_radio RadioButton
chicken_radio RadioButton
frog_radio RadioButton
joke_button Button
display_joke Label
Step 3
StringVar
identifier = tk.StringVar(value="Stick")
Here, we create a new special variable of type StringVar. This variable will store
the value of the selected joke. We set its initial value to "Stick", which is one of
the options available to the user. The identifier should be replaced with your
unique identifier as per the table above.
Step 4
The new widget that you need to use is the RadioButton widget. Here is a code
snippet:
RadioButton
identifier = tk.Radiobutton(app, text="Stick", variable=joke_choice, value="Stick")
identifier.pack(anchor=tk.W)
Here, we are creating a radio button, which is a type of widgets that presents
multiple options ot the user, but only one option can be selected at a time. We
are also using the pack() method as we did in the previous task to place the
radio button within the application window. Remember to use the pack() method
after each one of your widgets.
Step 1
Make sure that your Button calls the jokes subroutine when it is pressed.
Tip: Look at your previous app to remember how to properly set up the Button.
Step 2
The jokes subroutine needs to be defined at the beginning of the code. You
should place this underneath the import statement.
To access the selected option from the RadioButtons you created, you need to
use:
identifier.config
Below is a code snippet to get you started with the jokes subroutine.
def jokes():
joke = joke_choice.get()
if joke == "Stick":
display_joke.config(text="What is brown and sticky? A stick!")
Complete the jokes subroutine so that it covers all the jokes that a user can
select using the radio buttons.
Joke 3: Why did the chicken cross the road? To buy some toilet paper!
Joke 4: What happens to a frog's car when it breaks down? It gets toad away!
Step 3
Now test your program. At this point it should be fully functional and should look
like the screens below:
Screen on loading
Explorer task .
● Separate the jokes and the punchlines. The user should be able to click
one button that reveals the start of the joke, and press a second button
that reveals the punchline.
● Visit the Tkinter documentation page and look at the code snippets that
you can incorporate into your own programs.
● Look at a previous project that you have created and see if you can make
it work as a GUI.
Resources are updated regularly — the latest version is available at: ncce.io/tcc.
This resource is licensed under the Open Government Licence, version 3. For more information on
this licence, see ncce.io/ogl.