[Tutor] A volume control (:

Michael Lange klappnase at freenet.de
Fri Nov 21 14:45:49 EST 2003


On Fri, 21 Nov 2003 00:51:27 -0600
Larry <llwyble at cox.net> wrote:

> 
> Well, I have been working on this and got this far but I think I screwed up
> Because I can't figure out how to set the initial volume on the slider instead of
> it going back to 0 (zero) everytime I start it.  I wanted to use:
> 
> Current_vol = mixer.get(ossaudiodev.SOUND_MIXER_VOLUME)
> 
> To get the current volume of the mixer. But I didn't consider this until 
> I got this far and I don't think my slider code is going to allow an initial setting
> of the slider. Or is it?  Arg...
> 
> 
Hi Larry,

I think you should use a Tkinter.DoubleVar for the volume settings which you can bind to the Scale widget
(in case the volume is a float value, I don't know about ossaudiodev, if it is an integer of course you
should use a Tkinter.IntVar()).
If you set the value of this variable to the current volume before the scale is created I think it should work:

> #!/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()

Like I said, I don't know what mixer.get() returns, (I haven't even ossaudiodev installed), but
I think this should do the trick to set the initial volume.

Cheers

Michael



More information about the Tutor mailing list