<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://newjune.uoregon.edu/mediawiki/index.php?action=history&amp;feed=atom&amp;title=Python_GUI_Demo</id>
	<title>Python GUI Demo - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://newjune.uoregon.edu/mediawiki/index.php?action=history&amp;feed=atom&amp;title=Python_GUI_Demo"/>
	<link rel="alternate" type="text/html" href="https://newjune.uoregon.edu/mediawiki/index.php?title=Python_GUI_Demo&amp;action=history"/>
	<updated>2026-05-11T02:25:45Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.3</generator>
	<entry>
		<id>https://newjune.uoregon.edu/mediawiki/index.php?title=Python_GUI_Demo&amp;diff=327&amp;oldid=prev</id>
		<title>Wikiuser: Created page with &quot;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 ...&quot;</title>
		<link rel="alternate" type="text/html" href="https://newjune.uoregon.edu/mediawiki/index.php?title=Python_GUI_Demo&amp;diff=327&amp;oldid=prev"/>
		<updated>2014-03-21T22:35:55Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;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 ...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;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.&lt;br /&gt;
&lt;br /&gt;
== Python Code ==&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;&amp;quot;&amp;quot;A simple program to create a GUI to communicate with an Arduino and toggle an LED on and off.&lt;br /&gt;
 Created to demonstrate how to use Python with tkinter to create a GUI.&lt;br /&gt;
 - Colin Diehl&lt;br /&gt;
 &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
 from tkinter import * #import the modules for the GUI &lt;br /&gt;
 import serial #and the serial communications&lt;br /&gt;
 &lt;br /&gt;
 PORT = &amp;quot;COM5&amp;quot;&lt;br /&gt;
 ser = serial.Serial(PORT, 9600) #start the serial com w/ 9600 baudrate&lt;br /&gt;
 &lt;br /&gt;
 class Application(Frame): #create the class for the GUI to run&lt;br /&gt;
     def __init__(self, master):&lt;br /&gt;
         Frame.__init__(self, master)&lt;br /&gt;
         self.grid() # create the grid on the GUI for the part to snap onto&lt;br /&gt;
         self.create_widgets() #the function to create the GUI parts&lt;br /&gt;
 &lt;br /&gt;
     def create_widgets(self): #define all GUI parts&lt;br /&gt;
        self.label = Label(self) #create a label&lt;br /&gt;
         self.label[&amp;quot;text&amp;quot;] = &amp;quot;Use the button to toggle the LED on and off:&amp;quot; #set the text of the label&lt;br /&gt;
         self.label.grid( row = 0, column = 0)#snap the label to the top left of the grid&lt;br /&gt;
 &lt;br /&gt;
         self.button = Button(self) #create a button&lt;br /&gt;
         self.button[&amp;quot;text&amp;quot;] = &amp;quot;LED: OFF&amp;quot; #set the original text of the button&lt;br /&gt;
         self.button[&amp;quot;height&amp;quot;] = 2 #set the height of the button to 2 characters tall&lt;br /&gt;
         self.button[&amp;quot;width&amp;quot;] = 12 #set the button width to 12 characters wide&lt;br /&gt;
         self.button[&amp;quot;command&amp;quot;] = self.toggle #the function the button will run when pressed&lt;br /&gt;
         self.button.grid(row = 1, column = 0) #snap the button to the second row of the grid&lt;br /&gt;
 &lt;br /&gt;
         self.text = Text(self) #create a text box&lt;br /&gt;
         self.text[&amp;quot;height&amp;quot;] = 2 #set the text box height to 2 characters&lt;br /&gt;
         self.text[&amp;quot;width&amp;quot;] = 20 #set the text box width to 20 characters&lt;br /&gt;
         self.text.grid(row = 2, column = 0) #snap the text box to the third row of the grid&lt;br /&gt;
 &lt;br /&gt;
         self.end = Button(self) #create another button to end the program&lt;br /&gt;
         self.end[&amp;quot;text&amp;quot;] = &amp;quot;END&amp;quot; #set the text on the button&lt;br /&gt;
         self.end[&amp;quot;height&amp;quot;] = 2 #set the button height to 2 characters&lt;br /&gt;
         self.end[&amp;quot;width&amp;quot;] = 12 #set the button width to 12 characters&lt;br /&gt;
         self.end[&amp;quot;bg&amp;quot;] = &amp;quot;red&amp;quot; #set the background color of the button to red&lt;br /&gt;
         self.end[&amp;quot;command&amp;quot;] = self.quit #the quit function the button will run&lt;br /&gt;
         self.end.grid(row = 2, column = 1, sticky = E) #snap the button to the right side of the third row, second column(&lt;br /&gt;
 &lt;br /&gt;
     def toggle(self): #define the function from the first button&lt;br /&gt;
         index_dict={&amp;quot;OFF&amp;quot;: &amp;quot;ON&amp;quot; , &amp;quot;ON&amp;quot;: &amp;quot;OFF&amp;quot;} #define a sequence that switches from off to on to off&lt;br /&gt;
         index[0] = index_dict[index[0]] #progresses through the sequence, toggling the inital string in the sequence&lt;br /&gt;
         self.button[&amp;quot;text&amp;quot;] = &amp;quot;LED: &amp;quot; + str(index[0]) #updates the button text with the current state of the LED&lt;br /&gt;
         if index[0] == &amp;quot;OFF&amp;quot;: #when the inital string is off&lt;br /&gt;
             ser.write(bytes(&amp;quot;a&amp;quot;, encoding=&amp;quot;ascii&amp;quot;)) #writes lowercase a into the serial com, Arduino reads and sets output low&lt;br /&gt;
             self.text.delete(0.0, END) #deletes the contents of the text box&lt;br /&gt;
             self.text.insert(0.0, &amp;quot;The LED is OFF&amp;quot;) #inserts the string into the text box to display OFF state of the LED &lt;br /&gt;
         if index[0] == &amp;quot;ON&amp;quot;: #when the inital string is on&lt;br /&gt;
             ser.write(bytes(&amp;quot;A&amp;quot;, encoding=&amp;quot;ascii&amp;quot;)) #writes uppercase A into the serial com, Arduino reads and sets output high&lt;br /&gt;
             self.text.delete(0.0, END) #clears the text box&lt;br /&gt;
             self.text.insert(0.0, &amp;quot;The LED is ON&amp;quot;) #inserts the string into the text box to display ON state of the LED&lt;br /&gt;
 &lt;br /&gt;
     def quit(self): #define the function to kill the program&lt;br /&gt;
         ser.write(bytes(&amp;quot;a&amp;quot;, encoding=&amp;quot;ascii&amp;quot;)) #write lowercase a into serial com, to make sure LED goes off when program ends&lt;br /&gt;
         ser.close() #close the serial communication&lt;br /&gt;
         root.destroy() #destroy the GUI&lt;br /&gt;
 &lt;br /&gt;
 index = [&amp;quot;OFF&amp;quot;] #set the intial string in the sequence to off, LED starts off&lt;br /&gt;
 root.title(&amp;quot;GUI Demo&amp;quot;) #title the GUI window&lt;br /&gt;
 root = Tk() #these three lines start and run the GUI window in a loop&lt;br /&gt;
 app = Application(root)&lt;br /&gt;
 root.mainloop()&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
= Arduino Code ==&lt;br /&gt;
&lt;br /&gt;
 const int led = 12; // define led as the constant integer 12&lt;br /&gt;
 // connect the positive end of the LED to pin 12 of the Arduino&lt;br /&gt;
 char val; //define a variable val as a character&lt;br /&gt;
 &lt;br /&gt;
 void setup(){&lt;br /&gt;
   Serial.begin(9600); // begin the serial communication with a baudrate of 9600&lt;br /&gt;
   pinMode(led, OUTPUT); //set led (pin 12) as a digital output&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void loop(){&lt;br /&gt;
   if (Serial.available()){ //run only if there is serial communication available&lt;br /&gt;
     val = Serial.read(); //read the serial communication and set val as the read value&lt;br /&gt;
     if (val == &amp;#039;A&amp;#039;){ // if val is equal to uppercase A&lt;br /&gt;
       digitalWrite(led, HIGH); // set the led pin to high, turn the led on&lt;br /&gt;
     }&lt;br /&gt;
     if (val == &amp;#039;a&amp;#039;){ // if val is equal to lowercase a&lt;br /&gt;
       digitalWrite(led, LOW); // set the led pin to low, turn the led off&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 delay(100); //sleep for a tenth of a second before the loop begins again&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Wikiuser</name></author>
	</entry>
</feed>