问题
My kivy popup does not appear on the screen until the rest of the code in my method finishes running. I am trying to display a progress bar so it is worthless in it's current state.
I have tried to thread the process to open the popup and tried without threading.
from kivy.uix.progressbar import ProgressBar
from kivy.uix.popup import Popup
import threading
def submit():
popup = Popup(title='Submit Progress', content=ProgressBar())
thread_object = threading.Thread(target=popup.open)
thread_object.start()
perform some iterable actions
increment progress bar value
if progress_bar.value == progress_bar.max:
popup.auto_dismiss = True
# This is when the popup finally appears
I need the open function of my popup to actually take effect before performing the iterable actions so the user can watch the progress
回答1:
This is based on member 'inclement's' answer. I am not 100% what you are attempting to do but here is some code I tested that kicks off a long-running task in its own thread and holds the popup open until that task completes.
Kivy .kv file entry
Button:
text: "do..."
on_release: app.root.info_popup(self) # your app may need root.info_popup(self)
In the app, the target method
def info_popup(self, _button: Button):
print(str(type(_button)))
popup_content = ProgressBar(max=10)
popup = Popup(title="Countdown",
size_hint=(None, None), size=(400, 180),
content=popup_content,
auto_dismiss=False)
print("starting a new thread to do the countdown")
threading.Thread(target=partial(self.update_progress, an_object=popup_content,
the_popup=popup), daemon=True).start()
popup.open()
which starts this function as its own thread
def update_progress(self, an_object, the_popup: Popup):
for i in range(10, -1, -1):
time.sleep(1.0)
print("progress: {}".format(i))
an_object.value = i
the_popup.dismiss()
print("I have dismissed the popup")
回答2:
I solved the issue by using the Clock.schedule_once() function to schedule my other threaded updates.
来源:https://stackoverflow.com/questions/57732400/open-kivy-popup-before-continuing