Label it

When it comes to adding a label to your GUI, use the tkinter Label widget. You create it in code not unlike the way you create a button. Here's the code to add a label to an existing GUI application. The label simply displays a string:

Create a new label, attach it to the main window, give it some text, and adjust the label's height-

l = Label(app, text='When you are ready, click on the buttons!', height = 3) l.pack() \

Put the label Code before the Code for the buttons and the m will then look-^

like this.

Put the label Code before the Code for the buttons and the m will then look-^

like this.

Another variation replaces the text parameter with textvariable. If you assign a special tkinter variable to this parameter, the label will change whenever the value of the variable changes, automatically.

Create an "IntVar".

Associate — "the 'UVar" with the label.

11 = Label(app, textvariable = num_good) 11.pack(side = 'left')

num_good.set(100)

Mse the VtO" method to adjust the -r value of the Ml/ar", and the éjU SJ updates, as if by magic.

Mse the VtO" method to adjust the -r value of the Ml/ar", and the éjU SJ updates, as if by magic.

add labels long Ex^RClSe

Based on what you now know about adding a label to a GUI, rework your GUI code so that it uses two labels. One should display the number of correct answers and the other should display the number of wrong answers. We've left plenty of room for you to write in all the code that your program now needs.

label results h

Exs&ciSe

§OLutiOH

Based on what you now know about adding a label to a GUI, you were asked to rework your GUI code so that it uses two labels. One should display the number of correct answers and the other should display the number of wrong answers. We left plenty of room for you to write in all the code that your program now needs.

Start by importing the from tkinter import *

import pygamemixer def . p!ay_Correct_sound();

Create the two event handlers

IntVar and p.ay def play_wron9_sound():

the appropriate numbad.set(numbad.get() + 1) sound.

wrong_s.play()

Create.the GU' - app.title("TVN Game Show")

application window.

app.geometryC300xl/0+200+l00') tia.ize the sounds = pygame mixer

sound system. \

sounds.init()

o^ m -the (^correct s = sounds.Sound("correctwav") required sound

[ wrong_s = sounds.Sound("wrongwav")

Create two

of Correct answers I

num-3oodset(0;

Display a friendly

-stxtt Jells lab .=.Label(app, te.xt='Wh.e.n.,y?u. are. ready,, click . on . the .buttons,'', . height . = 3)

Be sure to PACK your lab.pack()

widgets.

Create t lab' = Label(app, textvariable = num_good)

each Counter

and connect the labels to the relevant IntVars^^ lab2 = Label(app, textvariable = num_bad) lab2.pack(side = 'right')

bl = Button(app, text = "Correct!", width = 10, Command = play_correct_sound)

and ^ b|-.Pack(side . = . left', , , .padx. = . 10, pady = . 10)

Connect them to their relevant

event hand|er. N. b2 = Button(app, text = "Wrong!", width = 10, Command = play_wrong_sound)

Start tkinter's main event loop-----

app.mainloop()

Was this article helpful?

0 0

Post a comment