在 .kv 文件中定义时,Kivy 对象不呈现
Kivy object doesn't render when defined in .kv file
我正在 kivy 中开发一个应用程序,目前正试图弄清楚为什么当我将对象添加到 Python 而不是在 .kv 文件中时我可以得到一个对象来渲染。
这很好用,渲染背景图像并在其顶部添加一个开关。
class LoginScreen(Screen):
def __init__(self,**kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.layout = BoxLayout(orientation='vertical')
self.add_widget(self.layout)
self.layout.add_widget(defSwitch())
def on_touch_up(self,touch):
if self.collide_point(*touch.pos):
self.parent.current = 'data'
class defSwitch(Switch):
active = False
.kv 文件所在位置:
<LoginScreen>:
imgname: './images/' + 'login.jpg'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: self.imgname
除了背景图片,这不会渲染任何东西:
class LoginScreen(Screen):
def on_touch_up(self,touch):
if self.collide_point(*touch.pos):
self.parent.current = 'data'
class defSwitch(Switch):
active = False
在这种情况下,.kv 文件具有:
<LoginScreen>:
imgname: './images/' + 'login.jpg'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: self.imgname
BoxLayout:
orientation: 'vertical'
defSwitch:
如果我将 .kv 文件中的 defSwitch: 替换为默认的 Switch 对象,则它可以正常呈现。为什么我不能使用自定义对象?
如果您想使用在 .py
文件中创建的带有 kv 语言的自定义 class,您也需要在 .kv
文件中进行设置:
<defScreen>: # class def
<LoginScreen>:
...
defScreen: # object
这样它就知道要查找什么。
此外,我还想推荐您在 kv 语言中使用驼峰式大小写,因为 其中解析器不会将短语识别为小部件。
我正在 kivy 中开发一个应用程序,目前正试图弄清楚为什么当我将对象添加到 Python 而不是在 .kv 文件中时我可以得到一个对象来渲染。
这很好用,渲染背景图像并在其顶部添加一个开关。
class LoginScreen(Screen):
def __init__(self,**kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.layout = BoxLayout(orientation='vertical')
self.add_widget(self.layout)
self.layout.add_widget(defSwitch())
def on_touch_up(self,touch):
if self.collide_point(*touch.pos):
self.parent.current = 'data'
class defSwitch(Switch):
active = False
.kv 文件所在位置:
<LoginScreen>:
imgname: './images/' + 'login.jpg'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: self.imgname
除了背景图片,这不会渲染任何东西:
class LoginScreen(Screen):
def on_touch_up(self,touch):
if self.collide_point(*touch.pos):
self.parent.current = 'data'
class defSwitch(Switch):
active = False
在这种情况下,.kv 文件具有:
<LoginScreen>:
imgname: './images/' + 'login.jpg'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: self.imgname
BoxLayout:
orientation: 'vertical'
defSwitch:
如果我将 .kv 文件中的 defSwitch: 替换为默认的 Switch 对象,则它可以正常呈现。为什么我不能使用自定义对象?
如果您想使用在 .py
文件中创建的带有 kv 语言的自定义 class,您也需要在 .kv
文件中进行设置:
<defScreen>: # class def
<LoginScreen>:
...
defScreen: # object
这样它就知道要查找什么。
此外,我还想推荐您在 kv 语言中使用驼峰式大小写,因为