Working with checkboxes in tkinter

The tkinter Checkbutton needs three things: an IntVar to hold its current value, an event-handler function to run when it's ticked, and a descriptive label to say what it does. Take a look at this example code:

The wflip_itO" function is the Checkbutton's ^Nk event handler.

The Checkbutton is associated With the "IntVar", links to the event handler, and has a descriptive label, too.

Create an "IntVar" to hold a va|ue that flipper = IntVar ( ) ^ is ei±. i , . dlue, ™at pp () is either 1 °r 0, depending on whether the checkbox is ticked.

Checkbutton(app, variable command text flipper, flip_it,

Using the get() method

If you look closely at the code for the flip_it ( ) event handler, you'll notice that the message displayed on screen is controlled by whatever value is returned from the call to flipper . get ( ). The get ( ) method is part of every IntVar object, and it lets you easily determine, in this case, the current value associated with the flipper variable.

But, what sets the value?

The Checkbutton automatically sets the value of flipper as a result of the click on the checkbox. Tick the box and the value is set to 1. Untick the box and the value is set to 0.

O Here's your code from earlier. Use your pencil to put a line through the code you don't need anymore:

from tkinter import * import pygame.mixer app = Tk()

app.title("Head First Mix") app.geometry('250x100+200+100')

sound_file = "50459_M_RED_Nephlimizer.wav"

mixer = pygame.mixer mixer.init()

def track_start():

def track_stop(): track.stop()

def shutdown(): track.stop() app.destroy()

track = mixer.Sound(sound_file)

start_button = Button(app, command = track_start, text start_button.pack(side = LEFT)

stop_button = Button(app, command = track_stop, text = stop_button.pack(side = RIGHT)

app.protocol("WM_DELETE_WINDOW", shutdown) app.mainloop()

©Write the code you need to implement the checkbox here, based on the sample code from the previous page. Give your IntVar the name track_playing. Use track_toggle as your function name, and call the checkbox track button:

checkbox added

Here's your code from earlier. You were to use your pencil to put a line through the code you don't need anymore:

from tkinter import * import pygame.mixer app = Tk()

app.title("Head First Mix") app.geometry('250x100+200+100')

sound_file = "50459_M_RED_Nephlimizer.wav"

mixer = pygame.mixer mixer.init()

def track_start().

^^ The functions that start and stop track. stopQ_ the track are no lon9er needed def shutdown(): track.stop() app.destroy()

You can also get rid of the two buttons, track = mixer.s°und(s°und_file) since they aren't needed either start_button - Button(app,—command = track_stnrt,—text - "Start")— start_b|tt°n pnck(sid~ ~ LEFT) -

stop_button = Rntton^app, command - trnc.k_st°p^—text -—"Stop" )___

app.protocol("WM_DELETE_WTNDOW", shutdown) app.mainloop()

©Write the code you need to implement the checkbox here. You were asked to give your TntVar the name track_playing, use track_toggle as your function name, and call the checkbox track_button:

else:

track_playing = |ntVar()

track_button = Checkbutton(app, variable = track_playing,

Use the. name of the sound command = track toggle, file as the text associated-------- ^

with the checkbox. text ~ sound_file)

track_button.pack()

Was this article helpful?

0 0

Post a comment