通过在Raspberry中使用GPIO更改颜色小部件 [英] Change the color widget by using GPIO in Raspberry
问题描述
从Tkinter导入*
导入tkFont
导入Tkinter
导入RPi.GPIO为GPIO
GPIO.setmode GPIO.BCM)
GPIO.setup(10,GPIO.IN)
类PersamaanKuadrat:
def __init __(self,parent,title):
self.parent = parent
#self.parent.geometry(1280x560)
self.parent.title(title)
self.parent.protocol(WM_DELETE_WINDOW )
self.aturKomponen()
def aturKomponen(self):
self.customFont = tkFont。 Font(family =Helvetica,size = 18)
mainCanvas = Canvas(self.parent,width = 1300,height = 600,bg =grey)
mainCanvas.pack fill = BOTH,expand = YES)
self.kanvas = mainCanvas
self.btnCariAkar = Button(mainCanvas,text =ENTER!,
$ b command = exit)
self.btnCariAkar.place(relx = .25,depend = .25)
self.btn1 = Button(mainCanvas,padx = 30,pady =文本=A1,bg =白色)
self.btn1.place(relx = .65,depend = .25)
self.btn2 = Button(mainCanvas,padx = 30,pady = 30,text =A2,bg =white)
self.btn2.place(relx = .80,depend = .25)
self.btn3 = Button(mainCanvas,padx = 30,pady = 30,text =B1,bg =white)
self.btn3.place(relx = .65,reli = .55)
self.btn4 = Button(mainCanvas,padx = 30,pady = 30,text =B2,bg =white)
self.btn4.place(relx = .80,depend = $ 55
def atur(self):
inputValue = GPIO.input(10)
if(inputValue == True):
self.btn1。 configure(bg =yellow)
else:
self.btn1.configure(bg =red)
如果__name__ =='__main__':
root = Tk()
aplikasi = PersamaanKuadrat(root,T)
root.mainloop()
- 我想通过按下连接到GPIO引脚(引脚10)的按钮来更改btn1的颜色,例如,如果我按下按钮,btn1将有黄色,如果我不推它的颜色将是红色的。当我运行模块没有错误,程序工作。但是当我按下按钮,btn1的颜色没有改变。
- 如果我想添加定时器功能,例如30秒后,btn1的颜色将从黄色变回红色,是可能的吗?
- 我可以使用'和'这个程序。例如,如果我要使用2个按钮将其更改为绿色,应该怎么办?
需要帮助,这个树莓的新手,感谢:)
1)我想你需要定时器运行 atur()
来测试是否按下了按钮。现在程序不知道您按下按钮
在 __ init __()
和 atur()
:
self.parent.after(10,self.atur)
2)是的,可以使用 self.parent.after(milliseconds,function_name )
3)你的意思是两个按钮连接到GPIO?我认为是的。
EDIT: b
每隔500ms定时器改变 btn2
颜色的示例。
我认为现在程序应该更改 btn1
按下按钮颜色。 (未测试)
来自Tkinter import *
import tkFont
import Tkinter
import RPi .GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(10,GPIO.IN)
类PersamaanKuadrat:
def __init __(self,parent,title):
self.parent = parent
#self.parent.geometry(1280x560)
self.parent.title (title)
self.parent.protocol(WM_DELETE_WINDOW)
self.aturKomponen()
self.parent.after(10,self.atur)
self.parent.after(500,self.change_color)
def aturKomponen(self):
self.customFont = tkFont.Font family =Helvetica,size = 18)
mainCanvas = Canvas(self.parent,width = 1300,height = 600,bg =grey)
mainCanvas.pack BOTH,expand = YES)
self.kanvas = mainCanvas
self.btnCariAkar = Button(mainCanvas,text =ENTER!,
命令= exit)
self.btnCariAkar.place(relx = .25,reli = .25)
self.btn1 = Button(mainCanvas,padx = 30,pady = 30,text = a1,bg =white)
self.btn1.place(relx = .65,reli = .25)
self.btn2 = Button(mainCanvas,padx = pady = 30,text =A2,bg =white)
self.btn2.place(relx = .80,depend = .25)
self.btn3 = Button mainCanvas,padx = 30,pady = 30,text =B1,bg =white)
self.btn3.place(relx = .65,reli = .55)
self.btn4 = Button(mainCanvas,padx = 30,pady = 30,text =B2,bg =white)
self.btn4.place(relx = .80,depend =。 55)
def atur(self):
打印测试按钮
inputValue = GPIO.input(10)
if(inputValue == True) :
self.btn1.configure(bg =yellow)
else:
self.btn1.configure(bg =red)
self.parent.after ,self.atur)
def change_color(self):
printchange_color
if self.btn2 ['bg'] =='yellow':
self.btn2 ['bg'] ='red'
else:
self.btn2 ['bg'] ='yellow'
self.parent.after(500,self.change_color)
if __name__ =='__main__':
root = Tk()
aplikasi = PersamaanKuadrat(root,T)
$ b b root.mainloop()
from Tkinter import *
import tkFont
import Tkinter
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(10, GPIO.IN)
class PersamaanKuadrat:
def __init__(self, parent, title):
self.parent = parent
#self.parent.geometry("1280x560")
self.parent.title(title)
self.parent.protocol("WM_DELETE_WINDOW")
self.aturKomponen()
def aturKomponen(self):
self.customFont = tkFont.Font(family="Helvetica", size=18)
mainCanvas = Canvas(self.parent, width=1300, height=600, bg="grey")
mainCanvas.pack(fill=BOTH, expand=YES)
self.kanvas = mainCanvas
self.btnCariAkar = Button(mainCanvas, text="ENTER!",
command=exit)
self.btnCariAkar.place(relx=.25, rely=.25)
self.btn1= Button(mainCanvas, padx=30, pady=30, text="A1", bg="white")
self.btn1.place(relx=.65, rely=.25)
self.btn2 = Button(mainCanvas, padx=30, pady=30, text="A2", bg="white")
self.btn2.place(relx=.80, rely=.25)
self.btn3 = Button(mainCanvas, padx=30, pady=30, text="B1", bg="white")
self.btn3.place(relx=.65, rely=.55)
self.btn4 = Button(mainCanvas, padx=30, pady=30, text="B2", bg="white")
self.btn4.place(relx=.80, rely=.55)
def atur(self):
inputValue = GPIO.input(10)
if(inputValue == True):
self.btn1.configure(bg="yellow")
else:
self.btn1.configure(bg="red")
if __name__ == '__main__':
root = Tk()
aplikasi = PersamaanKuadrat(root, "T")
root.mainloop()
- I want change the color of btn1 by pressing push-button which connected to GPIO pin (pin 10), for example, if I push the button, the btn1 will have yellow color, if i dont push it the color will be red. when I run the module there is no error, and the program works. but when I pushed the push button, the btn1's color didn't change. I Can't find the mistake, can you all help me.
- If I want to add timer function, for example after 30 seconds, the color of btn1 will change from yellow back to red, is it possible?
- can I use 'and' for this program. for example if I want to use 2 buttons to change it to green, what should I do?
Need Help, because I'm a newbie for this raspberry thing, thanks :)
1) I think you need timer to run atur()
to test if push-button was pressed. Now program doesn't know that you pressed push-button
In __init__()
and in atur()
:
self.parent.after(10, self.atur)
2) Yes it is possible with self.parent.after(milliseconds, function_name)
3) Do you mean two push-button connected to GPIO ? I think yes.
EDIT:
Example with timer changing btn2
color every 500ms.
I think now program should change btn1
color by pressing push-button. (not tested)
from Tkinter import *
import tkFont
import Tkinter
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(10, GPIO.IN)
class PersamaanKuadrat:
def __init__(self, parent, title):
self.parent = parent
#self.parent.geometry("1280x560")
self.parent.title(title)
self.parent.protocol("WM_DELETE_WINDOW")
self.aturKomponen()
self.parent.after(10, self.atur)
self.parent.after(500, self.change_color)
def aturKomponen(self):
self.customFont = tkFont.Font(family="Helvetica", size=18)
mainCanvas = Canvas(self.parent, width=1300, height=600, bg="grey")
mainCanvas.pack(fill=BOTH, expand=YES)
self.kanvas = mainCanvas
self.btnCariAkar = Button(mainCanvas, text="ENTER!",
command=exit)
self.btnCariAkar.place(relx=.25, rely=.25)
self.btn1= Button(mainCanvas, padx=30, pady=30, text="A1", bg="white")
self.btn1.place(relx=.65, rely=.25)
self.btn2 = Button(mainCanvas, padx=30, pady=30, text="A2", bg="white")
self.btn2.place(relx=.80, rely=.25)
self.btn3 = Button(mainCanvas, padx=30, pady=30, text="B1", bg="white")
self.btn3.place(relx=.65, rely=.55)
self.btn4 = Button(mainCanvas, padx=30, pady=30, text="B2", bg="white")
self.btn4.place(relx=.80, rely=.55)
def atur(self):
print "test push-button"
inputValue = GPIO.input(10)
if(inputValue == True):
self.btn1.configure(bg="yellow")
else:
self.btn1.configure(bg="red")
self.parent.after(10, self.atur)
def change_color(self):
print "change_color"
if self.btn2['bg'] == 'yellow':
self.btn2['bg'] = 'red'
else:
self.btn2['bg'] = 'yellow'
self.parent.after(500, self.change_color)
if __name__ == '__main__':
root = Tk()
aplikasi = PersamaanKuadrat(root, "T")
root.mainloop()
这篇关于通过在Raspberry中使用GPIO更改颜色小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!