Python 与GUI的通信循环

Python 与GUI的通信循环,python,tkinter,arduino,pyserial,raspberry-pi3,Python,Tkinter,Arduino,Pyserial,Raspberry Pi3,我正在用Python为一台机器制作GUI。我的python程序运行在Raspberry Pi 3上,它通过串行方式与Arduino UNO通信 我制定了一个协议;arduino发送一个“1”。树莓知道“1”代表Run/Stop,并返回RunStop变量的值(0或1)。之后,arduino发送一个“2”。树莓知道它代表速度值,并发回速度变量(介于1和4之间) 我已经完成了以下两个程序,但我不知道如何在GUI中实现它。因为当我将它添加到root.mainloop()之前时,GUI不会启动。当我把它放

我正在用Python为一台机器制作GUI。我的python程序运行在Raspberry Pi 3上,它通过串行方式与Arduino UNO通信

我制定了一个协议;arduino发送一个“1”。树莓知道“1”代表Run/Stop,并返回RunStop变量的值(0或1)。之后,arduino发送一个“2”。树莓知道它代表速度值,并发回速度变量(介于1和4之间)

我已经完成了以下两个程序,但我不知道如何在GUI中实现它。因为当我将它添加到root.mainloop()之前时,GUI不会启动。当我把它放在后面时,while循环永远不会开始

Arduino代码:

int RunStop, Speed = 0;

void setup() {

  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
}


void loop() {
  Serial.write(1);
  delay(2000);
  RunStop = Serial.read(); 
  if (RunStop == 1) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }
  delay(2000);


  Serial.write(2);
  delay(2000);
  Speed = Serial.read(); 
  if (Speed == 3) {
    digitalWrite(12, HIGH);
  } else {
    digitalWrite(12, LOW);
  }
  delay(2000);
}
Python通信代码:

import time
import serial

# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
    port='/dev/ttyAMA0',
    baudrate=9600,
)

ser.isOpen()

##Status variabelen

RunStop = b'\x01'
Speed = b'\x03'

while 1:
uartIn = ser.read()
print (uartIn)
time.sleep(1)
if uartIn == b'\x01':
    ser.write(RunStop)
    print ("Run/Stop Register")
elif uartIn == b'\x02':
    ser.write(Speed)
    print ("Speed Register")
else:
    print ("No Register Requested")
GUI代码:

from tkinter import *
import time
import serial

# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
    port='/dev/ttyAMA0',
    baudrate=9600,
    parity=serial.PARITY_ODD,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)

ser.isOpen()

root = Tk()
root.title("Cutter")

## Layout







label_1 = Label(root, text="Pieces")
label_2 = Label(root, text="Length")
entry_1 = Entry(root, width=9)

entry_2 = Entry(root, width=9)


label_1.grid(row=3, sticky=W, padx=(10))
label_2.grid(row=4, sticky=W, padx=(10))
entry_1.grid(row=3, column=1, pady=(10,10))
entry_2.grid(row=4, column=1, pady=(10,15))


runStop = b'\x00'
Speed = b'\x01'
Pieces = b'\x00'
Length = b'\x00'


##pieces OK button function
def piecesOKphase():
    x = entry_1.get()
    print("pieces: " + x)

##length OK button function
def lengthOKphase():
    x = entry_2.get()
    print("length: " + x)

def runPhase():
    global runStop
    runStop = b'\x01'

def stopPhase():
    global runStop
    runStop = b'\x00'





#OK and RUN / STOP buttons
piecesOK = Button(root, text="OK", command=piecesOKphase)
piecesOK.grid(row=3, column=2)

lengthOK = Button(root, text="OK", command=lengthOKphase)
lengthOK.grid(row=4, column=2)

runButton = Button(root, text="Run", width=6, bg='#58FA58', command=runPhase)
stopButton = Button(root, text="Stop", width=6, bg='#FA5858', command=stopPhase)

runButton.grid(row=0, column=0,padx=(10,10), pady=(10,10))
stopButton.grid(row=0, column=2)



##speed container

CurrentSpeed = 1




def delSpeed():
    global CurrentSpeed
    global speedLabel

    if CurrentSpeed > 1:
        CurrentSpeed -= 1
        speedLabel.config(text=str(CurrentSpeed))
        speedLabel.config(text=CurrentSpeed)

    else:
        pass

def addSpeed():
    global CurrentSpeed

    if CurrentSpeed < 4:
        CurrentSpeed += 1
        speedLabel.config(text=CurrentSpeed)

    else:
        pass


speedMin = Button(root, text="Speed -", width=6, command=delSpeed)

speedLabel = Label(root, text=CurrentSpeed, font=20)
speedLabel.grid(row=1, column=1, padx=(10))

speedPlus = Button(root, text="Speed +", width=6, command=addSpeed)

speedMin.grid(row=1, column=0)
speedPlus.grid(row=1, column=2)


#Number keyboard functions

def B1phase():
    try:
        root.focus_get().insert(END, "1")
    except AttributeError:
        pass
def B2phase():
    try:
        root.focus_get().insert(END, "2")
    except AttributeError:
        pass
def B3phase():
    try:
        root.focus_get().insert(END, "3")
    except AttributeError:
        pass
def B4phase():
    try:
        root.focus_get().insert(END, "4")
    except AttributeError:
        pass
def B5phase():
    try:
        root.focus_get().insert(END, "5")
    except AttributeError:
        pass
def B6phase():
    try:
        root.focus_get().insert(END, "6")
    except AttributeError:
        pass
def B7phase():
    try:
        root.focus_get().insert(END, "7")
    except AttributeError:
        pass
def B8phase():
    try:
        root.focus_get().insert(END, "8")
    except AttributeError:
        pass
def B9phase():
    try:
        root.focus_get().insert(END, "9")
    except AttributeError:
        pass
def B0phase():
    try:
        root.focus_get().insert(END, "0")
    except AttributeError:
        pass
def clearPhase():
    try:
        root.focus_get().delete(0, 'end')
    except AttributeError:
        pass


## Number keyboard buttons
B1 = Button(root, text="1", width=6, command=B1phase)
B2 = Button(root, text="2", width=6, command=B2phase)
B3 = Button(root, text="3", width=6, command=B3phase)
B4 = Button(root, text="4", width=6, command=B4phase)
B5 = Button(root, text="5", width=6, command=B5phase)
B6 = Button(root, text="6", width=6, command=B6phase)
B7 = Button(root, text="7", width=6, command=B7phase)
B8 = Button(root, text="8", width=6, command=B8phase)
B9 = Button(root, text="9", width=6, command=B9phase)
B0 = Button(root, text="0", width=6, command=B0phase)
clearButton = Button(root, text="Clear", width=6, bg='#FA5858', command=clearPhase)


B1.grid(row=5, column=0)
B2.grid(row=5, column=1)
B3.grid(row=5, column=2, padx=(10,10))
B4.grid(row=6, column=0)
B5.grid(row=6, column=1)
B6.grid(row=6, column=2, padx=(10,10))
B7.grid(row=7, column=0)
B8.grid(row=7, column=1)
B9.grid(row=7, column=2, padx=(10,10))
B0.grid(row=8, column=1)
clearButton.grid(row=8, column=0)



## Manual

label_4 = Label(root, text="")
label_4.grid(row=2, sticky=W)
label_3 = Label(root, text="Manual")
label_3.grid(row=9, sticky=W, padx=(10), pady=(10,10))

manualCut = Button(root, text="Cut", width=4)
manualCut.grid(row=10, column=0, pady=(1,15))

manualFeed = Button(root, text="Feed", width=4)
manualFeed.grid(row=10, column=1, pady=(1,15))


# Test function
def testPhase():

    print (CurrentSpeed)

manualTest = Button(root, text="Test", width=4, command=testPhase)
manualTest.grid(row=10, column=2, pady=(1,15))




## OTHER


root.mainloop()
从tkinter导入*
导入时间
导入序列号
#配置串行连接(所连接设备的参数不同)
ser=串行。串行(
端口='/dev/ttyam0',
波特率=9600,
奇偶校验=串行。奇偶校验,
停止位=串行。停止位\u一,
bytesize=串行8比特
)
伊索彭爵士()
root=Tk()
根标题(“切割器”)
##布局
标签\u 1=标签(根,text=“件”)
label_2=标签(根,text=“长度”)
条目_1=条目(根,宽度=9)
入口2=入口(根,宽度=9)
标签_1.网格(行=3,粘性=W,padx=(10))
标签2.网格(行=4,粘性=W,padx=(10))
条目1.网格(行=3,列=1,pady=(10,10))
条目2.网格(行=4,列=1,pady=(10,15))
runStop=b'\x00'
速度=b'\x01'
件数=b'\x00'
长度=b'\x00'
##单件OK按钮功能
def piecesOKphase():
x=条目_1.get()
打印(“件:+x)
##长度确定按钮功能
def lengthOKphase():
x=条目_2.get()
打印(“长度:+x)
def runPhase():
全局运行停止
runStop=b'\x01'
def stopPhase():
全局运行停止
runStop=b'\x00'
#确定和运行/停止按钮
piecesOK=按钮(根,text=“OK”,命令=piecesOKphase)
计件网格(行=3,列=2)
lengthOK=按钮(root,text=“OK”,command=lengthOKphase)
长网格(行=4,列=2)
runButton=Button(root,text=“Run”,width=6,bg='#58FA58',command=runPhase)
stopButton=按钮(root,text=“Stop”,width=6,bg='#FA5858',command=stopHase)
grid(行=0,列=0,padx=(10,10),pady=(10,10))
stopButton.grid(行=0,列=2)
##快速集装箱
当前速度=1
def delSpeed():
全局流速
全球速度标签
如果当前速度>1:
当前速度-=1
speedLabel.config(text=str(CurrentSpeed))
speedLabel.config(文本=当前速度)
其他:
通过
def addSpeed():
全局流速
如果当前速度<4:
当前速度+=1
speedLabel.config(文本=当前速度)
其他:
通过
speedMin=按钮(根,text=“Speed-”,宽度=6,命令=delSpeed)
speedLabel=Label(根,文本=CurrentSpeed,字体=20)
网格(行=1,列=1,padx=(10))
speedPlus=按钮(根,text=“Speed+”,宽度=6,命令=addSpeed)
speedMin.grid(行=1,列=0)
speedPlus.grid(行=1,列=2)
#数字键盘功能
def B1phase():
尝试:
root.focus_get().insert(结束,“1”)
除属性错误外:
通过
def B2phase():
尝试:
root.focus_get().insert(结束,“2”)
除属性错误外:
通过
def B3阶段():
尝试:
root.focus_get().insert(结束,“3”)
除属性错误外:
通过
def B4phase():
尝试:
root.focus_get().insert(结束,“4”)
除属性错误外:
通过
def B5phase():
尝试:
root.focus_get().insert(结束,“5”)
除属性错误外:
通过
def B6phase():
尝试:
root.focus_get()插入(结束,“6”)
除属性错误外:
通过
def B7phase():
尝试:
root.focus_get().insert(结束,“7”)
除属性错误外:
通过
def B8phase():
尝试:
root.focus_get().insert(结束,“8”)
除属性错误外:
通过
def B9phase():
尝试:
root.focus_get().insert(结束,“9”)
除属性错误外:
通过
def B0phase():
尝试:
root.focus_get().insert(结束,“0”)
除属性错误外:
通过
def clearPhase():
尝试:
root.focus_get().delete(0,'end')
除属性错误外:
通过
##数字键盘按钮
B1=按钮(根,text=“1”,宽度=6,命令=B1阶段)
B2=按钮(根,text=“2”,宽度=6,命令=B2phase)
B3=按钮(根,text=“3”,宽度=6,命令=B3阶段)
B4=按钮(根,text=“4”,宽度=6,命令=B4阶段)
B5=按钮(根,text=“5”,宽度=6,命令=B5阶段)
B6=按钮(根,text=“6”,宽度=6,命令=B6phase)
B7=按钮(根,text=“7”,宽度=6,命令=B7阶段)
B8=按钮(根,text=“8”,宽度=6,命令=B8phase)
B9=按钮(根,text=“9”,宽度=6,命令=B9phase)
B0=按钮(根,text=“0”,宽度=6,命令=B0阶段)
clearButton=Button(root,text=“Clear”,width=6,bg='#FA5858',command=clearPhase)
B1.网格(行=5,列=0)
B2.网格(行=5,列=1)
B3.网格(行=5,列=2,padx=(10,10))
B4.网格(行=6,列=0)
B5.网格(行=6,列=1)
网格(行=6,列=2,padx=(10,10))
B7.网格(行=7,列=0)
B8.网格(行=7,列=1)
网格(行=7,列=2,padx=(10,10))
B0.网格(行=8,列=1)
clearButton.grid(行=8,列=0)
##手册
label_4=标签(根,文本=)
标签4.网格(行=2,粘性=W)
label_3=标签(根,text=“手动”)
标签_3.网格(行=9,粘性=W,padx=(10),pady=(10,10))
手动切割=按钮(根,text=“切割”,宽度=4)
手动切割网格(行=10,列=0,帕迪=(1,15))
手动进给=按钮(根,text=“进给”,宽度=4)
手动进给网格(行=10,列=1,pady=(1,15))
#测试功能
def testPhase():
打印(当前速度)
manualTest=按钮(根,text=“Test”,宽度=4,命令=testPhase)
手动测试网格(行=10,列=2,pady=(1,15))
##其他
root.mainloop()
编辑

我使用了Tkinter的之后的

GUI启动,while循环运行。但我不能点击任何按钮

from tkinter import *
import time
import serial

# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
    port='/dev/ttyAMA0',
    baudrate=9600,
    parity=serial.PARITY_ODD,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)
ser.isOpen()





## GUI Title
root = Tk()
root.title("Cutter")





##Communication
RunStop = b'\x01' ## 0 = OFF, 1 = ON

Speed = b'\x03' ## 1, 2, 3, 4
CurrentSpeed = 1

Pieces = b'\x00'

Pieces100 = b'\x00'
Pieces10 = b'\x00'
Pieces1 = b'\x00'

length10000 = b'\x00'
Length1000 = b'\x00'
Length100 = b'\x00'
Length10 = b'\x00'
Length1 = b'\x00'



def Communication():
    while 1:
        uartIn = ser.read()
        print (uartIn)
        time.sleep(1)
        if uartIn == b'\x01':
            ser.write(RunStop)
            print ("Run/Stop Register")
        elif uartIn == b'\x02':
            ser.write(Speed)
            print ("Speed Register")
        else:
            print ("No Register Requested")
        root.after(500, Communication)





## Functions
def runBOT():
    global RunStop
    RunStop = b'\x01'

def stopBOT():
    global RunStop
    RunStop = b'\x00'

def delSpeedBOT():
    global CurrentSpeed
    global speedLabel

    if CurrentSpeed > 1:
        CurrentSpeed -= 1
        speedLabel.config(text=str(CurrentSpeed))
        speedLabel.config(text=CurrentSpeed)
    else:
        pass

def addSpeedBOT():
    global CurrentSpeed

    if CurrentSpeed < 4:
        CurrentSpeed += 1
        speedLabel.config(text=CurrentSpeed)
    else:
        pass

def piecesBOT():
    x = entryPieces.get()
    print("pieces: " + x)

def lengthBOT():
    x = entryLength.get()
    print("length: " + x)





## Layout

labelPieces = Label(root, text="Pieces")
labelLength = Label(root, text="Length")
entryPieces = Entry(root, width=9)
entryLength = Entry(root, width=9)

labelPieces.grid(row=3, sticky=W, padx=(10))
labelLength.grid(row=4, sticky=W, padx=(10))
entryPieces.grid(row=3, column=1, pady=(10,10))
entryLength.grid(row=4, column=1, pady=(10,15))





## Buttons

runButton = Button(root, text="Run", width=6, bg='#58FA58', command=runBOT)
runButton.grid(row=0, column=0,padx=(10,10), pady=(10,10))

stopButton = Button(root, text="Stop", width=6, bg='#FA5858', command=stopBOT)
stopButton.grid(row=0, column=2)

speedMin = Button(root, text="Speed -", width=6, command=delSpeedBOT)
speedMin.grid(row=1, column=0)

speedLabel = Label(root, text=CurrentSpeed, font=20)
speedLabel.grid(row=1, column=1, padx=(10))

speedPlus = Button(root, text="Speed +", width=6, command=addSpeedBOT)
speedPlus.grid(row=1, column=2)

piecesOK = Button(root, text="OK", command=piecesBOT)
piecesOK.grid(row=3, column=2)

lengthOK = Button(root, text="OK", command=lengthBOT)
lengthOK.grid(row=4, column=2)





#Number keyboard functions

def B1phase():
    try:
        root.focus_get().insert(END, "1")
    except AttributeError:
        pass
def B2phase():
    try:
        root.focus_get().insert(END, "2")
    except AttributeError:
        pass
def B3phase():
    try:
        root.focus_get().insert(END, "3")
    except AttributeError:
        pass
def B4phase():
    try:
        root.focus_get().insert(END, "4")
    except AttributeError:
        pass
def B5phase():
    try:
        root.focus_get().insert(END, "5")
    except AttributeError:
        pass
def B6phase():
    try:
        root.focus_get().insert(END, "6")
    except AttributeError:
        pass
def B7phase():
    try:
        root.focus_get().insert(END, "7")
    except AttributeError:
        pass
def B8phase():
    try:
        root.focus_get().insert(END, "8")
    except AttributeError:
        pass
def B9phase():
    try:
        root.focus_get().insert(END, "9")
    except AttributeError:
        pass
def B0phase():
    try:
        root.focus_get().insert(END, "0")
    except AttributeError:
        pass
def clearPhase():
    try:
        root.focus_get().delete(0, 'end')
    except AttributeError:
        pass





## Number keyboard buttons
B1 = Button(root, text="1", width=6, command=B1phase)
B2 = Button(root, text="2", width=6, command=B2phase)
B3 = Button(root, text="3", width=6, command=B3phase)
B4 = Button(root, text="4", width=6, command=B4phase)
B5 = Button(root, text="5", width=6, command=B5phase)
B6 = Button(root, text="6", width=6, command=B6phase)
B7 = Button(root, text="7", width=6, command=B7phase)
B8 = Button(root, text="8", width=6, command=B8phase)
B9 = Button(root, text="9", width=6, command=B9phase)
B0 = Button(root, text="0", width=6, command=B0phase)
clearButton = Button(root, text="Clear", width=6, bg='#FA5858', command=clearPhase)

B1.grid(row=5, column=0)
B2.grid(row=5, column=1)
B3.grid(row=5, column=2, padx=(10,10))
B4.grid(row=6, column=0)
B5.grid(row=6, column=1)
B6.grid(row=6, column=2, padx=(10,10))
B7.grid(row=7, column=0)
B8.grid(row=7, column=1)
B9.grid(row=7, column=2, padx=(10,10))
B0.grid(row=8, column=1)
clearButton.grid(row=8, column=0)





## Manual

label_4 = Label(root, text="")
label_4.grid(row=2, sticky=W)
label_3 = Label(root, text="Manual")
label_3.grid(row=9, sticky=W, padx=(10), pady=(10,10))

manualCut = Button(root, text="Cut", width=4)
manualCut.grid(row=10, column=0, pady=(1,15))

manualFeed = Button(root, text="Feed", width=4)
manualFeed.grid(row=10, column=1, pady=(1,15))


# Test function
def testPhase():
    pass

manualTest = Button(root, text="Test", width=4, command=testPhase)
manualTest.grid(row=10, column=2, pady=(1,15))





## Running GUI and communication
root.after(500, Communication)


root.mainloop()
从tkinter导入*
导入时间
进口系列
def poll_serial(root):

    uartIn = ser.read()
    if uartIn:
        process_value_from_serial(uartIn)

    root.after(1000, poll_serial, root)
root = Tk()
...
poll_serial(root)
root.mainloop()
def poll_serial(root):
    if (ser.inWaiting()>0):
        uartIn = ser.read()
        if uartIn:
            process_value_from_serial(uartIn)
    root.after(1000, poll_serial, root)