【问题标题】:Can not load kivy file from kv variable无法从 kv 变量加载 kivy 文件
【发布时间】:2021-02-20 14:09:20
【问题描述】:

所以我正在开发我的第一个应用程序,在学习 kivy 和观看 youtube 视频时,我看到人们加载 KV 文件的不同方式,其中一种是放入一个变量并从构建方法中返回它。每当我这样做时,不一定会抛出任何错误,但它不会正确加载窗口,并且为什么没有加载 KV 文件是没有意义的。如果有人能指出我正确的方向,我将不胜感激,代码如下。

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager



class LoginLayout(Widget):
    def login(self, **kwargs):
        print("Login function working")
        username = self.ids.username.text
        password = self.ids.password.text
        print(username)
        print(password)

kv = Builder.load_file('loginScreen.kv')


class LoginScreen(App):

    def build(self):
        return kv


app = LoginScreen()
app.run()

kv文件

<LoginLayout>:
    BoxLayout:
        orientation: 'vertical'
        size: root.width, root.height

        Label:
            text: 'Username'

        TextInput:
            id: username
            multiline: False
            size_hint: (.5, .3)
            pos_hint: {'center_x' : .5}


        Label:
            text: 'Password'

        TextInput:
            id: password
            multiline: False
            size_hint: (.5, .3)
            pos_hint: {'center_x' : .5}

        Button:
            text: 'Login'
            size_hint: (.2, .8)
            pos_hint: {'center_x' : 0.5}
            on_release: root.login()

        Button:
            text: 'Create Account'
            size_hint: (.2, .8)
            pos_hint: {'center_x' : 0.5}


        Button:
            text: 'Forgot login Info'
            size_hint: (.2, .8)
            pos_hint: {'center_x' : 0.5}

【问题讨论】:

    标签: python python-3.x kivy kivymd


    【解决方案1】:

    问题是您的kv 没有定义根小部件,它只定义了如何构建LoginLayout 的规则。一个简单的解决方法是从LoginLayout 周围删除&lt;&gt;

    【讨论】:

    • 谢谢,非常感谢。一旦你把小东西拿下来,Kivy 就会很有趣。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多