无法从 kv 变量加载 kivy 文件
Can not load kivy file from kv variable
所以我正在开发我的第一个应用程序,在学习 kivy 和观看 youtube 视频时,我看到人们会以不同的方式加载他们的 KV 文件,一个是放入一个变量并从构建方法返回它.每当我这样做时,都不会抛出任何错误,但它不会正确加载 window 并且为什么不加载 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}
问题是您的 kv
没有定义根小部件,它只定义了如何构建 LoginLayout
的规则。一个简单的解决方法是从 LoginLayout
.
周围删除 <>
所以我正在开发我的第一个应用程序,在学习 kivy 和观看 youtube 视频时,我看到人们会以不同的方式加载他们的 KV 文件,一个是放入一个变量并从构建方法返回它.每当我这样做时,都不会抛出任何错误,但它不会正确加载 window 并且为什么不加载 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}
问题是您的 kv
没有定义根小部件,它只定义了如何构建 LoginLayout
的规则。一个简单的解决方法是从 LoginLayout
.
<>