Learning To Program in 150 Challenges
Learning To Program in 150 Challenges
Tkinter GUI
Graphical User Interface
Explanation
A GUI (graphical user interface) makes the program easier to use. It allows you, as the
programmer, to create screens, text boxes and buttons to help the user navigate through
the program in a more user-friendly way. Tkinter is a library of features in Python that
allows you to do this.
Look at the code below and in particular the measurements that are used in the
window.geometry and button.place lines.
The geometry line in the code determines the size of the window and the place line in
the code determines the position of the individual item on the window.
Challenges 124 - 132: Tkinter GUI 111
Once the button is pressed it will run the “Call” subprogram and change the window to look
as follows:
112 Challenges 124 - 132: Tkinter GUI
Example Code
from tkinter import *
This line must go at the beginning of the program to import the Tkinter library.
window = Tk()
window.title(“Window Title”)
window.geometry(“450x100”)
Creates a window that will act as the
display, referred to as “window”, adds a title
and defines the size of the window.
output_box = Message(text = 0)
Creates a message box which is used to display an
output.
answer = output_txt[“text”]
Obtains the contents of a message box and
stores it in a variable called answer. This does
not work with an entry box.
output_txt[“text”] = total
Changes the content of a message box to
display the value of the variable total.
window.mainloop()
This must be at the end of the program to make
sure it keeps working.
Challenges
124 125 126
Create a window that will Write a program that Create a program that will ask
ask the user to enter their can be used instead the user to enter a number in a
name. When they click on of rolling a six-sided box. When they click on a
a button it should display die in a board game. button it will add that number
the message “Hello” and When the user clicks to a total and display it in
their name and change a button it should another box. This can be
the background colour display a random repeated as many times as
and font colour of the whole number they want and keep adding to
message box. between 1 to 6 the total. There should be
(inclusive). another button that resets the
total back to 0 and empties the
131 132
Create a program that will allow the Using the .csv file you created for the last
user to create a new .csv file. It should challenge, create a program that will allow
ask them to enter the name and age of people to add names and ages to the list
a person and then allow them to add and create a button that will display the
this to the end of the file they have contents of the .csv file by importing it to a
just created. list box.
Challenges 124 - 132: Tkinter GUI 115
Answers
124
116 Challenges 124 - 132: Tkinter GUI
125
Challenges 124 - 132: Tkinter GUI 117
126
118 Challenges 124 - 132: Tkinter GUI
127
Challenges 124 - 132: Tkinter GUI 119
128
120 Challenges 124 - 132: Tkinter GUI
129
Challenges 124 - 132: Tkinter GUI 121
130
122 Challenges 124 - 132: Tkinter GUI
131
Challenges 124 - 132: Tkinter GUI 123
132