2016-12-18 52 views
0

我有以下Kivy选项弹出Kivy弹出动态改变高度

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.heightcontent.height,但它不起作用?

The popup

回答

0

正确的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()