Kivy的ScreenManager不显示第一个屏幕。

huangapple go评论72阅读模式
英文:

Kivy ScreenManager not showing first screen

问题

I understand that you want a translation of the provided code. Here's the translated code:

我知道这个问题以前已经被提出过但是没有一个解决方案适用于我

我有一个简单的登录界面
```python
class Login(Screen):
    def build(self):
        self.window = GridLayout()
        self.window.cols = 1
        self.window.size_hint = (0.6, 0.7)
        self.window.pos_hint = {"center_x": 0.5, "center_y": 0.5}

        self.window.add_widget(Image(
            source="logo.png",
            size_hint=(10, 10)
        ))

        self.username = Label(
            text="Username",
            font_size=50,
            bold=True
        )
        self.window.add_widget(self.username)

        self.userinp = TextInput(
            multiline=False,
            padding_y=(20, 20),
            size_hint=(1, 0.8)
        )
        self.window.add_widget(self.userinp)

        self.password = Label(
            text="Password",
            font_size=50,
            bold=True
        )
        self.window.add_widget(self.password)

        self.passwordinp = TextInput(
            multiline=False,
            size_hint=(1, 0.8)
        )
        self.window.add_widget(self.passwordinp)

        self.login = Button(
            text="Login",
            size_hint=(1, 0.8),
            bold=True
        )
        self.login.bind(on_press=self.callback)
        self.window.add_widget(self.login)

        return self.window

    def callback(self, instance):
        print("button pressed")
        username = self.userinp.text
        password = self.passwordinp.text
        try:
            with open('passwords.json') as f:
                data = json.load(f)
        except FileNotFoundError:
            print("Error")
            return {}
        if username not in data:
            print('User not found')
            return
        elif data[username] != password:
            self.login.text = ('Incorrect password')
            return
        else:
            self.login.text = ('Login successful')
            self.cusername = username,
            root.manager.current = "Home"

如果我将其独立运行,将Login的继承改为App并使用以下代码:

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

那么一切都正常工作。但是,如果我尝试使用ScreenManager并将Login设置为我的第一个屏幕,我会得到一个空白窗口。

class AppManager(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(Login(name="Login"))
        # sm.add_widget(home(name="Home"))
        sm.current = "Login"
        return sm

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

我可以看到AppManagerbuild被调用,因为如果我将当前设置为其他任何值,控制台会报错。看起来Loginbuild没有被执行,但我无法找出原因。

英文:

I know this question has been asked before, but none of the solutions works for me.

I have a simple login screen:

class Login(Screen):
    def build(self):
        self.window = GridLayout()
        self.window.cols = 1
        self.window.size_hint = (0.6, 0.7)
        self.window.pos_hint = {"center_x": 0.5, "center_y": 0.5}

        self.window.add_widget(Image(
            source="logo.png",
            size_hint=(10, 10)
        ))

        self.username = Label(
            text="Username",
            font_size=50,
            bold=True
        )
        self.window.add_widget(self.username)

        self.userinp = TextInput(
            multiline=False,
            padding_y=(20, 20),
            size_hint=(1, 0.8)
        )
        self.window.add_widget(self.userinp)

        self.password = Label(
            text="Password",
            font_size=50,
            bold=True
        )
        self.window.add_widget(self.password)

        self.passwordinp = TextInput(
            multiline=False,
            size_hint=(1, 0.8)
        )
        self.window.add_widget(self.passwordinp)

        self.login = Button(
            text="Login",
            size_hint=(1, 0.8),
            bold=True
        )
        self.login.bind(on_press=self.callback)
        self.window.add_widget(self.login)

        return self.window

    def callback(self, instance):
        print("button pressed")
        username = self.userinp.text
        password = self.passwordinp.text
        try:
            with open('passwords.json') as f:
                data = json.load(f)
        except FileNotFoundError:
            print("Error")
            return {}
        if username not in data:
            print('User not found')
            return
        elif data[username] != password:
            self.login.text = ('Incorrect password')
            return
        else:
            self.login.text = ('Login successful')
            self.cusername = username,
            root.manager.current = "Home"

If I start this in isolation, changing Login's inheritance to App and using:

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

Then it all works. However, if I try to use ScreenManager and make Login my first screen, I get a blank window.

class AppManager(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(Login(name = "Login"))
        #sm.add_widget(home(name = "Home"))
        sm.current = "Login"
        return sm



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

I can see that the AppManager build is being called because I get an error on the console if I set current to anything else. It looks like the Login build isn't being executed, but I can't work out why.

答案1

得分: 1

你已为您的Login类定义了一个build()方法,但与App类不同,没有任何东西会调用您的build()方法。您可以通过在__init__()方法中调用该build()方法来纠正此问题,像这样:

class Login(Screen):
def __init__(self, **kwargs):
super(Login, self).__init__(**kwargs)
self.add_widget(self.build())
英文:

You have defined a build() method for your Login class, however, unlike an App class, nothing is going to call your build() method. You can correct this by just calling that build() method in an __init__() method, like this:

class Login(Screen):
def __init__(self, **kwargs):
super(Login, self).__init__(**kwargs)
self.add_widget(self.build())

huangapple
  • 本文由 发表于 2023年5月17日 19:29:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定