Kivy Popup动态改变高度

时间:2016-12-18 11:30:16

标签: python python-3.x kivy kivy-language

我有以下Kivy Option Popup

s ='''
<OptionPopup>:
    id: optionPopup
    size_hint : (None,None)
    width : min(0.95 * self.window.width, dp(500))
    title: "Option Title"
    height: content.height
    BoxLayout:
        id: content
        orientation: 'vertical'
        spacing: '5dp'
        height: contentButtons.height + cancelButton.height
        BoxLayout:
            id: contentButtons
            orientation: 'vertical'
            spacing: '0dp'
            height : self.minimum_height

        SettingSpacer:
        Button:
            id:cancelButton
            size_hint_y : None
            height: '50dp'
            text: "Back"
            on_release: optionPopup._dismiss()


'''
Builder.load_string(s)

此弹出窗口仅在我的应用程序中存在一次,我将动态添加按钮到optionPopup.ids["contentButtons"]。这意味着contentButton布局minimum_height会发生变化。如何正确调整父Boxlayouts content和PopUp窗口的大小? 上述kv选项似乎做了正确的事情,例如将optionPopup.height绑定到content.height,但它不起作用?

The popup

1 个答案:

答案 0 :(得分:1)

正确的kv设置

s ='''
<OptionPopup>:
    id: optionPopup
    size_hint : (None,None)
    width : min(0.95 * self.window.width, dp(500))
    title: "Option Title"
    height: dp(content.height) + dp(80)
    BoxLayout:
        id: content
        size_hint : (1,None)
        orientation: 'vertical'
        spacing: '5dp'
        height: dp(content.minimum_height)
        BoxLayout:
            size_hint : (1,None)
            id: contentButtons
            orientation: 'vertical'
            spacing: '0dp'
            height : dp(self.minimum_height)

        SettingSpacer:
        Button:
            id:cancelButton
            size_hint_y : None
            height: '50dp'
            text: "Back"
            on_release: optionPopup._dismiss()


'''
Builder.load_string(s)

class OptionPopup(Popup): 
    def __init__(self,**kwargs):
        self.window= Window
        super(OptionPopup,self).__init__(**kwargs)
        self.content = self.ids["content"]
        self.contentButtons = self.ids["contentButtons"]

    def _dismiss(self,*largs):
        self.dismiss()

    def open(self):
        super(OptionPopup,self).open()

    def _validate(self,instance):
        if self.optionCallBack is not None:
            self.optionCallBack(instance.optId)
        self.dismiss()

    def setOptions(self,options, callBack):
        self.optionCallBack = callBack
        print('OptionPopup::setOptions', options)
        print('OptionPopup::setOptionCallback: \n option changes go to: ',self.optionCallBack)
        self.contentButtons.clear_widgets()
        for optId,name in options.items():
            b = Button(text=name, size_hint_y=None, height='50dp')
            b.optId = optId
            b.bind(on_release=self._validate)
            self.contentButtons.add_widget(b)

    def setTitle(self,text):
        self.title = text

您可以通过将此代码添加到您的应用程序来测试此代码:

# Test the popup
o = OptionPopup()
o.setOptions({'opt1' : 'Options 1','opt2' : 'Options 2', 'opt3' : 'Options 3'})
o.open()