与kivy的黑屏

时间:2018-01-04 01:52:39

标签: python kivy

我是kivy的新手,并且使用了一些教程,但我只是在视频开发人员在屏幕上获取他们的小部件时才会出现黑屏。我尝试了多种解决方案,但我真的不明白问题所在。我想在这里建立一个登录界面是我写的代码:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput 

class LoginScreen(BoxLayout):
    def __int__(self, **kwargs):
        super(LoginScreen,self).__int__(**kwargs)
        sm=ScreenManager
        Screen=Screen(name="screen")
        layout=BoxLayout(orientation='vertical')
        self.Username = TextInput(multiline=False)
        self.Password = TextInput(multiline=False, password=True)
        layout.add_widget(Label(text="username"))
        layout.add_widget(self.Username)
        layout.add_widget(Label(text="password"))
        layout.add_widget(self.Password)
        Screen.add_widget(layout)
        sm.add_widget(Screen)
        return sm   

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

if __name__ == "__main__":
    simplekivy().run()

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您的代码有以下错误:

  • 构造函数方法名为__init__,不称为__int__
  • 构造函数方法不应返回任何内容
  • 变量的调用不应与类或函数相同,我的意思是以下代码:Screen=Screen(name="screen")
  • 最好分段是用你的代码来分隔你的代码,我想你想创建一个ScreenScreen的类名,所以创建另一个类是ScreenManager。
  • 另一个错误是您没有导入Label。
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput 
from kivy.uix.label import Label

class LoginScreen(Screen):
    def __init__(self, *args, **kwargs):
        super(LoginScreen,self).__init__(*args, **kwargs)
        layout=BoxLayout(orientation='vertical')
        self.Username = TextInput(multiline=False)
        self.Password = TextInput(multiline=False, password=True)
        layout.add_widget(Label(text="username"))
        layout.add_widget(self.Username)
        layout.add_widget(Label(text="password"))
        layout.add_widget(self.Password)
        self.add_widget(layout) 

class Manager(ScreenManager):
    def __init__(self, *args, **kwargs):
        super(ScreenManager,self).__init__(*args, **kwargs)
        self.add_widget(LoginScreen())

class simplekivy(App):
    def build(self):
        return Manager()

if __name__ == "__main__":
    simplekivy().run()