Search code examples
pythonmethodspopupkivybackground-process

Kivy - automatically start a method from popup


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.

  1. Displaying something in kivy while a process is running background
  2. Kivy: how to display a widget while waiting for another one to be displayed (both called from a same event)
  3. Kivy popup running in separate thread
  4. Pop up Kivy displaying while a process is running in background
  5. 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()

Solution

  • 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.