【问题标题】:Python dont see kv filePython 看不到 kv 文件
【发布时间】:2021-08-30 05:01:28
【问题描述】:

我想用 kivi 创建一个小程序。但是当我编写kv代码时,它不起作用。

from kivy.uix.button import Button
from kivymd.app import MDApp
from kivy.uix.image import Image
from kivy.core.window import Window

Window.size = (700, 300)

def launchClicker(instance):
     print('The button <%s> is being pressed' % instance.text)


class MainApp(MDApp):
    def build(self):
      pass

MainApp().run()

及kv文件代码:

<MainApp>
GridLayout:
    cols:2

    GridLayout:
        cols:1
        rows:3

        Label:
            text: "Your Mom"
        Label:
            text: "Your Mom"
        Label:
            text: "Your Mom"
    Button:
        text: "Submit"

当我运行项目时,它没有任何问题或错误。但我只看到没有标签和按钮的窗口。

enter image description here

【问题讨论】:

  • kv 文件必须命名为 main.kv。是这样吗。
  • 而且您不需要&lt;MainApp&gt;: 行。

标签: python python-3.x kivy kivymd


【解决方案1】:

将其包含在您的 Python 代码中

from kivy.lang import Builder

然后在您的MainApp 类中包含以下内容:

BLD = Builder.load_file("Example.kv")

然后修改Build():

def build():
    return BLD

所以这将是你的最终代码:

from kivy.uix.button import Button
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.image import Image
from kivy.core.window import Window

Window.size = (700, 300)

def launchClicker(instance):
     print('The button <%s> is being pressed' % instance.text)

BLD = Builder.load_file("Example.py")

class MainApp(MDApp):
    def build(self):
      return BLD
if __name__ == "__main__":
MainApp().run()

请告诉我这是否有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多