Python/Tkinter用标签设计Arduino串行连接接口

2024-10-04 11:25:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,正在尝试使用标签创建接口。
标签包含已连接和已断开连接的消息。你知道吗

from Tkinter import *

root = Tk()

def connectToArduino():

    arduino = serial.Serial(arduinoPort, serialTransferRate)
    arduino.timeout = None
    time.sleep(2)
    arduino.write("S\n")
    Label(root, text="Connected")
    return arduino


arduino = connectToArduino()
arduino.pack()
root.mainloop()

如何使用if-else条件在标签处添加已连接/已断开连接的邮件?你知道吗


Tags: fromimport消息tkinterdefserialroot标签
1条回答
网友
1楼 · 发布于 2024-10-04 11:25:16

必须将标签的ID存储在变量中

## save the ID for the label
lab=Label(root, text="Connected")
lab.grid()

## return the label's ID or it will be garbage collected
## when the function exits
return arduino, lab

然后你可以使用

lab.config(text="Disconnected")

在测试并发现其已断开连接的代码中。你知道吗

相关问题 更多 >