[Tutor] Help me understand this tkinter related code

Robert DeLaurentis lists at bobdel.com
Fri Feb 26 18:52:39 CET 2010

I've just begun learning Python and programming using the Head First Programming from O'Reilly. The following code works on my Mac just fine, but there is a mystery as to how its working that escapes me so far.

The local function change_volume(v) requires the argument v to operate properly, but I'm unclear where the value of that argument is being set or why the code fails without it. It appears to be a detail that isn't explained in the text. When I compare the two widgets and their associated functions, both seem to use the .get method to access the value set by the widget, so in that case the v seems superfluous.

I thought that perhaps the answer might be found in the tkinter code that defines Scale, but I'm not even sure where on the computer that code is located.

Thank you!

Here is the code (the comments are mine, otherwise the code matches the book except that I grouped some items differently -- I like to keep variables all in one place as much as possible):


# load external libraries
from tkinter import *
import pygame.mixer

# initialize the visual interface
app = Tk()
app.title("Head First Mix")

# initialize the sound player
mixer = pygame.mixer

# initialize the variables
sound_file = "50459_M_RED_Nephlimizer.wav"
track = mixer.Sound(sound_file)
track_playing = IntVar()

# local functions
def shutdown():

def track_toggle():
    if track_playing.get() == 1:
        track.play(loops = -1)

def change_volume(v):

# define interface widgets
track_button = Checkbutton(app, variable = track_playing, 
                                command = track_toggle, 
                                text = sound_file)


volume = DoubleVar()
volume_scale = Scale(app,
                        variable     = volume,
                        from_        = 0.0,
                        to           = 1.0,
                        resolution   = 0.1,
                        command      = change_volume,
                        label        = "Volume",
                        orient       = HORIZONTAL)


# main entry point
app.protocol("WM_DELETE_WINDOW", shutdown)

More information about the Tutor mailing list