简体   繁体   中英

Tkinter Sliders and Raspberry Pi 4 GPIO interfacing

I'm Python beginner but quite interested in learning UI design with Tkinter and control stuff on the screen via official touch screen. Basic and fun projects is the best way to do it. So I decided to put 3 sliders on the screen, each represents a color, red, green and blue. And I'm trying to light up my RGB led module based on the feedback of those 3 sliders. So they should interface with GPIO pins.

在此处输入图像描述

But I do not know how to transfer the value of the sliders as PWM signal number '0-255' to GPIO pins.

from Tkinter import *
import tkFont
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)


win = Tk()

myFont = tkFont.Font(family = 'Helvetica', size = 36, weight = 'bold')

def exitProgram():
        GPIO.cleanup()
    win.quit()  

def setColor():
    val = int(red_scaleget())
    GPIO.output(17,val)
    val2 = int(green_scaleget())
    GPIO.output(27,val2)
    val3 = int(blue_scaleget())
    GPIO.output(22,val3)


win.title("RGB COLOR CHANGER")
win.geometry('800x480')

exitButton  = Button(win, text = "Exit", font = myFont, command = exitProgram, height =2 , width = 6)
exitButton.pack(side = BOTTOM)

w = Label(win, text="RED")
w.place(x=375, y=5)

w = Label(win, text="BLUE")
w.place(x=381, y=90)

w = Label(win, text="GREEN")
w.place(x=378, y=190)


red_scale = Scale(win, from_=0, to=255,orient=HORIZONTAL,length=700,bg='red',command = setColor)
red_scale.place(x=50, y=30)

green_scale= Scale(win, from_=0, to=255,orient=HORIZONTAL,length=700,bg='green',command = setColor)
green_scale.place(x=50,y=120)

blue_scale= Scale(win, from_=0, to=255,orient=HORIZONTAL,length=700,bg='blue',command = setColor)
blue_scale.place(x=50, y=220)



mainloop()

While the GUI runs just fine without errors, I cannot see any change on my RGB led and when I check the linux terminal, where i ran the phyton code, i see this error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1550, in __call__
    return self.func(*args)
TypeError: setColor() takes no arguments (1 given)

Can anyone tell me what's going on here and how do I get the value from sliders and transfer it my GPIO pins as PWM signal?

i dont know if you still need the awnser but i had the same problem the other day and the way i fixed the problem is by using a different gpio mode. following script works:

def white(value):
    print(value)
    pi=pigpio.pi()
    pi.set_PWM_dutycycle(21, value)
    pi.stop()

def winCleanup():
    pi=pigpio.pi()
    pi.set_PWM_dutycycle(19, 0)
    pi.set_PWM_dutycycle(13, 0)
    pi.set_PWM_dutycycle(20, 0)
    pi.set_PWM_dutycycle(21, 0)
    pi.stop()
    win.destroy()

White = Scale(win,sliderlength=15,showvalue=0, 
sliderrelief=FLAT,troughcolor='white',width=10, from_=0, to=255, orient=HORIZONTAL, 
bg= 
'yellow',  bd=3, font=Font, label="Warm White",length=1670, command=white, 
relief=SOLID) 
White.grid()

obviously youll need to change the pins to the ones your using. i also gave a example for a button that destroys the window and resets the pins. hope this helps either you or others with the same questions.

For python 3.7.3

from tkinter import *

import RPi.GPIO as GPIO

from gpiozero import PWMLED

RED = PWMLED(22)

GREEN = PWMLED(27)

BLUE = PWMLED(17)

win = Tk()

myFont = font.Font(family = 'Helvetica', size = 36, weight = 'bold')

def exitProgram():

GPIO.cleanup()

win.destroy()  

def setColor(win):

val = int(red_scale.get())

RED.value = val / 255

val2 = int(green_scale.get())

GREEN.value = val2 / 255

val3 = int(blue_scale.get())

BLUE.value = val3 / 255

win.title("RGB COLOR CHANGER") win.geometry('800x480')

exitButton = Button(win, text = "Exit", font = myFont, command = exitProgram, height =2, width = 6) exitButton.pack(side = BOTTOM)

w = Label(win, text="RED") w.place(x=375, y=5)

w = Label(win, text="BLUE") w.place(x=381, y=90)

w = Label(win, text="GREEN") w.place(x=378, y=190)

red_scale = Scale(win, from_=0, to=255,orient=HORIZONTAL,length=700,bg='red',command = setColor) red_scale.place(x=50, y=30)

green_scale= Scale(win, from_=0, to=255,orient=HORIZONTAL,length=700,bg='green',command = setColor) green_scale.place(x=50,y=120)

blue_scale= Scale(win, from_=0, to=255,orient=HORIZONTAL,length=700,bg='blue',command = setColor) blue_scale.place(x=50, y=220)

mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM