Create a gist now

Instantly share code, notes, and snippets.

Brightness adjustment slider GUI (wxPython or Tk)
"""
A super-simple GUI for setting my laptop backlight.
For some reason Ubuntu Answers couldn't tell me where to change the minimum brightness and step interval.
So I had to make something because my laptop can be MUCH dimmer than Ubuntu's minimum.
Added this version for tk, since it is such a simple GUI.
"""
import os
import subprocess
from Tkinter import *
class BrightnessSlider(object):
find_max_brightness_cmd = ['cat', '/sys/class/backlight/intel_backlight/max_brightness']
find_brightness_cmd = ['cat', '/sys/class/backlight/intel_backlight/actual_brightness']
def __init__(self, master):
min_brightness = 92
p1 = subprocess.Popen(self.find_brightness_cmd,
stdout=subprocess.PIPE)
p2 = subprocess.Popen(self.find_max_brightness_cmd,
stdout=subprocess.PIPE)
current_brightness, stderr = p1.communicate()
max_brightness, stderr = p2.communicate()
self.slider = Scale(master,
from_=int(min_brightness),
to=int(max_brightness),
orient=HORIZONTAL,
command=self.on_slide)
self.slider.set(current_brightness)
self.slider.pack()
self.slider.focus_set()
def on_slide(self, new_value):
self.set_brightness(new_value)
def set_brightness(self, new_val):
with open('/sys/class/backlight/intel_backlight/brightness', 'w') as f:
f.write(new_val)
if __name__ == "__main__":
master = Tk()
b = BrightnessSlider(master)
mainloop()
import os
import wx
import subprocess
find_max_brightness_cmd = ['cat', '/sys/class/backlight/intel_backlight/max_brightness']
find_brightness_cmd = ['cat', '/sys/class/backlight/intel_backlight/actual_brightness']
def set_brightness(new_val):
with open('/sys/class/backlight/intel_backlight/brightness', 'w') as f:
f.write(str(new_val))
min_brightness = 92
p = subprocess.Popen(find_brightness_cmd, stdout=subprocess.PIPE)
current_brightness, stderr = p.communicate()
p = subprocess.Popen(find_max_brightness_cmd, stdout=subprocess.PIPE)
max_brightness, stderr = p.communicate()
class brightness_slider(wx.Frame):
"""
A super-simple GUI for setting my laptop backlight.
For some reason Ubuntu Answers couldn't tell me where to change the minimum brightness and step interval.
So I had to make something because my laptop can be MUCH dimmer than Ubuntu's minimum.
The GUI can be dragged by right-clicking anywhere on it.
"""
def __init__(self):
wx.Frame.__init__(self, parent=None, title="Change Brightness",
style=(wx.DEFAULT_FRAME_STYLE &
~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX)
)
)
panel = wx.Panel(self, -1)
self.slider = wx.Slider(panel, value = int(current_brightness), minValue=int(min_brightness), maxValue=int(max_brightness))#, size=())
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.slider, 1, wx.EXPAND)
panel.SetSizer(sizer)
#get the slider's height and width
ss = self.slider.GetBestSize()
#replace the slider's width with 100
ss[0] = 100
self.SetSize(ss)
self.SetMaxSize(ss)
self.slider.Bind(wx.EVT_SLIDER, self.on_slide)
#bind all the widgets
self.Bind(wx.EVT_RIGHT_UP, self.MouseUp)
panel.Bind(wx.EVT_RIGHT_UP, self.MouseUp)
self.slider.Bind(wx.EVT_RIGHT_UP, self.MouseUp)
self.slider.Bind(wx.EVT_RIGHT_DOWN, self.MouseDown)
self.Bind(wx.EVT_IDLE, self.OnIdle)
def on_slide(self, event):
set_brightness(int(event.GetEventObject().GetValue()))
def MouseDown(self, event):
wx.Window.CaptureMouse(self.slider)
#figure out how thick the Title and Frame border are
#subtract the panel size from the overall frame
sizeDiff = self.GetSize() - self.GetClientSize()
#these get the system specific thicknesses,
#since this works I believe Windows is showing a 3D border next to the normal border
borderAdjust = wx.SystemSettings.GetMetric(wx.SYS_BORDER_Y) + wx.SystemSettings.GetMetric(wx.SYS_EDGE_Y)
#the left and right side border is identical, so we can divide by 2
#since the upper border isn't the same as the bottom, adjust only for the top
sizeDiff = wx.Point(sizeDiff[0]/2, sizeDiff[1]-borderAdjust)
#use the global mouse position, since event.GetPosition() can come from a widget,
# and this will give non-Panel referenced coordinates
self.posInFrame = self.ScreenToClient(wx.GetMousePosition()) + sizeDiff
def MouseUp(self, event):
wx.Window.ReleaseMouse(self.slider)
def OnIdle(self, event):
if self.slider.HasCapture():
topLeftInScreen = wx.GetMousePosition()
topLeftInScreen = topLeftInScreen - self.posInFrame
self.SetPosition(topLeftInScreen)
event.Skip()
if __name__ == "__main__":
app = wx.App(False)
frame = brightness_slider()
frame.Show()
#import wx.lib.inspection
#wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment