英文:
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()
我可以看到AppManager
的build
被调用,因为如果我将当前设置为其他任何值,控制台会报错。看起来Login
的build
没有被执行,但我无法找出原因。
英文:
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())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论