Reputation: 103
I wonder if I can immidiately update a property of a Label described in a kv layout file as so:
BoxLayout:
orientation: 'horizontal'
Label:
id: new_order
font_size: 40
text: ''
Thats because in my app I have set up two Clock schedule interval objects and I wont them to work simultaneously.
class MyApp(App):
def on_start(self):
Clock.schedule_interval(self.update_label, 1)
Clock.schedule_interval(self.play_sounds, 1)
def update_label(self):
# does something
self.root.ids.new_order.text="Updated Text"
# does something more
def play_sounds(self):
for i in range(10):
sound.play()
sleep(2)
But... label is always updated after the play_sounds() method exits. So is there a way to immidiately update the displayed label text just after setting it to "Updated Text" inside update_label() method?
Upvotes: 0
Views: 64
Reputation: 103
What I have done and solved my problem, was to spawn a thread to handle sound-playing in parallel with main, after the helpful @john-anderson answer. So:
import Threading
def play_sounds():
for i in range(10):
sound.play()
sleep(2)
class MyApp(App):
def on_start(self):
Clock.schedule_interval(self.update_label, 1)
def update_label(self):
# if condition:
self.root.ids.new_order.text="Updated Text"
t = threading.Thread(target=play_sounds)
t.start()
Upvotes: 0
Reputation: 38822
Your play_sounds()
method will take at least 20 seconds to run (because of the sleep
) and you are trying to run that method every second. Sounds like a bad idea. Also, the sleep
freezes your entire GUI, since it is run on the main thread. Try changing play_sounds()
to:
def play_sounds(self, dt):
sound = SoundLoader.load('some_sound_file')
sound.play()
Upvotes: 1
Reputation: 243
You could nest the Clock.schedule_interval
. Within the function after the main logic body of the function, you could put in another Clock.schedule_interval
with the next function to call. I recommend in the inner function that you use a clock lambda function to create a one liner.
Upvotes: 0