表达式中使用的kv Langaje属性(
x:
,
y:
,
width:
)将予以遵守。当父母的
size
/
pos
更改,子小部件将相应更改。必须在Python类中提供此事件绑定:
class Book(Button):
def __init__(self, cover=BLANK_BOOK_COVER, **kwargs):
super(Book, self).__init__(**kwargs)
self.size_hint = (None, None)
self.book_cover = Image(source=cover)
self.book_cover.allow_stretch = True
self.book_cover.keep_ratio = False
self.add_widget(self.book_cover)
def on_size(self, *args):
self.book_cover.size = self.size
def on_pos(self, *args):
self.book_cover.pos = self.pos
要获得可点击的图像,一个更简单的选择是让您的类
Book
继承自
ButtonBehabior
和
Image
类别:
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
class Book(ButtonBehavior, Image):
def __init__(self, cover=BLANK_BOOK_COVER, **kwargs):
super(Book, self).__init__(**kwargs)
self.source = cover
self.size_hint = (None, None)
self.allow_stretch = True
self.keep_ratio = False