python - 如何在 main.py 中捆绑 Kivy 代码

标签 python kivy

我正在尝试构建一个 kivy 应用程序,我需要将我的 .py 文件和 .kv 文件放在名为 main.py 的文件中。我该怎么做呢?谢谢。

最佳答案

为此,您需要使用 Builder。 Docs 下面的代码说明了这一点。

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder

Builder.load_string("""
#:import hex kivy.utils.get_color_from_hex

<Root>:
    cols: 2
    canvas:
        Color:
            rgba: 1, 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

    Label:
        canvas.before:
            Color:
                rgb: 39/255., 174/255., 96/255.
            Rectangle:
                pos: self.pos
                size: self.size
        text: "rgb: 39/255., 174/255., 96/255."
    Label:
        canvas.before:
            Color:
                rgba: 39/255., 174/255., 96/255., 1
            Rectangle:
                pos: self.pos
                size: self.size
        text: "rgba: 39/255., 174/255., 96/255., 1"
    Label:
        canvas.before:
            Color:
                hsv: 145/360., 77.6/100, 68.2/100
            Rectangle:
                pos: self.pos
                size: self.size
        text: "hsv: 145/360., 77.6/100, 68.2/100"
    Label:
        canvas.before:
            Color:
                rgba: hex('#27ae60')
            Rectangle:
                pos: self.pos
                size: self.size
        text: "rgba: hex('#27ae60')"
""")


class Root(GridLayout):
    pass


class ColorusageApp(App):
    def build(self):
        return Root()


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

关于python - 如何在 main.py 中捆绑 Kivy 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41594252/

相关文章:

python - 将一列零添加到 csr_matrix

python - Django 错误 "Reverse for ' x' 未找到。 'x' 不是有效的 View 函数或模式名称。”,但 x 是模式名称

python - 如何在 Python 中向模拟函数提供条件参数?

python - 如何使用python更改kivy语言中的标签文本

python - kivy gridlayout,停止动态调整子级大小

python - 元组对列表到字典中

python - float 末尾的后缀e+number在python中是什么意思?

android - 使用 BlueStacks 调试 Kivy android 应用程序

python - Kivy 布局高度适应子部件的高度

python - 如何加载.kv 文件?