首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python + Kivy (第二个屏幕为空白加载)

Python + Kivy (第二个屏幕为空白加载)
EN

Stack Overflow用户
提问于 2018-08-06 04:23:27
回答 1查看 651关注 0票数 1

我现在正在学习kivy。我正在为一个朋友开发我的第一个应用程序,一个非常简单的。但我正面临着这样的错误:

每当我单击"create account“时,名称为”Login(Screen)“的页面加载为空白。我在kivy文件中创建的小部件都没有显示出来。

代码如下:

==========================================================================

python文件:

代码语言:javascript
复制
from kivy.app import App

from kivy.uix.screenmanager import Screen, ScreenManager


class Gerenciador(ScreenManager):
    pass

class BoasVindas(Screen):
    pass

class Login(Screen):
    def logar(self, usuario, senha):
        print("usuario={0}, senha={1}".format(usuario, senha))

class Resultado(Screen):
    pass

class LoginApp(App):

    def build(self):
        return Gerenciador()

LoginApp().run()

========================================================================

kivy文件:

代码语言:javascript
复制
<Gerenciador>:
BoasVindas:
    name: 'boasvindas'
    BoxLayout:
        orientation:'vertical'
        Label:
            text:'Faça o seu Login ou crie uma nova conta'
        Button:
            text:'Login'

        Button:
            text:'Criar nova conta'
            on_release:root.current='login'


Login:
    name: 'login'
    BoxLayout:
        TextInput:
            id:usuario
            hint_text:'Usuário'
            multiline: False
        TextInput:
            id:senha
            hint_text:'Senha'  
            multiline: False
            password: True
        Button:
            id:'btn'
            text:'Ok'
            on_press: self.parent.parent.logar(usuario.text, senha.text)
            on_release:root.current='boasvindas'

=========================================================================

知道我错过了什么吗?第一个屏幕完全加载。如果我交换订单,登录屏幕加载正常。但无论内容是什么,第二个屏幕都是空白的。只要它是第二个要加载的屏幕,它就会返回空白。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-08-06 10:58:49

Gerenciador__init__()方法中,您创建了LoginBoasVindas屏幕,但也在.kv文件中创建了它们。你不应该同时做这两件事。如果您在.kv文件中创建它们,则您的代码可能如下所示:

python文件:

代码语言:javascript
复制
from kivy.app import App

from kivy.uix.screenmanager import Screen, ScreenManager


class Gerenciador(ScreenManager):
    pass

class BoasVindas(Screen):
    pass

class Login(Screen):
    pass

class Resultado(Screen):
    pass

class TesteLogin(App):

    def build(self):
       return Gerenciador()

TesteLogin().run()

kv文件:

代码语言:javascript
复制
<Gerenciador>:
    BoasVindas:
        name: 'boasvindas'
        BoxLayout:
            orientation:'vertical'
            Label:
                text:'Faça o seu Login ou crie uma nova conta'
            Button:
                text:'Login'

            Button:
                text:'Criar nova conta'
                on_release:root.current='login'

    Login:
        name: 'login'
        BoxLayout:
            TextInput:
                id:usuario
                hint_text:'Usuário'
                multiline: False
            TextInput:
                id:senha
                hint_text:'Senha'
                multiline: False
                password: True
            Button:
                id:'btn'
                text:'Ok'
                #on_press:self.parent.parent.logar(usuario.text, senha.text)
                on_release:root.current='boasvindas'

在您的.kv文件中,当一个类名被<>括起来时,这意味着它是用于构建该类的模板,但实际上并不创建该类。创建了不带<>的类,但.kv文件中的类从属于Gerenciador类。因此,在创建Gerenciador类的实例时(就像在App.build()方法中一样),将创建LoginBoasVindas屏幕作为它的子类。请注意,我在.kv文件中为这些屏幕添加了name属性。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51697982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档