Can't get kivy progress bar to update

一笑奈何 提交于 2021-01-29 08:36:29

问题


I've been building an app and I've been struggling to get the progress bar to update after each function is called. In the code below, my intent was to call a function then once that function finished running, I would update the self.load attribute with a new value. After assigning the self.load with a new value I want to pass it onto the progress bar in kivy so that its value would increase and it would be displayed on screen.

import SEOProgram
import time
import os
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.clock import Clock
from kivy.uix.widget import Widget
from kivy.uix.progressbar import ProgressBar
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty

class progBar(GridLayout):
    pass

class ThirdWindow(Screen):
    pass

class SecondWindow(Screen):

    pb = ProgressBar()

    def __init__(self, **kwargs):
        super(SecondWindow, self).__init__(**kwargs)
        self.load = 0
        #self.update_bar = Clock.create_trigger(self.update_value)
        pass

    def calculate(self):
        #ids = SecondWindow()
        #print(ids.ids)

        with open("keyword.txt", "r") as keywordread:
            keyphrase = keywordread.readlines()

        phrase = keyphrase
        self.ids.pb.value = self.ids.pb.value

        main_URL = "https://google.com/search?q=" + str(phrase)

        os.remove("keyword.txt")

        SEOProgram.FirstPage.first_page_crawl(main_URL)
        self.load = 10 # the value is supposed to increase after each function
        #self.update_value()
        time.sleep(10)
        SEOProgram.remove_urls()
        self.load = 30
        SEOProgram.InternalLinks.get_internal()
        #time.sleep(10)
        SEOProgram.ExternalLinks.get_external()
        #time.sleep(10)
        SEOProgram.FindImages.get_images()
        #time.sleep(10)
        SEOProgram.WordCounter.count_words()
        #time.sleep(10)
        SEOProgram.WordCounter.show_word_count()

    #def update_value(self, dt=None):
     #   if self.load <= 10000:
      #      self.load += 10
       #     self.ids.pb.value += self.load
        #    self.update_bar()


class SEOLabel(Screen):

    def __init__(self, **kwargs):
        super(SEOLabel, self).__init__(**kwargs)

    main_keyword = ObjectProperty(None)


    def submit(self):
        self.keyword = self.main_keyword.text
        print(self.keyword)
        with open("keyword.txt", "w") as keywordfile:
            keywordfile.write(self.keyword)



class WindowManager(ScreenManager):
    pass


seofile = Builder.load_file("seo.kv")

class SEOApp(App):

    def build(self):
        return seofile



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

Below is the kv file:

WindowManager:
    SEOLabel:
    SecondWindow:
    ThirdWindow:

<SEOLabel>:

    name: "main"
    main_keyword: keyword


    Label:
        text: "Enter Your Keyword Below:"
        font_size: 40
        pos_hint: {'y' : 0.2}

    TextInput:
        id: keyword
        hint_text: "Keyword"
        multiline: False
        width: 200
        size_hint: None, None
        height: 40
        pos: root.width / 2.5, root.height / 2

    Button:
        text: "Access Keyword"
        size_hint: 0.5, 0.2
        pos: root.width / 3.75, root.height / 5
        on_release:
            root.submit()
            app.root.current = "main" if keyword.text == "" else "second"
            root.manager.transition.direction = "left"



<SecondWindow>:

    name: "second"
    pb: pb

    Label:
        text: "Click Button To Get Results"
        font_size: root.width / 30
        pos_hint: {"x" : 0.5, "y" : 0.5}


    ProgressBar:
        id: pb
        size: 200, 200
        max: 100
        value: root.load
        canvas:
            Color:
                rgb: .45, .28, .5
        pos_hint: {"x" : 0.5, "y" : 0.3}

    Button:
        text: "Get Results"
        size_hint: None, None
        pos_hint: {"x" : 0.5, "y" : 0.1}
        on_release:
            root.calculate()



<ThirdWindow>:

    name: "third"

    Button:

        text: "Enter New Keyword?"
        height: 100
        width: 200
        on_release:
            app.root.current = "main"

What should I do?


回答1:


    self.load = 10 # the value is supposed to increase after each function
    #self.update_value()
    time.sleep(10)
    SEOProgram.remove_urls()
    self.load = 30

time.sleep causes the program to...well, sleep. It isn't doing anything in that time, including the stuff you want to happen such as drawing the new state of the progress bar.

Instead of running everything one by one in a single long function, which will block the gui drawing until it finishes, use Clock.schedule_once(next_func, 0), where next_func is the next function you want to happen. This will schedule it for the next frame, after the progress bar has been updated. You can chain as many things as you like this way.



来源:https://stackoverflow.com/questions/61019432/cant-get-kivy-progress-bar-to-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!