我正在尝试制作一个具有最大下拉高度的微调器菜单,我已经尝试实现this问题中的答案,但无法使其正常工作。我的代码结构是一个较大程序的一部分,因此,如果我可以避免重新格式化,则将是首选。
Python文件:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import Screen , ScreenManager
from kivy.uix.spinner import Spinner
class Screen1(Screen):
pass
class Screen2(Screen):
pass
class SimpleKivy(App):
def build(self):
return
if __name__ == "__main__":
SimpleKivy().run()
Kivy文件:
ScreenManager:
Screen1:
name: 'screen1'
FloatLayout:
Button:
text: "Change Screen"
pos_hint: {"x": 0.3, 'top': 0.7}
size_hint: 0.4, 0.1
on_release:
root.current = 'screen2'
Screen2:
name: 'screen2'
FloatLayout:
Spinner:
id: spinner
text: 'Select Number'
values: ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20')
pos_hint: {"x": 0.3, 'top': 0.7}
size_hint: 0.4, 0.1
答案 0 :(得分:1)
您可以使用on_enter
类的Screen
事件来完成此操作,如下所示:
from kivy.app import App
from kivy.lang import Builder
from kivy.metrics import dp
from kivy.uix.screenmanager import Screen
class Screen1(Screen):
pass
class Screen2(Screen):
def limit_spinner(self, *args):
max = 5 # max number of Buttons to display in the Spinner DropDown
self.spinner.dropdown_cls.max_height = max * dp(48) # dp(48) is the size of each Button in the DropDown (from style.kv)
theRoot = Builder.load_string('''
ScreenManager:
Screen1:
name: 'screen1'
FloatLayout:
Button:
text: "Change Screen"
pos_hint: {"x": 0.3, 'top': 0.7}
size_hint: 0.4, 0.1
on_release:
root.current = 'screen2'
Screen2:
spinner: spinner
name: 'screen2'
on_enter: self.limit_spinner()
FloatLayout:
Spinner:
id: spinner
text: 'Select Number'
values: ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20')
pos_hint: {"x": 0.3, 'top': 0.7}
size_hint: 0.4, 0.1
''')
class SimpleKivy(App):
def build(self):
return theRoot
if __name__ == "__main__":
SimpleKivy().run()
这将使用您引用的问题中的一些信息。还使用spinner
中的ObjectProperty
Screen2
。 on_enter
事件触发limit_spinner()
方法。