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



我有一个树莓派和一顶感应帽。我正试图创建一个循环,将感应帽(温度和方向(的输出显示到GUI,并每秒自动更新。因此,将打开一个屏幕,显示圆周率提供的信息。但我的代码只有在关闭屏幕时才会刷新。我希望它每秒钟自动刷新一次。

这是我的代码(有点长(;我刚开始使用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, y=%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,p,h)))
myFile.write(str(","))
myFile.write(str((pitch,roll,yaw)))
myFile.write(str(","))
myFile.write(str((x, y, z)))
myFile.write(str(","))
myFile.write(str((x2,y2,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=" ")
welcom_message=Text(app,text="pitch=")
welcom_message=Text(app,text=pitch)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="roll=")
welcom_message=Text(app,text=roll)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="yaw=")
welcom_message=Text(app,text=yaw)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="Accelerometer x=")
welcom_message=Text(app,text=x)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="Accelerometer y=")
welcom_message=Text(app,text=y)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="Accelerometer z=")
welcom_message=Text(app,text=z)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="Accelerometer x=")
welcom_message=Text(app,text=x2)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="Accelerometer y=")
welcom_message=Text(app,text=y2)
welcom_message=Text(app,text=" ")
welcom_message=Text(app,text="Accelerometer z=")
welcom_message=Text(app,text=z2)
welcom_message=Text(app,text=" ")
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循环,如上面所示注册一个回调,并在该回调中查询要显示的值的传感器。

最新更新