Мodule 4 Lesson 1 Python GUI
М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.pack()
r2.pack()
However, if you run this code, you will see that both buttons are pressed:
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()
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)
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:
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:
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:
But the list is filled in unusually: not at the time of creation, as is usually the case,
but after creation and placement:
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