【问题标题】:Kivy ProgressBar Popup not showing properlyKivy ProgressBar 弹出窗口未正确显示
【发布时间】:2020-07-07 15:52:14
【问题描述】:

我想实现两种加载弹窗:

  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

【问题讨论】:

    标签: python popup kivy progress-bar clock


    【解决方案1】:

    您没有展示如何调用test2(),但如果它在主线程上执行,那么它将在主线程上停止所有其他执行7 秒(对sleep 的7 次调用)。然后当它完成时,主线程可以继续,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
    

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      • 2020-11-26
      • 2012-07-21
      • 1970-01-01
      • 2012-02-20
      • 2014-01-24
      相关资源
      最近更新 更多