Python GUI Demo

From Advanced Projects Lab
Jump to: navigation, search

The following code was created to demonstrate how to create a Graphical User Interface (GUI) using the Python module Tkinter, as well as communicate with an Arduino using the module PySerial. The code will not work without these modules.

Python Code

"""A simple program to create a GUI to communicate with an Arduino and toggle an LED on and off.
Created to demonstrate how to use Python with tkinter to create a GUI.
- Colin Diehl
"""
from tkinter import * #import the modules for the GUI 
import serial #and the serial communications

PORT = "COM5"
ser = serial.Serial(PORT, 9600) #start the serial com w/ 9600 baudrate

class Application(Frame): #create the class for the GUI to run
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid() # create the grid on the GUI for the part to snap onto
        self.create_widgets() #the function to create the GUI parts

    def create_widgets(self): #define all GUI parts
       self.label = Label(self) #create a label
        self.label["text"] = "Use the button to toggle the LED on and off:" #set the text of the label
        self.label.grid( row = 0, column = 0)#snap the label to the top left of the grid

        self.button = Button(self) #create a button
        self.button["text"] = "LED: OFF" #set the original text of the button
        self.button["height"] = 2 #set the height of the button to 2 characters tall
        self.button["width"] = 12 #set the button width to 12 characters wide
        self.button["command"] = self.toggle #the function the button will run when pressed
        self.button.grid(row = 1, column = 0) #snap the button to the second row of the grid

        self.text = Text(self) #create a text box
        self.text["height"] = 2 #set the text box height to 2 characters
        self.text["width"] = 20 #set the text box width to 20 characters
        self.text.grid(row = 2, column = 0) #snap the text box to the third row of the grid

        self.end = Button(self) #create another button to end the program
        self.end["text"] = "END" #set the text on the button
        self.end["height"] = 2 #set the button height to 2 characters
        self.end["width"] = 12 #set the button width to 12 characters
        self.end["bg"] = "red" #set the background color of the button to red
        self.end["command"] = self.quit #the quit function the button will run
        self.end.grid(row = 2, column = 1, sticky = E) #snap the button to the right side of the third row, second column(

    def toggle(self): #define the function from the first button
        index_dict={"OFF": "ON" , "ON": "OFF"} #define a sequence that switches from off to on to off
        index[0] = index_dict[index[0]] #progresses through the sequence, toggling the inital string in the sequence
        self.button["text"] = "LED: " + str(index[0]) #updates the button text with the current state of the LED
        if index[0] == "OFF": #when the inital string is off
            ser.write(bytes("a", encoding="ascii")) #writes lowercase a into the serial com, Arduino reads and sets output low
            self.text.delete(0.0, END) #deletes the contents of the text box
            self.text.insert(0.0, "The LED is OFF") #inserts the string into the text box to display OFF state of the LED 
        if index[0] == "ON": #when the inital string is on
            ser.write(bytes("A", encoding="ascii")) #writes uppercase A into the serial com, Arduino reads and sets output high
            self.text.delete(0.0, END) #clears the text box
            self.text.insert(0.0, "The LED is ON") #inserts the string into the text box to display ON state of the LED

    def quit(self): #define the function to kill the program
        ser.write(bytes("a", encoding="ascii")) #write lowercase a into serial com, to make sure LED goes off when program ends
        ser.close() #close the serial communication
        root.destroy() #destroy the GUI

index = ["OFF"] #set the intial string in the sequence to off, LED starts off
root.title("GUI Demo") #title the GUI window
root = Tk() #these three lines start and run the GUI window in a loop
app = Application(root)
root.mainloop()
       

Arduino Code =

const int led = 12; // define led as the constant integer 12
// connect the positive end of the LED to pin 12 of the Arduino
char val; //define a variable val as a character

void setup(){
  Serial.begin(9600); // begin the serial communication with a baudrate of 9600
  pinMode(led, OUTPUT); //set led (pin 12) as a digital output
}

void loop(){
  if (Serial.available()){ //run only if there is serial communication available
    val = Serial.read(); //read the serial communication and set val as the read value
    if (val == 'A'){ // if val is equal to uppercase A
      digitalWrite(led, HIGH); // set the led pin to high, turn the led on
    }
    if (val == 'a'){ // if val is equal to lowercase a
      digitalWrite(led, LOW); // set the led pin to low, turn the led off
    }
  }
delay(100); //sleep for a tenth of a second before the loop begins again
}