Включение / выключение светодиода при использовании Tkinter с использованием Arduino - PullRequest
0 голосов
/ 23 февраля 2019
enter code here

from tkinter import* 
import serial
import time
ArduinoUnoSerial = serial.Serial('COM3',9600)
print(ArduinoUnoSerial.readline())
#Create Serial port object called ArduinoUnoSerialData time.sleep(2)                                                             #wait for 2 secounds for the communication to get established
#read the serial data and print it as line 

print ("You have new message from Arduino")
one = '1'
one_byte = str.encode(one) #This will convert this into a binary code since the Arduino cannot read in strings.
zero = '0'
zero_byte = str.encode(zero)

def Set_Led():
    while 1:
        var = Set_Led()                          #get input from user             
    if (var == '1'):                                      #if the value is 1         
            ArduinoUnoSerial.write(one_byte)                  #send 1 to the arduino's Data code       
            print ("LED turned ON")         
            time.sleep(1)
    else:
        var = Set_Led()
        if (var == '0'):                                   #if the value is 0         
            ArduinoUnoSerial.write(zero_byte)               #send 0 to the arduino's Data code    
            print ("LED turned OFF")         
            time.sleep(1)

win = Tk()
win.title('Turn the LED ON/OFF')

Box_1 = Entry(win, width = 5)

Label = Label(win,text = 'How do you want the LED?').grid(column = 1, row = 3)

Box_1.grid(column = 2, row = 3)

Button1 = Button(win, text ='Okay', command = Set_Led)

Button1.grid(column = 2, row = 4)


win.mainloop()

У меня были проблемы с получением интергера, чтобы поместить его в мой блок ввода, чтобы сработала кнопка «ОК».Я получаю ошибки, такие как RecursionError: максимальная глубина рекурсии превышена.Я хочу иметь возможность создать одну функцию, которая будет включать / выключать светодиод, но я хотел знать, почему моя функция не допускает включение интергера, который должен быть размещен в сетке, при установке 1.Я попытался установить переменную для Box_1 и Button1, но я получил ошибки, такие как отсутствие позиционного аргумента.Любая помощь или советы помогут. Спасибо.

...