# 2f-RGB-LED-GUI.py
# use GUI sliders to control LEDs
from tkinter import *
from gpiozero import PWMLED
import time
# Configure the GPIO pins
red=PWMLED(24)
green=PWMLED(23)
blue=PWMLED(18)
# Start Pulse Width Modulation (PWM) on the red, green and blue channels
red.value = 1 # start at minimum brightness
green.value = 1 # value 0 to 1
blue.value = 1 # backwards because common anode
# group together all of the GUI code into a class called App
class App:
# this function gets called when the app is created
def __init__(self, master):
# A frame holds the various GUI controls
frame = Frame(master)
frame.pack()
# Create the labels and position them in a grid layout
Label(frame, text='Red').grid(row=0, column=0)
Label(frame, text='Green').grid(row=1, column=0)
Label(frame, text='Blue').grid(row=2, column=0)
# Create the sliders and position them in a grid layout
# the 'command' attribute specifys a method to call when
# a slider is moved
scaleRed = Scale(frame, from_=0, to=100,
orient=HORIZONTAL, command=self.updateRed)
scaleRed.grid(row=0, column=1)
scaleGreen = Scale(frame, from_=0, to=100,
orient=HORIZONTAL, command=self.updateGreen)
scaleGreen.grid(row=1, column=1)
scaleBlue = Scale(frame, from_=0, to=100,
orient=HORIZONTAL, command=self.updateBlue)
scaleBlue.grid(row=2, column=1)
# These methods called whenever a slider moves
def updateRed(self, duty):
# change the led brightness to match the slider
red.value = 1-float(int(duty) / 100)
def updateGreen(self, duty):
green.value = 1-float(int(duty) / 100)
def updateBlue(self, duty):
blue.value = 1-float(int(duty) / 100)
# Set the GUI running, give the window a title, size and position
root = Tk()
root.wm_title('RGB LED Control')
app = App(root)
root.geometry("200x150+0+0")
try:
root.mainloop()
finally:
print("Cleaning up")
red.value = 1 # turn off LED when exiting
green.value = 1
blue.value = 1
# use GUI sliders to control LEDs
from tkinter import *
from gpiozero import PWMLED
import time
# Configure the GPIO pins
red=PWMLED(24)
green=PWMLED(23)
blue=PWMLED(18)
# Start Pulse Width Modulation (PWM) on the red, green and blue channels
red.value = 1 # start at minimum brightness
green.value = 1 # value 0 to 1
blue.value = 1 # backwards because common anode
# group together all of the GUI code into a class called App
class App:
# this function gets called when the app is created
def __init__(self, master):
# A frame holds the various GUI controls
frame = Frame(master)
frame.pack()
# Create the labels and position them in a grid layout
Label(frame, text='Red').grid(row=0, column=0)
Label(frame, text='Green').grid(row=1, column=0)
Label(frame, text='Blue').grid(row=2, column=0)
# Create the sliders and position them in a grid layout
# the 'command' attribute specifys a method to call when
# a slider is moved
scaleRed = Scale(frame, from_=0, to=100,
orient=HORIZONTAL, command=self.updateRed)
scaleRed.grid(row=0, column=1)
scaleGreen = Scale(frame, from_=0, to=100,
orient=HORIZONTAL, command=self.updateGreen)
scaleGreen.grid(row=1, column=1)
scaleBlue = Scale(frame, from_=0, to=100,
orient=HORIZONTAL, command=self.updateBlue)
scaleBlue.grid(row=2, column=1)
# These methods called whenever a slider moves
def updateRed(self, duty):
# change the led brightness to match the slider
red.value = 1-float(int(duty) / 100)
def updateGreen(self, duty):
green.value = 1-float(int(duty) / 100)
def updateBlue(self, duty):
blue.value = 1-float(int(duty) / 100)
# Set the GUI running, give the window a title, size and position
root = Tk()
root.wm_title('RGB LED Control')
app = App(root)
root.geometry("200x150+0+0")
try:
root.mainloop()
finally:
print("Cleaning up")
red.value = 1 # turn off LED when exiting
green.value = 1
blue.value = 1