Обновление Progress Bar отображается в Popup в Kivy - PullRequest
0 голосов
/ 18 сентября 2018

Как обновить индикатор выполнения, отображаемый в Kivy.В следующем примере я получаю AttributeError: 'super' object has no attribute '__getattr__'.Проблема в следующей строке

self.ids.progress.value = value

Я понимаю, почему, поскольку виджет progressBar находится в <LoadingPopup>, а не <MainScreen>, но после попытки нескольких разных вещей я не могу ссылаться на progressBar виджет в <LoadingPopup> из do_update method.

Заранее спасибо

import threading
from functools import partial

from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import StringProperty
from kivy.uix.spinner import Spinner
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.popup import Popup



Builder.load_string('''

<LoadingPopup>:
    title: "Popup"
    size_hint: None, None
    size: 400, 400
    auto_dismiss: False

    BoxLayout:
        orientation: "vertical"
        ProgressBar:
            id: progress
            size_hint: (1.0, 0.06)

<MainScreen>:
    BoxLayout:
        orientation: 'vertical'

        Spinner:
            id: first
            text: ' First Number'
            values: ['1','2','3','4','5','6','7','8','9']

        Spinner:
            id: second
            text: ' Second Number'
            values: ['1','2','3','4','5','6','7','8','9']

        Label:
            id: result
            text: ' Result'
            color: 0,0,0,1
        Button:
            id: res
            on_press: root.doit_in_thread(first.text,second.text)
            text: 'Multiply'


''')

class LoadingPopup(Popup):

    def __init__(self, obj, **kwargs):
        super(LoadingPopup, self).__init__(**kwargs)


class MainScreen(FloatLayout):
    changet = StringProperty()
    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)

    def doit_in_thread(self, fir, sec):
        popup = LoadingPopup(self)
        popup.open()
        threading.Thread(target=partial(self.onMul, fir, sec, popup)).start()

    def do_update(self, value, text, *args):
        self.ids.progress.value = value
        self.ids.result.text = text

    def onMul(self,fir,sec, popup):
        a = (int(fir)*int(sec))
        print(a)
        b = 0
        old_value = 0
        endRange = 1000000
        for i in range(endRange):
            progress = int(((i+1)*100)/endRange)
            if progress != old_value and progress % 5 == 0:
                text = str(b*(int(fir)*int(sec)))
                Clock.schedule_once(partial(self.do_update, progress, text))
                old_value = progress
            b+=1

        popup.dismiss()

class TestApp(App):
    def build(self):
        return MainScreen()

if __name__ == "__main__":
    TestApp().run()

Ответы [ 2 ]

0 голосов
/ 19 сентября 2018

Python Code

  1. Замените все ссылки popup на self.popup, чтобы вы могли получить к ним доступ в методах внутри класса MainScreen.
  2. Заменить self.ids.progress.value на self.popup.ids.progress.value
  3. Поскольку a = (int(fir)*int(sec), замените str(b*(int(fir)*int(sec))) на str(b * a)
  4. Не нужно передавать popup в onMul метод, доступ к нему осуществляется с помощью self.popup

Фрагмент

class MainScreen(FloatLayout):
    changet = StringProperty()

    def doit_in_thread(self, fir, sec):
        self.popup = LoadingPopup(self)
        self.popup.open()
        threading.Thread(target=partial(self.onMul, fir, sec)).start()

    def do_update(self, value, text, *args):
        self.popup.ids.progress.value = value
        self.ids.result.text = text

    def onMul(self, fir, sec):
        a = (int(fir)*int(sec))
        print(a)
        b = 0
        old_value = 0
        endRange = 1000000
        for i in range(endRange):
            progress = int(((i+1)*100)/endRange)
            if progress != old_value and progress % 5 == 0:
                text = str(b*(int(fir)*int(sec)))
                # text = str(b * a)
                print("\ttext=", text)
                Clock.schedule_once(partial(self.do_update, progress, text))
                old_value = progress
            b+=1
    self.popup.dismiss()

файл kv

result не отображается, поскольку цвет фона виджета Label по умолчанию черный, а цвет текста для result также установлен на черный, color: 0,0,0,1.Поэтому удалите color: 0,0,0,1.

Фрагмент

    Label:
        id: result
        text: ' Result'

Выход

Img01

0 голосов
/ 18 сентября 2018

Один из способов сделать это - сохранить ссылку на всплывающее окно, чтобы вы могли обратиться к ней позже:

def __init__(self, **kwargs):
    super(MainScreen, self).__init__(**kwargs)
    self.popup = LoadingPopup(self)

def doit_in_thread(self, fir, sec):
    self.popup.open()
    threading.Thread(target=partial(self.onMul, fir, sec, self.popup)).start()

def do_update(self, value, text, *args):
    self.popup.ids.progress.value = value
    self.ids.result.text = text
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...