0% found this document useful (0 votes)
346 views

Learning To Program in 150 Challenges

This document provides explanations and examples of how to create graphical user interfaces (GUIs) using Tkinter in Python. It discusses Tkinter features like windows, labels, buttons, and entry boxes. It provides example code to create a basic GUI and explanations of code lines. It then lists 13 challenges for readers to create Tkinter programs that prompt users for input, perform calculations, and save/load data to CSV files.

Uploaded by

Matt Hunt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
346 views

Learning To Program in 150 Challenges

This document provides explanations and examples of how to create graphical user interfaces (GUIs) using Tkinter in Python. It discusses Tkinter features like windows, labels, buttons, and entry boxes. It provides example code to create a basic GUI and explanations of code lines. It then lists 13 challenges for readers to create Tkinter programs that prompt users for input, perform calculations, and save/load data to CSV files.

Uploaded by

Matt Hunt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

110 Challenges 124 - 132: Tkinter GUI

Challenges 124 - 132

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.

Now look at the window that this code will produce:

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.

label = Label(text = “Enter number:”)


Adds text to the screen displaying the message
shown.

entry_box = Entry (text = 0)


Creates a blank entry box. Entry boxes can be used by
the user to input data or used to display output.

output_box = Message(text = 0)
Creates a message box which is used to display an
output.

output_box [“bg”] = “red” output_box [“fg”] = “white”


Specifies the background colour of the Specifies the font colour of the object.
object.

output_box [“relief”] = “sunken” list_box = Listbox()


Specifies the style of the box. This can be flat, Creates a drop-down list box
raised, sunken, grooved and ridged. which can only contain strings.

entry_box [“justify”] = “center”


Specifies the justification of the text in an entry box,
but this does not work for message boxes.

button1 = Button(text = “Click here”, command = click)


Creates a button that will run the subprogram “click”.
Challenges 124 - 132: Tkinter GUI 113

label.place(x = 50, y = 20, width = 100, height = 25)


Specifies the position in which the object will appear in the window. If the position is
not specified the item will not appear in the window.

entry_box.delete(0, END) num = entry_box.get()


Deletes the contents of an entry or Saves the contents of an entry box and stores it
list box. in a variable called num. This does not work
with message boxes.

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.

Every challenge you


complete helps you
become a better
programmer.
114 Challenges 124 - 132: Tkinter GUI

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

127 original text box, ready for


them to start again.
Create a window that will ask the user to enter a
name in a text box. When they click on a button it
will add it to the end of the list that is displayed on 128
the screen. Create another button which will clear
1 kilometre = 0.6214 miles and 1
the list.
mile = 1.6093 kilometres. Using
these figures, make a program
129 that will allow the user to
Create a window that will ask the convert between miles and
user to enter a number in a text box. kilometres.
When they click on a button it will
use the code 130
variable.isdigit() to check Alter program 129 to add a third button that
to see if it is a whole number. If it is will save the list to a .csv file. The code
a whole number, add it to a list box, tmp_list = num_list.get(0,END)
otherwise clear the entry box. Add can be used to save the contents of a list box
another button that will clear the as a tuple called tmp_list.
list.

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

You might also like