我做了一些研究,但我无法创建一个递归下拉按钮。我想有1个DropDown包含2个包含2个按钮的child-DropDowns,如下所示: DropDown(DropDown(Button,Button),DropDown(Button,Button)) 。这是我的代码,它不起作用( TypeError:__ init __()缺少1个必需的位置参数:'screen_manager' )
的.py:
from kivy.app import App
from kivy.uix.dropdown import DropDown
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.properties import StringProperty
class MyScreenManager(ScreenManager):
button_text = StringProperty('Show possibilities')
def __init__(self, **kwargs):
super(MyScreenManager, self).__init__(**kwargs)
self.dropdown1 = CustomDropDown1(self)
self.dropdown2 = CustomDropDown2(self)
def open_drop_down(self, widget):
self.dropdown1.open(widget)
class MyScreen(Screen):
def __init__(self, **kwargs):
super(MyScreen, self).__init__(**kwargs)
class CustomDropDown1(DropDown):
def __init__(self, screen_manager, **kwargs):
super(CustomDropDown1, self).__init__(**kwargs)
self.sm = screen_manager
def on_select(self, data):
self.sm.button_text = data
class CustomDropDown2(DropDown):
def __init__(self, screen_manager, **kwargs):
super(CustomDropDown2, self).__init__(**kwargs)
self.sm = screen_manager
def on_select(self, data):
self.sm.button_text = data
Builder.load_file("debug.kv")
class MyAppli(App):
def build(self):
return MyScreenManager()
if __name__ == '__main__':
MyAppli().run()
.kv:
<MyScreenManager>:
MyScreen:
AnchorLayout:
anchor_y: "top"
anchor_x: "center"
Button:
text: root.button_text
size:(200,50)
size_hint:(None,None)
on_release: root.open_drop_down(self)
<CustomDropDown2>:
Button:
text:"Item 1"
size:(200,50)
size_hint:(None,None)
on_release: root.select("ONE")
Button:
text:"Item 2"
size:(200,50)
size_hint:(None,None)
on_release: root.select("TWO")
Button:
text:"Item 3"
size:(200,50)
size_hint:(None,None)
on_release: root.select("THREE")
<CustomDropDown1>:
CustomDropDown2:
CustomDropDown2:
答案 0 :(得分:0)
在你的“debug.py”中文件,添加一个CustomDropDown类和一个&#39; open_drop_down()&#39;方法:
from kivy.app import App
from kivy.uix.dropdown import DropDown
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.properties import StringProperty
class MyScreenManager(ScreenManager):
button_text = StringProperty('Show possibilities')
def __init__(self, **kwargs):
super(MyScreenManager, self).__init__(**kwargs)
self.dropdown = CustomDropDown(self)
def open_drop_down(self, widget):
self.dropdown.open(widget)
class MyScreen(Screen):
def __init__(self, **kwargs):
super(MyScreen, self).__init__(**kwargs)
class CustomDropDown(DropDown):
def __init__(self, screen_manager, **kwargs):
super(CustomDropDown, self).__init__(**kwargs)
self.sm = screen_manager
def on_select(self, data):
self.sm.button_text = data
Builder.load_file("debug.kv")
class MyAppli(App):
def build(self):
return MyScreenManager()
if __name__ == '__main__':
MyAppli().run()
然后修改您的&#39; debug.kv&#39;文件使用CustomDropDown:
<MyScreenManager>:
MyScreen:
BoxLayout:
Button:
text: root.button_text
size:(200,50)
size_hint:(None,None)
on_release: root.open_drop_down(self)
<CustomDropDown>:
Button:
text:"Item 1"
size:(200,50)
size_hint:(None,None)
on_release: root.select("ONE")
Button:
text:"Item 2"
size:(200,50)
size_hint:(None,None)
on_release: root.select(self.text)
Button:
text:"Item 3"
size:(200,50)
size_hint:(None,None)
on_release: root.select("THREE")
DropDown菜单将附加到传递给open()
方法的小部件,因此在这种情况下,它附加到&#34;显示可能性&#34;按钮&#34;