如何解决动态更新KivyMD按钮Python
我有一个打开(初始化)YOLO的KivyMD按钮。 YOLO初始化后,按钮变为绿色。可以,但是YOLO初始化大约需要8秒钟。因此,我想在初始化期间将按钮变成琥珀色,完成后变成绿色。麻烦的是,琥珀色从不显示,因为例程正在阻塞。有什么想法可以重新绘制按钮以显示琥珀色,直到对yolo_init()的调用返回时?谢谢!
def btn_yolo(self):
if self.root.ids['_btn_yolo'].text_color == green: #we want to turn-off YOLO
self.yolo.close_session()
self.root.ids['_btn_yolo'].text_color = black #this shows
else: #we want to initialize YOLO
self.root.ids['_btn_yolo'].text_color = amber #this never shows
#update/refresh the button how?
self.yolo = init_yolo(FLAGS) #this takes long
self.root.ids['_btn_yolo'].text_color = green #this shows
解决方法
您可以在另一个线程中执行init_yolo()
,这样就不会阻止颜色变为琥珀色。然后,该线程可以调用Clock.schedule_once()
将颜色设置为绿色。这假定init_yolo()
不会对GUI进行任何更改(必须在主线程上完成)。该代码尚未经过测试,因此可能会有一些错误,但是它应该使您大致了解如何执行此操作。
def btn_yolo(self):
if self.root.ids['_btn_yolo'].text_color == green: #we want to turn-off YOLO
self.yolo.close_session()
self.root.ids['_btn_yolo'].text_color = black #this shows
else: #we want to initialize YOLO
self.root.ids['_btn_yolo'].text_color = amber #this never shows
#update/refresh the button how?
threading.Thread(target=self.do_init,args=(FLAGS)).start()
def do_init(self,FLAGS):
self.yolo = init_yolo(FLAGS) #this takes long
Clock.schedule_once(self.update_text_color)
def update_text_color(self,dt):
self.root.ids['_btn_yolo'].text_color = green #this shows
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。