问题
PROBLEM
I am using Python3 and Kivy2. Regardless I would like a popup to automatically start a time consuming method in the background
DESCRIPTION In the script call for the popup when a condition is met.
if self.header == "loadbag":
messageTitle = "machine status"
messageLabel = "work in progress"
self.popup(messageTitle, messageLabel)
The popup method structure is:
def popup(self, boxTitle, boxLabel): # popup for showing the activity
self.MessageButtonCancel = "ANNULLA"
self.MessageBoxTitle = boxTitle
self.MessageBoxLabel = boxLabel
self.popup = ActivityBox(self)
self.popup.open()
The kv language part in the Builder.load_string for the ActivityBox is:
<ActivityBox>:
size_hint: 1, .7
auto_dismiss: False
title: app.MessageBoxTitle
title_align: "center"
title_size: 30
BoxLayout:
orientation: "vertical"
Label:
font_size: '30sp'
text: app.MessageBoxLabel
BoxLayout:
orientation: "horizontal"
spacing: 10
size_hint: 1, .5
# both buttons are not necessary
# the popup just shows a message
# Button:
# font_size: 50
# background_color: 0,204,0,1
# text: app.MessageButtonConfirm # "CONFIRM"
# on_press:
# self.disabled = True
# self.background_color = 0,255,0,1
# app.do()
# Button:
# font_size: 50
# background_color: 204,0,0,1
# text: app.MessageButtonCancel # "CANCEL"
# on_press:
# self.background_color = 255,0,0,1
# app.cancel()
# root.dismiss()
As you can see, the buttons in the ActivityBox are disabled as I only want the label to appear. Below are some similar questions I have checked without finding a solution.
- Displaying something in kivy while a process is running background
- Kivy: how to display a widget while waiting for another one to be displayed (both called from a same event)
- Kivy popup running in separate thread
- Pop up Kivy displaying while a process is running in background
- Open kivy popup before continuing
QUESTION
Is there a way to start a method automatically from within the ActivityBox, without binding it to the buttons?
UPDATE 1
I tried to insert the method call in the def popup(self, boxTitle, boxLabel): but the popup appeared only after the method terminated.
def popup(self, boxTitle, boxLabel): # popup for showing the activity
self.MessageBoxTitle = boxTitle
self.MessageBoxLabel = boxLabel
self.popup = ActivityBox(self)
self.time_consuming_method() # here goes the method to run
self.popup.open()
UPDATE 2
I tried with threading without success. The popup and the method each in a separate thread. The popup appeared only after the method terminated.
def popup(self, boxTitle, boxLabel): # popup for showing the activity
self.MessageBoxTitle = boxTitle
self.MessageBoxLabel = boxLabel
self.popup = ActivityBox(self)
self.popup.open()
t1 = threading.Thread(target = popup, args = (boxTitle, boxLabel))
t2 = threading.Thread(target = time_consuming_method)
t1.start()
t2.start()
t1.join()
t2.join()
回答1:
Using the join()
method of a Thread
causes a wait until that Thread
has completed, which is almost equivalent to not using threading
.
Try eliminating thread t1
, and just call popup
without using a new thread for that. Then start thread t2
, but eliminate the t2.join()
call.
回答2:
ANSWER
The answer proposed @John Anderson was helpful. The join method causes an undesired wait.
First I tried to run the def popup
method as the lead and the time_consuming_method
as a thread. This approach did not solved the problem.
NOT WORKING CODE
def popup(self, boxTitle, boxLabel): # popup for showing the activity
self.MessageBoxTitle = boxTitle
self.MessageBoxLabel = boxLabel
self.popup = ActivityBox(self)
self.popup.open()
self.popup(boxTitle, boxLabel)
t2 = threading.Thread(target = time_consuming_method)
t2.start()
Then I tried running self.popup.open()
method as the lead and the the time_consuming_method
as a thread from inside the def popup
method. This solution worked out fine, the popup appears when the method starts.
WORKING CODE
def popup(self, boxTitle, boxLabel): # popup for showing the activity
self.MessageBoxTitle = boxTitle
self.MessageBoxLabel = boxLabel
self.popup = ActivityBox(self)
self.popup.open() # call the popup
t2 = threading.Thread(target = time_consuming_method) # define the thread
t2.start() # start the thread
来源:https://stackoverflow.com/questions/62691704/kivy-automatically-start-a-method-from-popup