GUI重复输入数据并刷新输入数据

问题描述

我有一个RaspBerry Pi和一顶感官帽子。我正在尝试创建一个循环,以显示从感应帽(温度和方向)到GUI的输出,并每秒自动更新一次。因此,将打开一个屏幕,显示Pi提供的信息。但是,只有关闭屏幕后,我的代码才会刷新。我希望每秒钟自动刷新一次。

这是我的代码(有点长);我刚刚开始使用Python:

from sense_hat import SenseHat
from datetime import date,datetime

from datetime import datetime
import datetime
from time import sleep
import time
from guizero import App,Text

sense=SenseHat()
sense.set_imu_config(True,True,True) # accelerometer,magnetometer,gyroscope sense.clear() myFile=open('PiTempData.txt','a') myFile.write(str(['datetime','temp','pres','hum','itch_degree','roll_degree','yaw_degree','acc_x_raw','acc_y_raw','acc_z_raw','acc_x','acc_y','acc_z','compass']))

while True:
    t= sense.get_temperature()-20
    p = sense.get_pressure()
    h= sense.get_humidity()

orientation = sense.get_orientation_degrees()

pitch = orientation["pitch"]
roll = orientation["roll"]
yaw = orientation["yaw"]

accel_only = sense.get_accelerometer()
raw = sense.get_accelerometer_raw()
acceleration = sense.get_accelerometer_raw()
x = acceleration['x']
y = acceleration['y']
z = acceleration['z']
x1 = acceleration['x']
y1 = acceleration['y']
z1 = acceleration['z']
x2=round(x1,0)
y2=round(y1,0)
z2=round(z1,0)
raw1 = sense.get_compass_raw()
gyro_only = sense.get_gyroscope()

north = sense.get_compass()

print ("")
print ("The current time",datetime.datetime.Now())
print ("")
print ("Temp:%.i c,Pressure = %.0f. RH= %.0f" % (t,p,h))
print ("")
print ("Orientation")
print("pitch {0} roll {1} yaw {2}".format(pitch,roll,yaw))
print ("")
print ("Accelerometer")
print("x=%s,y=%s,z=%s" % (x,y,z))
print("x=%s,z=%s" % (x2,y2,z2))
print ("")
print("compass")
print("north: %s" % north)
time.sleep(0.5)

myFile=open('PiTempData.txt','a')

myFile.write("\n")
myFile.write(str(datetime.datetime.Now()))
myFile.write(str(","))
myFile.write(str ((t,h)))
myFile.write(str(","))
myFile.write(str((pitch,yaw)))
myFile.write(str(","))
myFile.write(str((x,z)))
myFile.write(str(","))
myFile.write(str((x2,z2)))
myFile.write(str(","))
myFile.write(str(north))


app=App(title= "IMU")
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="temp=")
welcom_message=Text(app,text=t)
welcom_message=Text(app,text="pitch=")
welcom_message=Text(app,text=pitch)
welcom_message=Text(app,text="roll=")
welcom_message=Text(app,text=roll)
welcom_message=Text(app,text="yaw=")
welcom_message=Text(app,text=yaw)
welcom_message=Text(app,text="Accelerometer x=")
welcom_message=Text(app,text=x)
welcom_message=Text(app,text="Accelerometer y=")
welcom_message=Text(app,text=y)
welcom_message=Text(app,text="Accelerometer z=")
welcom_message=Text(app,text=z)
welcom_message=Text(app,text=x2)
welcom_message=Text(app,text=y2)
welcom_message=Text(app,text=z2)
welcom_message=Text(app,text="compass=")
welcom_message=Text(app,text=north)

app.display()

解决方法

app.display()之前,您必须注册一个回调,该回调将被定期调用:

welcom_message.after(1000,update_label)

def update_label():
    welcom_message.set(read_sensor()) # here read the sensor data on every callback call
    # recursive call
    welcom_message.after(1000,update_label)

但是,您的代码有一个问题:

while True:
    t= sense.get_temperature()-20
    p = sense.get_pressure()
    h= sense.get_humidity()

脚本中间的while循环似乎是无限的,即您的程序将永远不会超过该循环。我相信app.display()实际上正在阻止-它应该防止您的程序返回。因此,我建议摆脱这个while循环,如我上面所示在该回调中注册一个回调,并在传感器中查询要显示的值。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...