首页 > 解决方案 > Kivy Python 使用按钮打开 .kv 文件来更改 GUI

问题描述

我正在尝试使用一个按钮在屏幕上循环显示 GUI 这是我尝试使用主文件然后使用 2 个额外文件的代码。代码本身有效,我不断收到“未定义生成器”错误,该错误不会将新文件加载到屏幕上

目标是有一个按钮,它将屏幕切换到下一个屏幕并再次返回

任何帮助深表感谢

主文件

# import subprocess
# import kivy
from kivy.uix.button import Button

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import NumericProperty
from kivy.uix.floatlayout import FloatLayout

from kivy.config import Config

Config.set('graphics', 'resizable', False)
Config.set('graphics', 'width', '600')
Config.set('graphics', 'height', '300')


class CustomRow(FloatLayout):
    pass


class CustomGrid(FloatLayout):
    pass


class RotationApp(App):
    OilTemp = NumericProperty(0)
    print(OilTemp)

    def build(self):
        print("building...")
        return Builder.load_file('oiltemp.kv')


# the __name__ idiom executes when run from command line but not from import.
if __name__ == '__main__':
    RotationApp().run()

第二个文件

<oiltemp>:
    CustomRow:
        FloatLayout:
            id: 0
        FloatLayout:
            id: 1
        # etc, creating as many FloatLayouts as many number of columns you want.

    CustomGrid:
        CustomRow:
            id: 0
        CustomRow:
            id: 1


        Image:
            source: 'OilTempV2.png'
            size: self.texture_size

        Image:
            source: 'NeedleV1.png'
            size: self.texture_size
            pos_hint: {'center_x': .5, 'center_x': .5}

        Button:
            text: 'Next Screen'
            size_hint: None, None
            pos_hint: {'left_x': .5, 'bottom_y': .5}
            canvas.before:
                PushMatrix
                Rotate:
                    angle: 0
                    origin: self.center
            canvas.after:
                PopMatrix

第三个文件

<airtemp>:
    CustomRow:
        FloatLayout:
            id: 0
        FloatLayout:
            id: 1
        # etc, creating as many FloatLayouts as many number of columns you want.

    CustomGrid:
        CustomRow:
            id: 0
        CustomRow:
            id: 1


        Image:
            source: 'AirTempV1.png'
            size: self.texture_size

        Image:
            source: 'NeedleV1.png'
            size: self.texture_size
            pos_hint: {'center_x': .5, 'center_x': .5}

        Button:
            text: 'Next Screen'
            size_hint: None, None
            pos_hint: {'left_x': .5, 'bottom_y': .5}
            canvas.before:
                PushMatrix
                Rotate:
                    angle: 0
                    origin: self.center
            canvas.after:
                PopMatrix

标签: pythonuser-interfacekivy

解决方案


推荐阅读