问题
In my kivy project, I have a button allowing me to generate a matplotlib graph in .png format. Generating this image takes time (around 20 seconds), and I would like to display a pop-up window to warn the user.
What i tried :
<MyPopup@Popup>:
auto_dismiss: False
Button:
text: 'This could take time, please wait :) '
on_release: root.dismiss()
and :
ActionButton:
text: 'generate graph'
on_release: Factory.MyPopup().open()
#on_release: root.generate_graph()
Unfortunately, if I uncomment the second "on_release", the pop_up window never appears?
Do you have any guess?
Thank you in advance!
回答1:
You were overwriting the on_release method.
ActionButton:
text: 'generate graph'
on_release:
Factory.MyPopup().open()
root.generate_graph()
回答2:
To display a popup while a method is running I use threads. When the popup fires up, the method runs in a thread.
CODE:
def popupThread(self):
#set the popup structure
self.popup = ActivityBox(self)
self.popup.open()
# run the method in threads
t1 = threading.Thread(target = self.someMethod)
t1.start()
The popup is defined in the Builder.load_string()
:
def build(self):
sm = Builder.load_string("""
<ActivityBox>:
size_hint: 1, .7
auto_dismiss: False
title: 'some activity'
title_align: "center"
title_size: 30
BoxLayout:
orientation: "vertical"
Label:
font_size: '30sp'
text: 'work in progress'
BoxLayout:
orientation: "horizontal"
spacing: 10
size_hint: 1, .5
""")
来源:https://stackoverflow.com/questions/54924125/pop-up-kivy-displaying-while-a-process-is-running-in-background