You are not logged in.

#1 2009-08-06 13:29:02

FSX
Member
Registered: 2009-08-06
Posts: 57

Volume control (python systemtray volume controller)

Made a simple volume controller in Python/PyGTK that uses amixer to adjust the volume.

Just needed something to adjust the volume. ^^

volume_control.png

#!/usr/bin/env python

import gtk, subprocess

class volume_control():
    window_position = (0, 0)
    vol_adjust = range(0, 26)

    def __init__(self):
        # Icons/systemtray
        self.staticon = gtk.StatusIcon()
        self.staticon.set_from_stock(gtk.STOCK_MEDIA_RECORD)

        # Menu
        menu = gtk.Menu()
        menu_about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
        menu_about.connect('button-press-event', self.show_about_dialog)
        menu_quit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
        menu_quit.connect('button-press-event', gtk.main_quit)
        menu.append(menu_about)
        menu.append(menu_quit)

        # Events
        self.staticon.connect('activate', self.cb_activate_icon)
        self.staticon.connect('popup-menu', self.cb_tray_popup, menu)

        # Initialize
        self.master_slider_window()

    #
    # The volume slider window
    #
    def master_slider_window(self):
        # Window
        self.window = gtk.Window(gtk.WINDOW_POPUP)
        self.window.set_size_request(44, 171)

        # Frame
        frame = gtk.Frame()
        frame.set_border_width(1)

        # Slider
        master_slider = gtk.VScale()
        master_slider.set_inverted(True)
        master_slider.set_range(0, 100)
        master_slider.set_increments(1, 10)
        master_slider.set_digits(0)
        master_slider.set_size_request(34, 160)
        master_slider.set_value_pos(gtk.POS_BOTTOM)
        master_slider.set_value(self.get_master_volume())

        # Events
        self.window.connect('destroy', gtk.main_quit)
        master_slider.connect('value-changed', self.cb_master_slider_change)

        # Add widgets
        fixed_master_slider = gtk.Fixed()
        fixed_master_slider.put(master_slider, 3, 5)
        frame.add(fixed_master_slider)
        self.window.add(frame)

    #
    #  Tray icon click
    #
    def cb_activate_icon(self, widget, data=None):
        if self.window.get_property('visible'):
            self.window.hide()
        else:
            self.set_window_position()
            self.window.move(self.window_position[0], self.window_position[1])
            self.window.show_all()
            self.window.present()

    #
    #  System tray menu
    #
    def cb_tray_popup(self, widget, button, time, data = None):
        if button == 3:
            if data:
                data.show_all()
                data.popup(None, None, None, 3, time)

    #
    #  Set master volume
    #
    def cb_master_slider_change(self, widget):
        val = widget.get_value()

        if (val / 4) in self.vol_adjust:
            proc = subprocess.Popen('/usr/bin/amixer sset Master ' + str(val) + '%', shell=True, stdout=subprocess.PIPE)
            proc.wait()

    #
    #  Set window position (just above the system tray icon)
    #
    def set_window_position(self):
        staticon_geometry = self.staticon.get_geometry()[1]

        if staticon_geometry.y <= 200:
            y_coords = staticon_geometry.y
        else:
            y_coords = staticon_geometry.y-180

        self.window_position = (staticon_geometry.x-13, y_coords)

    #
    #  Get Master volume
    #
    def get_master_volume(self):
        proc = subprocess.Popen('/usr/bin/amixer sget Master', shell=True, stdout=subprocess.PIPE)
        amixer_stdout = proc.communicate()[0].split('\n')[4]
        proc.wait()

        find_start = amixer_stdout.find('[') + 1
        find_end = amixer_stdout.find('%]', find_start)

        return float(amixer_stdout[find_start:find_end])

    #
    #  About dialog
    #
    def show_about_dialog(self, widget, data=None):
        about = gtk.AboutDialog()
        about.set_program_name('Volume Control')
        about.set_version('0.1.1')
        about.set_comments('Volume Control is a simple tool to adjust your Master volume. It uses "amixer" from alsa to set and get the volume.\n\nWritten by FSX')
        about.run()
        about.destroy()

volume_control()
gtk.main()

Offline

#2 2009-10-27 23:27:33

Gattaka
Member
Registered: 2009-10-27
Posts: 1

Re: Volume control (python systemtray volume controller)

Thanks a lot ! smile

This is what I've been loking for - I've just add a part for control volume by scrolling with mouse wheel over the tray icon - hope someone find it useful like I did wink

import subprocess
import pygtk
pygtk.require('2.0')
import gtk
from gtk import gdk
self.staticon.connect('scroll_event', self.scroll_event)

To the slider:

global master_slider
def scroll_event(self,param2,event):
        
        if event.direction == gdk.SCROLL_UP:
            if master_slider.get_value() < 100:
                master_slider.set_value(master_slider.get_value() + 1)
                
        if event.direction == gdk.SCROLL_DOWN:
            if master_slider.get_value() > 0:
                master_slider.set_value(master_slider.get_value() - 1)
    
        self.cb_master_slider_change(master_slider)

Last edited by Gattaka (2009-10-27 23:29:56)

Offline

#3 2009-10-30 13:13:56

ApotheoZ
Member
Registered: 2009-08-10
Posts: 40

Re: Volume control (python systemtray volume controller)

Could you please be more clear about the code modification for the mouse support please Gattaka ?
I do not understand where do you place your modification into the original source code.

Anyway, great job FSX !

Last edited by ApotheoZ (2009-11-05 07:44:34)

Offline

#4 2009-11-03 13:27:17

FSX
Member
Registered: 2009-08-06
Posts: 57

Re: Volume control (python systemtray volume controller)

Here's some updated code: http://61924.nl/blog/e/00016-pygtk-volume-slider.html

I can't work on it anymore. I switched to OSS some weeks ago. Maybe I'll try to write a slider for that too. tongue

Offline

#5 2009-11-04 08:46:44

jarryson
Member
Registered: 2007-02-18
Posts: 298

Re: Volume control (python systemtray volume controller)

i am using oss too, and i want an OSS version ; )

Offline

#6 2009-11-04 17:43:13

FSX
Member
Registered: 2009-08-06
Posts: 57

Re: Volume control (python systemtray volume controller)

Someone at #oss (freenode) gave me this script: http://pastebin.furver.se/0xflchkfz/

And it works great. smile

Offline

#7 2009-11-06 03:05:39

jarryson
Member
Registered: 2007-02-18
Posts: 298

Re: Volume control (python systemtray volume controller)

yes, it works well, thanks!

Offline

#8 2010-02-09 20:29:52

anonymous_user
Member
Registered: 2009-08-28
Posts: 3,058

Re: Volume control (python systemtray volume controller)

When I tried FSX's script I get this:

Traceback (most recent call last):
  File "/home/kevin/bin/volume1", line 149, in <module>
    VolumeControl()
  File "/home/kevin/bin/volume1", line 40, in __init__
    self.slider_window()
  File "/home/kevin/bin/volume1", line 55, in slider_window
    slider.set_value(self.__get_master_volume())
  File "/home/kevin/bin/volume1", line 134, in __get_master_volume
    return float(amixer_stdout[find_start:find_end])
ValueError: invalid literal for float(): Mono

Offline

#9 2010-02-09 21:00:08

FSX
Member
Registered: 2009-08-06
Posts: 57

Re: Volume control (python systemtray volume controller)

Sorry, I can't do much about it at the moment. I'm using OSS now.

Offline

#10 2010-02-10 02:01:15

epinull
Member
Registered: 2010-01-25
Posts: 10

Re: Volume control (python systemtray volume controller)

On your blog you said you couldn't find a volume slider, so just FYI, there are a few:
volwheel
gvolwheel
I also posted one a few weeks ago, but no one seemed to care.

Anyway, thansk for sharing! smile

EDIT: Sorry, didn't realize this is an old thread. :S

Last edited by epinull (2010-02-10 02:08:01)

Offline

#11 2010-03-02 16:35:30

banan
Member
Registered: 2010-03-01
Posts: 13

Re: Volume control (python systemtray volume controller)

-removed

Last edited by banan (2010-03-02 16:39:14)

Offline

Board footer

Powered by FluxBB