【发布时间】:2021-02-20 14:09:20
【问题描述】:
所以我正在开发我的第一个应用程序,在学习 kivy 和观看 youtube 视频时,我看到人们加载 KV 文件的不同方式,其中一种是放入一个变量并从构建方法中返回它。每当我这样做时,不一定会抛出任何错误,但它不会正确加载窗口,并且为什么没有加载 KV 文件是没有意义的。如果有人能指出我正确的方向,我将不胜感激,代码如下。
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
class LoginLayout(Widget):
def login(self, **kwargs):
print("Login function working")
username = self.ids.username.text
password = self.ids.password.text
print(username)
print(password)
kv = Builder.load_file('loginScreen.kv')
class LoginScreen(App):
def build(self):
return kv
app = LoginScreen()
app.run()
kv文件
<LoginLayout>:
BoxLayout:
orientation: 'vertical'
size: root.width, root.height
Label:
text: 'Username'
TextInput:
id: username
multiline: False
size_hint: (.5, .3)
pos_hint: {'center_x' : .5}
Label:
text: 'Password'
TextInput:
id: password
multiline: False
size_hint: (.5, .3)
pos_hint: {'center_x' : .5}
Button:
text: 'Login'
size_hint: (.2, .8)
pos_hint: {'center_x' : 0.5}
on_release: root.login()
Button:
text: 'Create Account'
size_hint: (.2, .8)
pos_hint: {'center_x' : 0.5}
Button:
text: 'Forgot login Info'
size_hint: (.2, .8)
pos_hint: {'center_x' : 0.5}
【问题讨论】:
标签: python python-3.x kivy kivymd