Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Tkinter widgets driving Arduino uno output pins
#1
Program is partial working. I am able to turn pin 13 on and off. What I am trying to do is have "true" feed back when the pin changes state. I am trying to send pin 13 status ("1" or "0") to a tkinter label widget in python (below the "Exit" button) If I run using Arduino serial monitor everything works correctly. I get the text "Hello" and when I push the on button I get a "1" (no quotes) and when I push the off button I get a "0" (no quotes). Pushing Exit quits the program. Since I wrote this I am now getting "PY_VAR1 in my label field. I believe this is a python problem, I will also post to Arduino. Thanks for you help in advance

Jim


Python 3.6
[python]
----------------------------------------
#! /usr/bin/env python

import serial
import time
from tkinter import *


ArduinoSerial = serial.Serial('com3', 9600, timeout=.1)
time.sleep(2)
global status1

def led_on():
ArduinoSerial.write(b'1')
status1 = (ArduinoSerial.readline() .decode('utf-8').strip())
print(status1)

def led_off():
ArduinoSerial.write(b'0')
status1 = (ArduinoSerial.readline().decode('utf-8').strip())
print(status1)

def led_Exit():
ArduinoSerial.write(b'0')
status1 = "Quit"
print(status1)
quit()

root = Tk()
status1 = StringVar()
root.title("Arduino Push Buttons")
btn1 = Button(root, text="Led on", command=led_on)
btn2 = Button(root, text="Led off", command=led_off)
btn3 = Button(root, text="Exit", command=led_Exit)
msg1 = Label (root, textvariable = status1, relief=RAISED,width = 20)
btn1.pack()
btn2.pack()
btn3.pack()
msg1.pack()
status1.set(StringVar())
root.mainloop()

/python]
Python 3.6
-----------------------------------------------------------

Arduino 1.6.12

int data;

void setup()
{
Serial.begin(9600); //initialize serial COM at 9600 baudrate
pinMode(13, OUTPUT); //make the LED pin (13) as output
digitalWrite (13, LOW);
}
void loop()
{
if (Serial.available()> 0)
{
data = Serial.read();
}
if (data == '1')
{
digitalWrite (13, HIGH);
Serial.println(digitalRead(13));
data = '2';
}
else if (data == '0')
{
digitalWrite (13, LOW);
Serial.println(digitalRead(13));
data = '2';
}
}

----------------------------------------------
Quote
#2
please re-post using code tags! see BBCODE
Quote
#3
Program is partial working. I am able to turn pin 13 on and off. What I am trying to do is have "true" feed back when the pin changes state. I am trying to send pin 13 status ("1" or "0") to a tkinter label widget in python (below the "Exit" button) If I run using serial monitor everything works correctly. I get the text "Hello" and when I push the on button I get a "1" (no quotes) and when I push the off button I get a "0" (no quotes). Pushing Exit quits the program. Since I wrote this I am getting "PY_VAR1 in my label field. I believe this is a python problem, I will also post to Arduino. Thanks for you help in advance

Jim

#! /usr/bin/env python

import serial
import time
from tkinter import *


ArduinoSerial = serial.Serial('com3', 9600, timeout=.1)
time.sleep(2)
global status1

def led_on():
    ArduinoSerial.write(b'1')
    status1 = (ArduinoSerial.readline() .decode('utf-8').strip())
    print(status1)

def led_off():
    ArduinoSerial.write(b'0')
    status1 = (ArduinoSerial.readline().decode('utf-8').strip())
    print(status1)

def led_Exit():
    ArduinoSerial.write(b'0')
    status1 = "Quit"
    print(status1)
    quit()

root = Tk()
status1 = StringVar()
root.title("Arduino Push Buttons")
btn1 = Button(root, text="Led on", command=led_on)
btn2 = Button(root, text="Led off", command=led_off)
btn3 = Button(root, text="Exit", command=led_Exit)
msg1 = Label (root, textvariable = status1, relief=RAISED,width = 20)
btn1.pack()
btn2.pack()
btn3.pack()
msg1.pack()
status1.set(StringVar())
root.mainloop()


  1. 
    ARDUINO CODE
    
    int data;
    
    void setup() 
    { 
      Serial.begin(9600); //initialize serial COM at 9600 baudrate
      pinMode(13, OUTPUT); //make the LED pin (13) as output
      digitalWrite (13, LOW);
    } 
    void loop() 
    {
      if (Serial.available()> 0)
      {
        data = Serial.read();
      }
       if (data == '1')
        {
        digitalWrite (13, HIGH); 
        Serial.println(digitalRead(13));
        data = '2';
        }
       else if (data == '0')
        {
        digitalWrite (13, LOW);
        Serial.println(digitalRead(13));
        data = '2';
        }
    }
    ARDUINO CODE
    
    
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Tkinter/Label/Arduino/ if Else condition emrebt39 1 221 Oct-20-2017, 11:44 AM
Last Post: Larz60+
  [Tkinter] Widgets position antoniojescobar 2 243 Sep-29-2017, 01:17 PM
Last Post: antoniojescobar
  Tkinter widgets not appearing (3.x) RSAngryfiend 1 332 Jul-05-2017, 09:13 PM
Last Post: nilamo
  Using 3rd party QT widgets in Python Standard_user 4 1,471 May-11-2017, 02:17 PM
Last Post: Standard_user

Forum Jump:


Users browsing this thread: 1 Guest(s)