You are not logged in.
Hey all, I wrote a little python app to scartch an itch I had, namely having an icon somewhere that uses my gtk icon theme and that allows me to change the volume. I managed to do this, but now I want to add one more (important) piece of functionality. As of now, I call update_status_icon() whenever some sort of event is triggered in my application that changes the volume, so that the icon is set according to the current volume level. However, I would like it to also change the icon if I change the volume using the volume buttons on my laptop or using alsamixer directly. How can I make my application 'watch' over the current volume and update the icon when it changes?
Here is my sourcecode:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import alsaaudio as alsa
import gtk
import threading
######################
## VOLUME FUNCTIONS ##
######################
def get_master_volume():
    return alsa.Mixer("Master").getvolume()[0]
def is_master_mute():
    if alsa.Mixer("Master").getmute()[0] == 1:
        return True
    return False
def set_master_volume(value):
    if value > 100:
        value = 100
    elif value < 0:
        value = 0
    alsa.Mixer("Master").setvolume(value)
def toggle_master_mute():
    if is_master_mute():
        alsa.Mixer("Master").setmute(0)
    else:
        alsa.Mixer("Master").setmute(1)
################
## GTK APPLET ##
################
class VolumeApp:
    def __init__(self):
        self.statusicon = gtk.StatusIcon()
        self.statusicon.connect("button_press_event", self.cb_button)
        self.statusicon.connect("scroll_event",       self.cb_scroll)
        self.updater = threading.Timer(0.1, self.update).start()
        self.update()
    def cb_button(self, param, data=None):
        toggle_master_mute()
        self.update()
    def cb_scroll(self, param, data=None):
        if data.direction == gtk.gdk.SCROLL_UP:
            set_master_volume(get_master_volume() + 4)
        elif data.direction == gtk.gdk.SCROLL_DOWN:
            set_master_volume(get_master_volume() - 4)
        self.update()
    def get_icon_name(self):
        if is_master_mute() == True:
            return "audio-volume-muted"
        value = get_master_volume()
        if 66 < value <= 100:
            return "audio-volume-high"
        if 33 < value <= 66:
            return "audio-volume-medium"
        if 0 < value <= 33:
            return "audio-volume-low"
        if value == 0:
            return "audio-volume-muted"
        return "error"
    def update(self):
        self.statusicon.set_from_icon_name(self.get_icon_name())
        self.statusicon.set_tooltip(str(get_master_volume()))
    def main(self):
        self.statusicon.set_visible(True)
        gtk.main()
##################
## MAIN PROGRAM ##
##################
if __name__ == "__main__":
    app = VolumeApp()
    app.main()Last edited by robrene (2010-01-17 18:54:30)
 
 
 
 
 
 
 
 
 
 
 ![]()
Offline
Update:
I am now trying to get the application to run update() at a specified interval through a seperate thread. However, I'm not familiar with python threads. Could someone please tell me why this isn't working? I posted the edited source code in my original post
Last edited by robrene (2010-01-17 15:50:31)
 
 
 
 
 
 
 
 
 
 
 ![]()
Offline
Update:
I am now trying to get the application to run update() at a specified interval through a seperate thread. However, I'm not familiar with python threads. Could someone please tell me why this isn't working? I posted the edited source code in my original post
I think you need one of these:
gobject.threads_init()also take a look at the section of the gtk faq on threads http://faq.pygtk.org/index.py?req=show& … 20.001.htp
Offline
That one silly line of code solved everything! Thanks! I've been looking for a solution for this problem all day now.
 
 
 
 
 
 
 
 
 
 
 ![]()
Offline