我有一个KivyMD按钮,可以打开(初始化) YOLO。初始化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发布于 2020-08-23 01:28:57
您可以在另一个线程中执行init_yolo(),这样它就不会阻止颜色更改为琥珀色。然后该线程可以调用Clock.schedule_once()将颜色设置为绿色。这假设init_yolo()不会对图形用户界面进行任何更改(这必须在主线程上完成)。这段代码没有经过测试,因此可能会有一些错误,但它应该会让您大致了解如何做到这一点。
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 showshttps://stackoverflow.com/questions/63538664
复制相似问题