【发布时间】:2018-06-14 16:01:04
【问题描述】:
我正在尝试在我的 kivy 程序中实现一个回收视图小部件。它工作正常,响应 on press 事件等,但是当它绘制时,它添加了一个不适合其他格式的额外行。这是我的代码的相关部分,有很多所以我只包括了这个,但如果需要我会发布更多。任何帮助表示赞赏。我喜欢 kivy,但这是我用过的最奇怪的小部件。
输出
py 文件
class MessageBox(Popup):
def popup_dismiss(self):
self.dismiss()
class SelectableButton(RecycleDataViewBehavior, Button):
index = None
def refresh_view_attrs(self, rv, index, data):
""" Catch and handle the view changes """
self.index = index
return super(SelectableButton, self).refresh_view_attrs(rv, index, data)
def on_press(self):
self.parent.selected_value = 'Selected: {}'.format(self.parent.btn_info[int(self.id)])
def on_release(self):
MessageBox().open()
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
selected_value = StringProperty('')
btn_info = ListProperty(["ButtonText" for x in range(0,2)])
class MainScreen(RecycleView):
rv_layout = ObjectProperty(None)
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
self.data = [{'text': "Button" + str(x), 'id': str(x)} for x in range(0,2)]
kv文件
<MessageBox>:
title: 'Popup Message Box'
size_hint: None, None
size: 400, 400
BoxLayout:
orientation: 'vertical'
#Label:
#text: app.root.rv_layout.selected_value
Button:
size_hint: 1, 0.2
text: 'OK'
on_press:
root.dismiss()
<SelectableButton>:
orientation: 'horizontal'
Button:
size_hint_x: .10
text: '+'
#font_size: 50
texture_size: self.width, self.height
size: self.texture_size
Button:
size_hint_x: .90
text: root.text
#font_size: 50
texture_size: self.width, self.height
size: self.texture_size
<MainScreen>:
pos_hint: {'x': 0, 'y': .11}
size_hint: 1, .70
viewclass: 'SelectableButton'
SelectableRecycleBoxLayout:
id: layout
default_size_hint: 1, 1
#size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
【问题讨论】: