我在我的树莓派 3 上使用带有 python 3.4.2 的 kivy 1.10.1,带有一些按钮和标签的 GUI 工作正常。现在我想每秒更新一次 GUI 以显示一些新数据(更新时钟、从数据库中获取一些值等(
问题:
当我启动计时器时,GUI 变得非常慢。我测试了代码的一些修改,例如禁用数据读取,但没有任何效果。每次调用计时器时,GUI 都会滞后几毫秒。
有人知道这是什么原因吗?我确信我的覆盆子的硬件不是问题,因为当我使用kivy示例(例如,使用一些3D渲染(时,一切都可以正常工作,没有任何滞后。
基维详细信息:
[INFO ] [Kivy ] v1.10.1
[INFO ] [Python ] v3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1]
[INFO ] [Factory ] 194 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO ] [Window ] Provider: egl_rpi
[INFO ] [GL ] Using the "OpenGL ES 2" graphics system
[INFO ] [GL ] Backend used <gl>
[INFO ] [GL ] OpenGL version <b'OpenGL ES 2.0'>
[INFO ] [GL ] OpenGL vendor <b'Broadcom'>
[INFO ] [GL ] OpenGL renderer <b'VideoCore IV HW'>
[INFO ] [GL ] OpenGL parsed version: 2, 0
[INFO ] [GL ] Shading version <b'OpenGL ES GLSL ES 1.00'>
[INFO ] [GL ] Texture max size <2048>
[INFO ] [GL ] Texture max units <8>
[INFO ] [Window ] virtual keyboard allowed, multiuser mode, not docked
[INFO ] [Text ] Provider: sdl2
[INFO ] [KivyMD ] KivyMD version: 0.1.2
代码片段:
# Start timer in a separated thread
def start_timer_gui(self):
try:
# Test connection
if s_conn.test_serial(self) == True:
th = threading.Thread(target=self.thread_timer_gui,
args=())
th.start()
...
# Start timer (is in a separated thread)
def thread_timer_gui(self):
Clock.schedule_interval(partial(self.thread_timer_gui_methods), 1)
@timeit
# Call functions for updating gui from separated thread
def thread_timer_gui_methods(self, dt=0):
self.update_gui()
#self.get_measured_vales()
def update_gui(self):
self.emcstatebar.lbl_local_time.text = "13-03-2019 07:32"
...
我已经阅读了此页面的信息 https://kivy.org/doc/stable/guide/events.html 但它对我没有帮助。
此致敬意!
我找到了解决方案。如果只是用"schedule_interval(...("更新 GUI 元素,一切正常。如果我尝试通过"schedule_interval(...("获取测量值,则会发生滞后。我将获取值更改为单独的线程:
# Get measured values in a separated thread)
def thread_timer_values(self):
while True:
self.get_measured_vales()
time.sleep(2)
现在我可以在 GUI 中显示测量值而不会延迟!