[Tutor] A volume control (:

Michael Lange klappnase at freenet.de
Fri Nov 21 23:04:04 EST 2003


On Fri, 21 Nov 2003 19:59:43 -0600
Larry <llwyble at cox.net> wrote:
> 
> 
> This didn't set the current volume.  I'm not really sure what it does.
> The variable word is a global name. It's coming after the call to print.
> And even if I put it before the call to print, it doesn't change the setting
> of the  scale on startup. It still starts at zero.  I'm not completely sure
> what to do with 'variable'.   (:
> 
> Where can I read about 'variable' and the relationship with 'IntVar()'
> 
> I'm pretty green at this and I don't quite grasp what's going on here.
> 
> Thanks
> 
> 


Hmmm,

what's wrong here....

I've written a little test script to see how the variable works:
#############################

from Tkinter import *

class Scale_Test:
    def __init__(self, master):
        self.a = IntVar()
        self.a.set(49)
        self.s = Scale(master, from_=100, to=0, length=200, width=20,\
                       variable=self.a, command=self.scale_cmd)
        self.s.pack()

    def scale_cmd(self, event=None):
        print self.a.get()

        
def main():
    r = Tk()
    t = Scale_Test(r)
    r.mainloop()

main()

##############################

This does obviously what it should: on startup the Scale widget gets set to 49
and when the Scale is moved the variable changes its value.

Now, what happens in your script?

> #!/usr/local/bin/python
> 
> 
> from Tkinter import *
> 
> import ossaudiodev
> mixer = ossaudiodev.openmixer()
> 
> class VOLUME(Frame):
>     def print_value(self, val):
>         ival = int(val)
>         a = (ival,ival)
>         mixer.set(ossaudiodev.SOUND_MIXER_VOLUME, a)
> 
>     def createWidgets(self):
>         self.slider = Scale(self, from_=0, to=100,
>                             orient=HORIZONTAL,
>                             length="3i",
>                             command=self.print_value,
			      variable=self.Current_vol)
> 
>         self.QUIT = Button(self, text='QUIT', foreground='red', command=self.quit)
> 
>         self.slider.pack(side=LEFT)
>         self.QUIT.pack(side=LEFT, fill=BOTH)
> 
>     def __init__(self, master=None):
>         Frame.__init__(self, master)
>         Pack.config(self)
	  #get the current volume setting:
	  self.Current_vol = IntVar()
	  self.Current_vol.set(mixer.get(ossaudiodev.SOUND_MIXER_VOLUME))
>         self.createWidgets()
> 
> test = VOLUME()
> test.mainloop()

Maybe the problem is here:

>>>  self.Current_vol.set(mixer.get(ossaudiodev.SOUND_MIXER_VOLUME))

Are you sure that mixer.get(ossaudiodev.SOUND_MIXER_VOLUME) returns an integer?
With the set method you used a 2-tuple:

>>> a = (ival,ival)
>>> mixer.set(ossaudiodev.SOUND_MIXER_VOLUME, a)

so maybe mixer.get(etc.) returns a tuple with the left- and right channel values.
(like I said before, I don't know anything about ossaudiodev).

You might find out this if you change your __init__ like this:

>     def __init__(self, master=None):
>         Frame.__init__(self, master)
>         Pack.config(self)
	  #get the current volume setting:
	  self.Current_vol = IntVar()
          cv = mixer.get(ossaudiodev.SOUND_MIXER_VOLUME)
          print type(cv)
	  self.Current_vol.set(cv)
>         self.createWidgets()

Good luck to you

Michael



More information about the Tutor mailing list