渲染python但不是kivy语言

时间:2013-12-19 07:56:07

标签: 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()

my.kv文件:

#:kivy 1.0

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

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

生成(稍微调整一下大小)

enter image description here

为什么会这样,我怎么可能解决它?

1 个答案:

答案 0 :(得分:4)

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

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

#:kivy 1.0

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

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

否则,将创建LoginScreen内的另一个GridLayout。