Change the color widget by using GPIO in Raspberry
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 tkFontimport Tkinterimport RPi.GPIO as GPIOGPIO.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()