我们在Kivy应用程序的主菜单中使用手风琴,并且正在努力解决两个问题:
1)当我们打开手风琴项目时,当我们第二次按下该项目时,该项目不会崩溃。让它崩溃的唯一方法是按下另一个手风琴项目。这只是Kivy的手风琴小部件的工作方式还是有办法改变这个设置?
2)当我们的屏幕打开时,列表中的最后一个手风琴项目显示为从开始展开。我们如何让这个手风琴项目在折叠位置加载?我们尝试设置崩溃:在我们的kv文件中为True,但这不起作用
我们的kv代码如下:
GeneralBoxLayout:
GridLayout1:
BodyBoxLayout:
rows: 2
GeneralTextGridLayout:
size_hint: (1,.07)
GeneralTextLabel:
text: '[color=0046C3]Select a topic[/color]'
ScrollView:
size_hint: (1,.93)
HomeGridLayout:
Accordion:
orientation: "vertical"
AccordionItem:
title: "Topic 1"
background_normal: 'img/blue_button5.png'
background_selected: 'img/blue_button5.png'
size_hint_y: None
height: '50dp'
font_size: '12sp'
border: 20, 20, 20, 20
GameButton0:
text: 'Game 1'
AccordionItem:
title: 'Topic 2'
background_normal: 'img/blue_button5.png'
background_selected: 'img/blue_button5.png'
GameButton0:
text: 'Game 1'
AccordionItem:
title: 'Topic 3'
background_normal: 'img/blue_button5.png'
background_selected: 'img/blue_button5.png'
GameButton0:
text: 'Game 1'
AccordionItem:
title: 'Topic 4'
background_normal: 'img/blue_button5.png'
background_selected: 'img/blue_button5.png'
GameButton0:
text: 'Game 1'
AccordionItem:
title: 'Topic 5'
background_normal: 'img/blue_button5.png'
background_selected: 'img/blue_button5.png'
GameButton0:
text: 'Game 1'
AccordionItem:
title: 'Topic 6'
background_normal: 'img/blue_button5.png'
background_selected: 'img/blue_button5.png'
GameButton0:
text: 'Game 1'
AccordionItem:
title: 'Topic 7'
background_normal: 'img/blue_button5.png'
background_selected: 'img/blue_button5.png'
GameButton0:
text: 'Game 1'
AccordionItem:
title: 'Topic 8'
background_normal: 'img/blue_button5.png'
background_selected: 'img/blue_button5.png'
GameButton0:
text: 'Game 1'
AccordionItem:
title: 'Topic 9'
background_normal: 'img/blue_button5.png'
background_selected: 'img/blue_button5.png'
collapse: True
GameButton0:
text: ' Game 1'
FooterGridLayout:
ReturnButton:
text: 'Logout'
由于
答案 0 :(得分:1)
我不确定如何解决你的第一个问题,但是我为使手风琴在扩展位置开始所做的是使用折叠:我的第一个手风琴项目的假标志。
MyAccordion:
orientation: 'vertical'
MyItem: #The the top accordion item that needs to open expanded
title: 'First Item'
collapse: False
MyItem: #The the next accordion item, will be collapsed
title: 'Second Item'
MyItem: #The the next accordion item, will also be collapsed
title: 'Third Item'
答案 1 :(得分:0)
因此,我意识到这个问题已有5年历史了,但今天我自己遇到了这个问题,并且可以通过创建自定义Accordion
和{{1 }}类,并覆盖AccordionItem
和Accordion._do_layout
。我只是从它们的基类中复制并粘贴了这些方法,并做了以下一些小的更改。
AccordionItem.on_touch_down
# file: popupaccordion.py
from kivy.app import App
from kivy.uix.accordion import Accordion, AccordionItem
from kivy.logger import Logger
class PopUpAccordion(Accordion):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def _do_layout(self, dt):
children = self.children
if children:
all_collapsed = all(x.collapse for x in children)
else:
all_collapsed = False
# Changed below: if all items are collapsed, do nothing. This is what we want.
# if all_collapsed:
# children[0].collapse = False
orientation = self.orientation
min_space = self.min_space
min_space_total = len(children) * self.min_space
w, h = self.size
x, y = self.pos
if orientation == 'horizontal':
display_space = self.width - min_space_total
else:
display_space = self.height - min_space_total
if display_space <= 0:
Logger.warning('Accordion: not enough space '
'for displaying all children')
Logger.warning('Accordion: need %dpx, got %dpx' % (
min_space_total, min_space_total + display_space))
Logger.warning('Accordion: layout aborted.')
return
if orientation == 'horizontal':
children = reversed(children)
for child in children:
child_space = min_space
child_space += display_space * (1 - child.collapse_alpha)
child._min_space = min_space
child.x = x
child.y = y
child.orientation = self.orientation
if orientation == 'horizontal':
child.content_size = display_space, h
child.width = child_space
child.height = h
x += child_space
else:
child.content_size = w, display_space
child.width = w
child.height = child_space
y += child_space
class PopUpAccordionItem(AccordionItem):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.title_template = 'PopUpAccordionItemTitle'
def on_touch_down(self, touch):
if not self.collide_point(*touch.pos):
return
if self.disabled:
return True
if self.collapse:
self.collapse = False
return True
# Changed below: if item is not collapsed and user clicked the title bar, collapse.
if not self.collapse and self.container_title.collide_point(*touch.pos):
self.collapse = True
return super(AccordionItem, self).on_touch_down(touch)
class PopUpAccordionApp(App):
def build(self):
return PopUpAccordion()
if __name__ == "__main__":
PopUpAccordionApp().run()
请注意,kv文件包含一个kivy模板。我不确定这些如何工作,并且不建议使用kivy模板。但是,如果您想更改打开/关闭# file: popupaccordion.kv
[PopUpAccordionItemTitle@Label]:
text: ctx.title
normal_background: ctx.item.background_normal if ctx.item.collapse else ctx.item.background_selected
disabled_background: ctx.item.background_disabled_normal if ctx.item.collapse else ctx.item.background_disabled_selected
canvas.before:
Color:
rgba: self.disabled_color if self.disabled else self.color
BorderImage:
source: self.disabled_background if self.disabled else self.normal_background
pos: self.pos
size: self.size
PushMatrix
Translate:
xy: self.center_x, self.center_y
Rotate:
angle: 90 if ctx.item.orientation == 'horizontal' else 0
axis: 0, 0, 1
Translate:
xy: -self.center_x, -self.center_y
canvas.after:
PopMatrix
<PopUpAccordion>:
pos_hint: {"center_x": 0.5, "y": 0}
orientation: "vertical"
PopUpAccordionItem:
title: "test1"
Label:
text: "test item 1"
PopUpAccordionItem:
title: "test2"
Label:
text: "test item 2"
的按钮的外观,这(至少是一种方法)是可以做到的。还要注意,我们通过向其传递模板名称的字符串值来将PopUpAccordionItem
与其在PopUpAccordionItem
中的模板连接起来……看起来很奇怪,但是可以使用。