我正在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:
如果我用默认的Switch对象替换.kv文件中的defSwitch:那么渲染就好了。为什么我不能使用自定义对象?
答案 0 :(得分:1)
如果您想使用在.py
文件中使用kv语言创建的自定义类,则还需要在.kv
文件中设置它:
<defScreen>: # class def
<LoginScreen>:
...
defScreen: # object
以便它知道要查找的内容。
此外,我还建议您使用kv语言的驼峰式案例,因为situations解析器不会将该短语识别为小部件。