python - 以 python 呈现,但不以 kivy 语言呈现

标签 python python-2.7 kivy

这段代码:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput


class LoginScreen(GridLayout):

 def __init__(self, **kwargs):
     super(LoginScreen, self).__init__(**kwargs)
     self.cols = 2
     self.add_widget(Label(text='User Name'))
     self.username = TextInput(multiline=False)
     self.add_widget(self.username)
     self.add_widget(Label(text='password'))
     self.password = TextInput(password=True, multiline=False)
     self.add_widget(self.password)


class MyApp(App):

 def build(self):
     return LoginScreen()


if __name__ == '__main__':
 MyApp().run()

按预期工作并生成此窗口(窗口的大小有所调整):

enter image description here


然而,当在 kivy language 中实现相同的示例时它失败了:

Python 文件:

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

class LoginScreen(GridLayout):
    pass

class MyApp(App):
    def build(self):
        return LoginScreen()

if __name__ == '__main__':
    MyApp().run()

我的.kv文件:

#:kivy 1.0

<LoginScreen>:
    GridLayout:
        cols: 2
        Label:
            text: "Username"
        TextInput:
            multiline: False

        Label:
            text: "Password"
        TextInput:
            multiline: False

产生(再次调整大小):

enter image description here

为什么会这样,我该如何解决?

最佳答案

你不需要在 kivy 文件中创建 GridLayout

my.kv 文件中删除 GridLayout:,如下所示:

#:kivy 1.0

<LoginScreen>:
    cols: 2
    Label:
        text: "Username"
    TextInput:
        multiline: False

    Label:
        text: "Password"
    TextInput:
        multiline: False

否则,将在 LoginScreen 中创建另一个 GridLayout。

关于python - 以 python 呈现,但不以 kivy 语言呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20676219/

相关文章:

python-2.7 - 使用 matplotlib.image 从文件夹中读取图像

macos - Python 虚拟环境,Py2app 构建,wxpython 错误

python - 如何将 kivy 转换为视频文件

python - NLTK:从树值转换为 Lambda 函数表示法

python - 当我运行代码时,他们告诉我 "int"对象在第 4 行不可调用

python - android 中的 kivy 应用程序,带有 buildozer,终端循环在 "# Waiting for application to start."

python - 我如何在 kivy 中向后发送图像?

python - Django REST 框架 - 多个查找字段?

python - 值错误 : Unknown label type: 'unknown' when plotting SVM classifiers in the iris dataset

python - 识别出现在语料库文档中少于 1% 的单词