Tkinter 滑块和 Raspberry Pi 4 GPIO 接口

Posted

技术标签:

【中文标题】Tkinter 滑块和 Raspberry Pi 4 GPIO 接口【英文标题】:Tkinter Sliders and Raspberry Pi 4 GPIO interfacing 【发布时间】:2020-04-10 09:44:31 【问题描述】:

我是 Python 初学者,但对使用 Tkinter 学习 UI 设计以及通过官方触摸屏控制屏幕上的内容非常感兴趣。基本和有趣的项目是最好的方法。所以我决定在屏幕上放 3 个滑块,每个滑块代表一种颜色,红色、绿色和蓝色。我正在尝试根据这 3 个滑块的反馈点亮我的 RGB LED 模块。所以它们应该与 GPIO 引脚接口。

但我不知道如何将滑块的值作为 PWM 信号编号“0-255”传输到 GPIO 引脚。

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()

虽然 GUI 运行良好且没有错误,但我在 RGB LED 上看不到任何变化,当我检查运行 phyton 代码的 linux 终端时,我看到了这个错误:

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)

谁能告诉我这里发生了什么以及如何从滑块中获取值并将其作为 PWM 信号传输到我的 GPIO 引脚?

【问题讨论】:

RGB LED 如何准确连接到电路板?有什么原理图要分享吗? 【参考方案1】:

我不知道您是否还需要遮阳篷,但前几天我遇到了同样的问题,我解决问题的方法是使用不同的 gpio 模式。 以下脚本有效:

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()

显然,您需要将引脚更改为您使用的引脚。 我还举了一个按钮的例子,它破坏了窗口并重置了引脚。 希望这对您或其他有相同问题的人有所帮助。

【讨论】:

【参考方案2】:

对于 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()

【讨论】:

以上是关于Tkinter 滑块和 Raspberry Pi 4 GPIO 接口的主要内容,如果未能解决你的问题,请参考以下文章

用于Raspberry Pi的Tkinter / Canvas-based kiosk-like程序

将串行从 Raspberry 传递到 Arduino USB HID

能不能在 raspberry pi 上实现这样的网络设置

javascript [滑块]控件#js上的渐进增强滑块和svg图标

树莓派(Raspberry Pi)4B无界面安装 Raspberry Pi 系统篇

树莓派(Raspberry Pi)4B无界面安装 Raspberry Pi 系统篇