我现在正在学习kivy。我正在为一个朋友开发我的第一个应用程序,一个非常简单的。但我正面临着这样的错误:
每当我单击"create account“时,名称为”Login(Screen)“的页面加载为空白。我在kivy文件中创建的小部件都没有显示出来。
代码如下:
==========================================================================
python文件:
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文件:
<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'
=========================================================================
知道我错过了什么吗?第一个屏幕完全加载。如果我交换订单,登录屏幕加载正常。但无论内容是什么,第二个屏幕都是空白的。只要它是第二个要加载的屏幕,它就会返回空白。
谢谢!
发布于 2018-08-06 10:58:49
在Gerenciador
的__init__()
方法中,您创建了Login
和BoasVindas
屏幕,但也在.kv
文件中创建了它们。你不应该同时做这两件事。如果您在.kv
文件中创建它们,则您的代码可能如下所示:
python文件:
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文件:
<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()
方法中一样),将创建Login
和BoasVindas
屏幕作为它的子类。请注意,我在.kv
文件中为这些屏幕添加了name
属性。
https://stackoverflow.com/questions/51697982
复制相似问题