Control Arduino LED using Python with GUI

This example demonstrate how to control Arduino LED using Python with GUI, using tkinter library.


#tkinter for Python 3.x
#Tkinter for Python 2.x

import serial
import time
import tkinter

def quit():
global tkTop
tkTop.destroy()

def setCheckButtonText():
if varCheckButton.get():
varLabel.set("LED ON")
ser.write(bytes('H', 'UTF-8'))
else:
varLabel.set("LED OFF")
ser.write(bytes('L', 'UTF-8'))

ser = serial.Serial('/dev/ttyACM1', 9600)
print("Reset Arduino")
time.sleep(3)
ser.write(bytes('L', 'UTF-8'))

tkTop = tkinter.Tk()
tkTop.geometry('300x200')

varLabel = tkinter.StringVar()
tkLabel = tkinter.Label(textvariable=varLabel)
tkLabel.pack()

varCheckButton = tkinter.IntVar()
tkCheckButton = tkinter.Checkbutton(
tkTop,
text="Control Arduino LED",
variable=varCheckButton,
command=setCheckButtonText)
tkCheckButton.pack(anchor=tkinter.CENTER)

tkButtonQuit = tkinter.Button(
tkTop,
text="Quit",
command=quit)
tkButtonQuit.pack()

tkinter.mainloop()

For the code in Arduino side, refer to the post "Communicate with Arduino in Python programmatically".

Comments

Popular posts from this blog

How to Remove �Powered by Blogger� attribution From Blogspot Blog?

Fritzing: an Electronic Design Automation software