首页 > 解决方案 > 如何解决Kivy中的“.kv多次加载”错误?

问题描述

我在 Kivy 中有一个非常简单的程序,有 3 个窗口。但是当我运行它时,它说

The file C:\Users\ab79\Documents\GPX WEATHER\weather.kv is loaded multiples times, you might have unwanted behaviors.

我可以运行它,但实际上有一个不需要的行为,我的三个窗口之一消失了!当我现在运行代码时,它会跳过中间的“infoWindow”。我可以观察到它,因为我放置了 FloatLayout 而不是 GridLayout,但是错误消息之前已经在这里了。我不明白出了什么问题。

这是最小的代码

Python

today = datetime.datetime.now()

class ImportFileWindow(Screen):
    pass

class InfoWindow(Screen):
    pass

class ResultWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass


kv = Builder.load_file("weather.kv")

class WeatherApp(App):
    def build(self):
        return kv

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

基维

WindowManager:
    ImportFileWindow:
    InfoWindow:
    ResultWindow:



<Label>    
    canvas.before:
        Color:
            rgba: 1, 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size    
    font_size:40
    color:  0,0,0,1

<Button>
    font_size:40
    color:  0,0,0,1
    background_normal: ''

<ImportFileWindow>:
    name: "import"


    Button:
        text: "Importer"
        on_release:
            app.root.current = "info"
            root.manager.transition.direction="left"


<InfoWindow>:
    name: "info"
    day: day


    FloatLayout:
        cols:2
        size:root.width,root.height/2


        Label:
            text:"Jour :"

        TextInput:
            id:day
            multiline:False

    Button:
        text:"Valider"
        on_release:
            app.root.current="result"
            root.manager.transition.direction="left"
    Button:
        text:"Retour"
        on_release:
            app.root.current="import"
            root.manager.transition.direction="right"


<ResultWindow>:
    name: "result"

    Button:
        text: "Retour"
        on_release:
            app.root.current="info"
            root.manager.transition.direction="right"



       ```

The error is are since the begining but the real issues are here since I use a FloatLayout instead of GridLayout.
Any ideas ?

:)

标签: pythonkivy

解决方案


该文件weather.kv会自动加载,请参阅文档。但是你也明确地加载它:

kv = Builder.load_file("weather.kv")

我认为您只需要删除上面的行,并将您的WeatherApp课程更改为:

class WeatherApp(App):
    pass

推荐阅读