Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Мodule 4 Lesson 1 Python GUI

Tugas 1. Radiobuttons
Radiobutton is one of the possible group selection widgets in Tkinter.
Like any radio button, they are not created individually, but within one group,
so that only one answer choice can be made. If one button in a group is pressed,
the others are disabled.
Creating such buttons is very simple:

r1 = Radiobutton(text='The first button')


r2 = Radiobutton(text='The second button')

r1.pack()
r2.pack()

However, if you run this code, you will see that both buttons are pressed:

That’s because there’s no connection between them.


We'll find out how to make that connection in the next exercise.
Tugas 2. Variables in TKinter
In elements such as Radiobutton, the connection is established through a common
variable, the value of which corresponds to the value of the selected button.
First, you need to create this shared variable, and then pass this variable to all
buttons of the same group in the variable property.
However, it's not that simple. In Tkinter, you cannot use any variable to store
widget states. There are special TKinter-variables for this purpose, and their basic
types are:
BooleanVar Logic variable (True or FALSE)

IntVar Integer variable

DoubleVar Real variable

StringVar String variable

In addition, each radio button must have its value, which is written to a common
variable when that button is selected. This works the same way as with Entry, only
in this case the value of the field is not entered from the keyboard, but is
predefined.

In the end, the correct Radiobutton group will look like this:

r_var = BooleanVar()
r_var.set(0)
r1 = Radiobutton(text="The first button", variable=r_var, value=0)
r2 = Radiobutton(text="The second button", variable=r_var, value=1)

r1.pack()
r2.pack()

And the result is like this:

Tugas 3. Recommendations
Let's try to develop a program where the user selects a range that includes his age,
and in response receives a recommendation for a suitable age-restricted movie:
First, create an integer TKinter-variable and set its value using the set() command:

var = IntVar()
var.set(0)

Then we make several radiobuttons linked to this variable:

kids = Radiobutton(text="0-7", variable=var, value=0)


teens = Radiobutton(text="7-14", variable=var, value=1)
youngs = Radiobutton(text="14-21", variable=var, value=2)

Now let's write a handler change(), which will display the correct list of movies in
our Label (not yet created) depending on the selected button.

We will read the values from the TKinter-variable with the get() command:

def change():
if var.get() == 0:
label["text"] = "Puss in Boots, Shrek"
elif var.get() == 1:
label["text"] = "The Avengers, Dune"
elif var.get() == 2:
label["text"] = "Gone with the Wind, King Kong"
Now let’s create a button and a Label widget that shows the right movies:

button = Button(text="Сhange", command=change)


label = Label(width=40, height=10)

And finally, let's place everything in our window!

kids.pack()
teens.pack()
youngs.pack()
button.pack()

Tugas 4. Checkbuttons
Checkbutton - or flag - is another selection widget from Tkinter. Flags work the
same way as radiobuttons, but there are several possible answers in the case of
flags.
The flags are not connected and do not depend on each other in any way, because
each answer choice is a separate button. Does this mean that we don't need
TKinter-variables here?
The correct answer is: not at all because you still have to store the state of the
flags. Otherwise, it will be impossible to determine whether the user has ticked the
box or not.
Let's try to create a couple of Checkbutton widgets. They are notoriously unrelated,
which means that each flag must have a different variable, otherwise one flag will
change the state of the other:

var1 = BooleanVar()
var1.set(0)
var2 = BooleanVar()
var2.set(0)

Then create the flags themselves - first_check and second_check, and set a
variable, value, and handler function for each of them:
first_check = Checkbutton(text="The first button", variable=var1, onvalue=1,
offvalue=0, command=check)
second_check = Checkbutton(text="The second button", variable=var2, onvalue=1,
offvalue=0, command=check)

This is pretty much the same as the Radiobutton, except that the button now stores
not one value, but two: a value for the disabled flag and a value for the enabled
flag. It's also worth noting that not only Button has the command property, but
also Checkbutton and Radiobutton.
So, we already have a check() handler and all the preparatory work is done. It only
remains to create a Label and output results of the handler's work:

label = Label(text="Nothing is selected", width=25, height=5)


first_check.pack()
second_check.pack()
label.pack()

And enjoy the result!

Tugas 5. Knowledge survey


Create a program that finds out which programming languages from the list below
a user knows :

 Python
 Java
 C#

And then display in the Label a string like "I know Python, Java, C#" with the
user's choices:
Hint: use StringVar variables!

Tugas 6. Listbox
Listbox is a widget that can display a list of specified items with the ability to
select one or more items from the list.
Create and arrange this widget as usual: specify the width, height, and selectmode
property that can take the SINGLE value if only one element can be selected, or
EXTENDED value if several elements can be selected:

listbox = Listbox(width=20, height=10, selectmode = SINGLE)


listbox.pack()

But the list is filled in unusually: not at the time of creation, as is usually the case,
but after creation and placement:

for element in ("The first", "The second", "The third"):


listbox.insert(0, element)

Here are all the functions that Listbox has:

get(element_index) gets an element by the index

insert(insert_place_index, inserts an element, if you want to add


element) an element to the end of the list,
specify END as the insertion index

delete(element_index) removes an element by their index


curselection() returns the index of the element
selected with the cursor

Tugas 7. Cash register


A large retail chain needs its own accounting system for sold goods. Let’s help to
program this system!

We need to write a program that gets the name (or part number) of a product using
the usual Entry, and then adds it to the List displaying this list on the screen:

Tugas 8. Kahoot!
Follow the link and enter the pin code that the teacher gave you to participate in
the Kahoot quiz!
https://kahoot.it/
Pekerjaan Rumah 1. Phone book
Once again, we have programmer Ben with his endless ideas!
This time Ben decided to make an electronic phone book with the numbers of his
friends. But he still hasn't learned TKinter, so he needs our help again :)
Let's help Ben to make a phone book with the numbers of his four friends:
 Billy - 445-241-12
 Mike - 442-555-41
 Jack - 448-112-37
 Tom - 441-536-52
You need to make sure that Ben chooses a friend's name (Radiobutton will help
here), and when the button is pressed, the phone number of this person is displayed
in the Label.
Try to set up this property for each radiobutton:

indicatoron=0

Then each radio button will look like an ordinary button:

Pekerjaan Rumah 2. To-do list


In any large IT company, programmers divide their tasks into several lists and keep
track of their status. There are only two main lists:
 tasks for the day
 already completed tasks
Let's help programmers from a not very large, but very ambitious company to
create such a program.
Show two lists on the screen, and between them place a button that will move the
selected task from the first list to the second:

You might also like