【问题标题】:Kivy Popup dynamically change heightKivy Popup 动态改变高度
【发布时间】:2018-11-05 11:05:31
【问题描述】:

我有以下 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.height 绑定到content.height,但它不起作用?

【问题讨论】:

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


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 2012-12-22
      • 1970-01-01
      • 2016-06-17
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多