You have too many things starting the ShowTime()
method:
Clock.schedule_interval(updateClock.ShowTime, 0.5)
and
on_state: root.ShowTime(self.state)
and
threading.Timer(1, self.ShowTime).start()
And each of those has the possibility to start an infinite loop (while state == 'down':
), since the state
variable that is passed into ShowTime()
will never change. The loop started by clicking on the ToggleButton
will run in the main thread, freezing your GUI.
I believe a better approach would be to just start/stop the ShowTime()
method at one location. perhaps using the ToggleButton
.
Try changing the kv
to accomplish that:
ToggleButton:
text: "Time"
on_state: root.startShowTime(self.state) if self.state == 'down' else root.stopShowTime()
backgrund_normal: ""
background_color: (150/255,150/255,150/255,1)
and add/change GUILayout
and GUI
methods to support that:
class GUILayout(Widget):
def startShowTime(self, state):
self.clock_event = Clock.schedule_interval(self.ShowTime, 0.5)
def stopShowTime(self):
self.clock_event.cancel()
if (ard_connected):
ard.write(" ".encode())
ard.flush()
def ShowTime(self, dt):
if (ard_connected):
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
ard.write(current_time.encode())
ard.flush()
class GUI(App):
def build(self):
updateClock = GUILayout()
return updateClock
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…