无法更新kivy进度条

2024-10-04 09:22:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在构建一个应用程序,我一直在努力让进度条在每个函数调用后更新。在下面的代码中,我的意图是调用一个函数,然后一旦该函数完成运行,我将用一个新值更新self.load属性。在给self.load分配了一个新值之后,我想把它传递到kivy中的进度条上,这样它的值就会增加,并显示在屏幕上

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()

以下是kv文件:

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"

我该怎么办


Tags: textfromimportselftimevaluemainload
1条回答
网友
1楼 · 发布于 2024-10-04 09:22:46
    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导致程序…嗯,睡眠。在这段时间里,它没有做任何事情,包括你想做的事情,比如画进度条的新状态

使用Clock.schedule_once(next_func, 0),而不是在单个长函数中逐个运行所有内容(这将阻止gui绘图,直到绘图完成),其中next_func是您希望执行的下一个函数。这将在更新进度条后为下一帧安排进度。你可以用这种方式链接任意多的东西

相关问题 更多 >