无法在Kivy中显示进度栏

时间:2019-11-18 01:36:19

标签: python kivy

运行此代码时,屏幕全黑,并显示进度条。我不知道为什么。有人可以帮我还是解释一下为什么屏幕上什么都没有显示。预先感谢!

progressbar.py文件

import kivy
from kivy.app import App
kivy.require('1.9.0')
from kivy.uix.label import Label
from kivy.uix.progressbar import ProgressBar
from kivy.uix.boxlayout import BoxLayout

class ProgBar(BoxLayout):
    pass

class mainApp(App):
    def build(self):
        return ProgBar()


if __name__ == '__main__':
    mainApp().run()

progressbar.kv文件

<ProgBar>:

    orientation: 'vertical'

    canvas: 
        Color: 
            rgb: .45, .28, .5
        Rectangle: 
            pos: self.pos 
            size: self.size 


    Label: 
        text: '[size = 40px]Progress Bar 1 (at .25)'
        color: .5, 0, .5, 1
        markup: True


    ProgressBar: 
        value: .25
        min: 0
        max: 1
        pos_hint: {'x':.1} 
        size_hint_x: .8


    Label: 
        text: '[size = 40px]Progress Bar 2 (at .55)'
        color: .5, 0, .5, 1
        markup: True


    ProgressBar: 
        value: .55
        min: 0
        max: 1
        pos_hint: {'x':.1} 
        size_hint_x: .8

1 个答案:

答案 0 :(得分:1)

您在代码的哪一部分表示.kv加载?嗯,您无处可做,默认情况下,如果从App继承的类的名称是您无法满足的xApp,则kivy会加载x.kv,因此必须通过Builder.load_file()使用Builder显式加载它。方法:

import kivy

kivy.require("1.9.0")

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang.builder import Builder

Builder.load_file("progressbar.kv")


class ProgBar(BoxLayout):
    pass


class mainApp(App):
    def build(self):
        return ProgBar()


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