Page 288 -
P. 288
building a graphical user interface
Load in the
required sound correct_s = sounds.Sound(“correct.wav”)
effects. wrong_s = sounds.Sound(“wrong.wav”)
Create two
IntVars: one to
count the number num_good = IntVar()
of correct answers
and another to num_good.set(0)
count the number num_bad = IntVar()
of wrong answers.
num_bad.set(0)
Display a friendly
message that tells the lab = Label(app, text=‘When you are ready, click on the buttons!', height = 3)
host what to do.
Be sure to PACK your lab.pack()
widgets.
lab1 = Label(app, textvariable = num_good)
Create two
labels to hold lab1.pack(side = ‘left')
each counter
and connect the
labels to the
relevant IntVars. lab2 = Label(app, textvariable = num_bad)
lab2.pack(side = ‘right')
b1 = Button(app, text = “Correct!", width = 10, command = play_correct_sound)
Create each of
the buttons and b1.pack(side = ‘left', padx = 10, pady = 10)
connect them to
their relevant
event handler. b2 = Button(app, text = “Wrong!", width = 10, command = play_wrong_sound)
b2.pack(side = ‘right', padx = 10, pady = 10)
Start
tkinter’s main
event loop.
app.mainloop()
you are here 4 253