我在使用Tkinter更新窗口上的數據時遇到了一些問題。我使用我的覆盆子pi和Atlas科學pH傳感器進行串行通信。該窗口完美地工作,但沒有按照我的預期更新數據,所以,我做錯了什麼?這是代碼。我非常感謝你的幫助。使用與樹莓派和pH傳感器的串行通信更新窗口上的數據
from Tkinter import *
import time
import serial
def datos(port):
bus = ""
while True:
d = port.read()
bus += d
if d == '\r':
return bus
def sensor_pH():
pH.set(datos(port))
root.after(1000, sensor_pH)
port = serial.Serial('/dev/ttyAMA0', baudrate = 9600, timeout = 2)
port.write('L,1\r')#Command to turn the sensor's led on
port.write('C,1\r')#Command to read data, (pH), continuously every second
root = Tk()
root.geometry('300x300')
pH = IntVar()
bus = StringVar()
etpH = Label (root, text = 'pH: ')
etpH.grid(row=0, column=0)
et1= Label(root, textvariable = pH)
et1.grid(row=0, column=9)
sensor_pH
root.mainloop()
您是否關閉了'ttyAMA0'上的串行控制檯?你可以使用'sudo raspi-config'來做到這一點,重新啓動,然後嘗試運行你的代碼。 – dhruvvyas90