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!
Bob
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):
#!/usr/local/bin/python3
# load external libraries
from tkinter import *
import pygame.mixer
# initialize the visual interface
app = Tk()
app.title("Head First Mix")
app.geometry('250x100+200+100')
# initialize the sound player
mixer = pygame.mixer
mixer.init()
# initialize the variables
sound_file = "50459_M_RED_Nephlimizer.wav"
track = mixer.Sound(sound_file)
track_playing = IntVar()
# local functions
def shutdown():
track.stop()
app.destroy()
def track_toggle():
if track_playing.get() == 1:
track.play(loops = -1)
else:
track.stop()
def change_volume(v):
track.set_volume(volume.get())
# define interface widgets
track_button = Checkbutton(app, variable = track_playing,
command = track_toggle,
text = sound_file)
track_button.pack()
volume = DoubleVar()
volume.set(track.get_volume())
volume_scale = Scale(app,
variable = volume,
from_ = 0.0,
to = 1.0,
resolution = 0.1,
command = change_volume,
label = "Volume",
orient = HORIZONTAL)
volume_scale.pack()
# main entry point
app.protocol("WM_DELETE_WINDOW", shutdown)
app.mainloop()
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor