Kivy Popup 动态改变高度
Kivy Popup dynamically change height
我有以下 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)
这个 Popup 在我的应用程序中只存在一次,我将动态添加 Buttons 到 optionPopup.ids["contentButtons"]
。这意味着 contentButton
布局 minimum_height
已更改。如何正确调整父 Boxlayouts content
和 PopUp windows 的大小?
上面的 kv
选项似乎做了正确的事情,比如将 optionPopup.height
绑定到 content.height
,但它不起作用?
正确的 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()
我有以下 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)
这个 Popup 在我的应用程序中只存在一次,我将动态添加 Buttons 到 optionPopup.ids["contentButtons"]
。这意味着 contentButton
布局 minimum_height
已更改。如何正确调整父 Boxlayouts content
和 PopUp windows 的大小?
上面的 kv
选项似乎做了正确的事情,比如将 optionPopup.height
绑定到 content.height
,但它不起作用?
正确的 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()