0% found this document useful (0 votes)
7 views6 pages

A3 Worksheet - Joke Machine

Uploaded by

austinnguyen0712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

A3 Worksheet - Joke Machine

Uploaded by

austinnguyen0712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

KS4 - Programming Activity sheet

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):

Set up your app .

Access the starter file on Trinket here (ncce.io/ks4-joke-starter).

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.

Build the app .

Page 1 Last updated: 31-05-2024


KS4 - Programming Activity sheet
Lesson 25 - GUIs

This program uses the following widgets:

App, RadioButton, Label, Button

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.

Here is a table to help you with the identifiers of each widget.

Identifier Widget

instruction Label

joke_choice StringVar

stick_radio RadioButton

fluff_radio RadioButton

chicken_radio RadioButton

frog_radio RadioButton

Page 2 Last updated: 21-05-21


KS4 - Programming Activity sheet
Lesson 25 - GUIs

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.

Create the jokes subroutine .

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.

Page 3 Last updated: 21-05-21


KS4 - Programming Activity sheet
Lesson 25 - GUIs

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 2: What is pink and fluffy? Pink fluff!

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

Page 4 Last updated: 21-05-21


KS4 - Programming Activity sheet
Lesson 25 - GUIs

Screen after pressing Show Joke

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.

Further explorer tasks .

Page 5 Last updated: 21-05-21


KS4 - Programming Activity sheet
Lesson 25 - GUIs

● 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.

Page 6 Last updated: 21-05-21

You might also like