Kivy ProgressBar 弹出窗口未正确显示

Kivy ProgressBar Popup not showing properly

我想实现两种加载弹出窗口:

  1. 以预定义的持续时间调用弹出窗口
  2. 弹出窗口从其他函数更新

第一个弹出窗口正常工作。但是,在函数完成之前,第二个弹出窗口不会显示。我想我在某处遗漏了一个 Clock.schedule_once,但我不确定具体在哪里以及如何实现它。

class BoxL(BoxLayout): 
    def __init__(self): 
        super(BoxL, self).__init__()

    def next(self, dt):
        if self.progress_bar.value>=100:
            return False
        self.progress_bar.value += 1
        if self.progress_bar.value == 100:
            self.popup.dismiss()

    def next_manually(self, dt):
        if self.progress_bar.value>=100:
            return False
        self.progress_bar.value = self.loadingValue
        if self.progress_bar.value == 100:
            self.popup.dismiss()
            self.loadingValue = 0

    def loading(self, *argv):
        if len(argv) == 1:
            loadingText = argv[0]
        if len(argv) == 2:
            loadingText = argv[0]
            loadingTime = argv[1]
        self.progress_bar = ProgressBar()
        self.popup = Popup(title='[b]Loading: [/b]' + loadingText,
                               title_size = 20,
                               title_align = 'center',
                               auto_dismiss = False,
                               size_hint = (None, None),
                               size = (384, 160),
                               content = self.progress_bar)
        if len(argv) == 2:
            self.popup.bind(on_open=
                            Clock.schedule_interval(self.next, loadingTime/100))
        if len(argv) == 1:
            self.popup.bind(on_open=
                            Clock.schedule_interval(self.next_manually, 5/100))
        self.progress_bar.value = 0
        self.popup.open()

现在实际调用弹出窗口的函数:

    def test(self):
        self.loading('Testload', 5)
    
    def test2(self):
        self.loading('TestLoad2')
        time.sleep(1)
        self.loadingValue = 10
        time.sleep(1)
        self.loadingValue = 30
        time.sleep(1)
        self.loadingValue = 35
        time.sleep(1)
        self.loadingValue = 50
        time.sleep(1)
        self.loadingValue = 60
        time.sleep(1)
        self.loadingValue = 90
        time.sleep(1)
        self.loadingValue = 100

你没有显示 test2() 是如何调用的,但是如果它在主线程上执行,那么它将停止主线程上的所有其他执行 7 秒(对 [= 的 7 次调用12=]).然后当它完成时,主线程可以继续并且 ProgressBar 将更新为 loadingValue 的值,那时是 100。我认为您需要将大部分 test2() 方法放在另一个线程中。尝试这样的事情:

def test2(self):
    self.loading('TestLoad2')
    threading.Thread(target=self.do_updates).start()

def do_updates(self):
    time.sleep(1)
    self.loadingValue = 10
    time.sleep(1)
    self.loadingValue = 30
    time.sleep(1)
    self.loadingValue = 35
    time.sleep(1)
    self.loadingValue = 50
    time.sleep(1)
    self.loadingValue = 60
    time.sleep(1)
    self.loadingValue = 90
    time.sleep(1)
    self.loadingValue = 100