#!/usr/bin/env python # Import library functions we need import time import Tkinter import wiringpi2 as wiringpi wiringpi.wiringPiSetup() # Setup software PWMs on the GPIO pins PIN_RED = 0 PIN_GREEN = 2 PIN_BLUE = 3 LED_MAX = 100 wiringpi.softPwmCreate(PIN_RED, 0, LED_MAX) wiringpi.softPwmCreate(PIN_GREEN, 0, LED_MAX) wiringpi.softPwmCreate(PIN_BLUE, 0, LED_MAX) wiringpi.softPwmWrite(PIN_RED, 0) wiringpi.softPwmWrite(PIN_GREEN, 0) wiringpi.softPwmWrite(PIN_BLUE, 0) # A function to set the LedBorg colours def SetLedBorg(red, green, blue): wiringpi.softPwmWrite(PIN_RED, int(red * LED_MAX)) wiringpi.softPwmWrite(PIN_GREEN, int(green * LED_MAX)) wiringpi.softPwmWrite(PIN_BLUE, int(blue * LED_MAX)) # A function to turn the LedBorg off def LedBorgOff(): SetLedBorg(0, 0, 0) # Class representing the GUI dialog class LedBorg_tk(Tkinter.Tk): # Constructor (called when the object is first created) def __init__(self, parent): # Setup the parent object Tkinter.Tk.__init__(self, parent) self.parent = parent self.protocol("WM_DELETE_WINDOW", self.OnExit) # Call the OnExit function when user closes the dialog # Set any state variables self.red = 0 self.green = 0 self.blue = 0 # Setup a grid of 3 sliders which control red, green, and blue self.grid() self.sldRed = Tkinter.Scale(self, from_ = LED_MAX, to = 0, orient = Tkinter.VERTICAL, command = self.sldRed_move) self.sldRed.set(0) self.sldRed.grid(column = 0, row = 0, rowspan = 1, columnspan = 1, sticky = 'NSEW') self.sldGreen = Tkinter.Scale(self, from_ = LED_MAX, to = 0, orient = Tkinter.VERTICAL, command = self.sldGreen_move) self.sldGreen.set(0) self.sldGreen.grid(column = 1, row = 0, rowspan = 1, columnspan = 1, sticky = 'NSEW') self.sldBlue = Tkinter.Scale(self, from_ = LED_MAX, to = 0, orient = Tkinter.VERTICAL, command = self.sldBlue_move) self.sldBlue.set(0) self.sldBlue.grid(column = 2, row = 0, rowspan = 1, columnspan = 1, sticky = 'NSEW') # Make an Off button self.butOff = Tkinter.Button(self, text = 'All Off', command = self.butOff_click) self.butOff['font'] = ("Arial", 20, "bold") self.butOff.grid(column = 0, row = 1, rowspan = 1, columnspan = 3, sticky = 'NSEW') # Set our grid layout, make the sliders taller than the button self.grid_columnconfigure(0, weight = 1) self.grid_columnconfigure(1, weight = 1) self.grid_columnconfigure(2, weight = 1) self.grid_rowconfigure(0, weight = 4) self.grid_rowconfigure(1, weight = 1) # Set the size and name of the dialog self.resizable(True, True) self.geometry('200x600') self.title('LedBorg Example GUI') # Setup the initial colour SetLedBorg(self.red, self.green, self.blue) # Called when the user closes the dialog def OnExit(self): # Turn the LedBorg off and end the program LedBorgOff() self.quit() # Called when sldRed is moved def sldRed_move(self, value): # Update the red value then set the colour again self.red = float(value) / LED_MAX SetLedBorg(self.red, self.green, self.blue) # Called when sldGreen is moved def sldGreen_move(self, value): # Update the green value then set the colour again self.green = float(value) / LED_MAX SetLedBorg(self.red, self.green, self.blue) # Called when sldBlue is moved def sldBlue_move(self, value): # Update the blue value then set the colour again self.blue = float(value) / LED_MAX SetLedBorg(self.red, self.green, self.blue) # Called when butOff is clicked def butOff_click(self): self.sldRed.set(0) self.sldGreen.set(0) self.sldBlue.set(0) # Create a copy of the LedBorg GUI and pass control to it app = LedBorg_tk(None) app.mainloop()