You are not logged in.
Made a simple volume controller in Python/PyGTK that uses amixer to adjust the volume.
Just needed something to adjust the volume. ^^
#!/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
Thanks a lot !
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
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
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
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.
Offline
i am using oss too, and i want an OSS version ; )
Offline
Someone at #oss (freenode) gave me this script: http://pastebin.furver.se/0xflchkfz/
And it works great.
Offline
yes, it works well, thanks!
Offline
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
Sorry, I can't do much about it at the moment. I'm using OSS now.
Offline
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!
EDIT: Sorry, didn't realize this is an old thread. :S
Last edited by epinull (2010-02-10 02:08:01)
Offline
-removed
Last edited by banan (2010-03-02 16:39:14)
Offline