运行此DropDown示例有效。但是,在一些使用/时间之后我得到错误ReferenceError:弱引用的对象不再存在
这可能是on_release中的一个问题:dropdown.open(self)
奖励点为什么on_parent:self.dismiss()也不能用我设置这些小部件的方式工作。如果没有这个,我会在应用程序首次运行时出现子菜单项,启用此选项后,子菜单项会闪烁(显示并快速消失)。
#!/usr/bin/kivy
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.properties import ObjectProperty
from kivy.uix.dropdown import DropDown
from kivy.core.window import Window
Window.size = (400, 240)
sm = """
ScreenManager:
id:manager
canvas.before:
Color:
rgba: 0.5, 0.5, 0.5, 0.5
Rectangle:
pos: 0,0
size: 800, 480
Notes:
id:Notes
name: 'Notes'
manager: manager
<Notes>:
name: "Notes"
orientation: "vertical"
FloatLayout:
size_hint: None, None
canvas.before:
Color:
rgba: 1, 1, 0, 1
Button:
id: mainbutton
text: "Menu name"
font_size: 20
size_hint: None, None
size: 150, 50
pos: 20,400
on_release:dropdown.open(self)
CustomDropDown:
id: dropdown
#on_parent: self.dismiss()
on_select: mainbutton.text = '{}'.format(args[1])
Button:
id: button1
text: 'First Item'
size_hint_y: None
height: 40
font_size: 18
on_release: dropdown.select('First Item')
Button:
id: button2
text: 'Second Item'
size_hint_y: None
height: 40
font_size: 18
on_release: dropdown.select('Second Item')
Button:
id: button3
text: 'Third Item'
size_hint_y: None
height: 40
font_size: 18
on_release: dropdown.select('Third Item')
"""
class Notes(Screen):
pass
class CustomDropDown(DropDown):
pass
#dropdown = CustomDropDown()
class TestApp(App):
def build(self):
return Builder.load_string(sm)
if __name__ == '__main__':
TestApp().run()
答案 0 :(得分:0)
当您点击主按钮时,菜单名称有时会出错, ReferenceError:弱引用的对象不再存在 。如果没有 ReferenceError ,则下拉列表会闪烁(显示并快速消失)。原因是DropDown被解雇了。
它会在app启动时显示CustomDropDown列表。单击主按钮菜单名称时,启动时出现的下拉列表会消失,但会显示两次下拉列表,即子菜单项重复两次。
下拉列表类似于Popup。它们是特殊的小部件。不要尝试将其作为孩子添加到任何其他小部件。如果这样做,它们将像普通的小部件一样处理,并且不会在后台隐藏创建。
请参阅下面的示例,说明如何创建下拉列表。
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.dropdown import DropDown
from kivy.core.window import Window
Window.size = (800, 480)
class CustomDropDown(DropDown):
pass
class Notes(Screen):
pass
class MyScreenManager(ScreenManager):
pass
class TestApp(App):
title = "Kivy Drop-Down List Demo"
def build(self):
return MyScreenManager()
if __name__ == '__main__':
TestApp().run()
#:kivy 1.10.0
#:import Factory kivy.factory.Factory
<CustomDropDown>:
on_select: app.root.ids.Notes.ids.mainbutton.text = '{}'.format(args[1])
Button:
id: button1
text: 'First Item'
size_hint_y: None
height: 40
font_size: 18
on_release: root.select(self.text)
Button:
id: button2
text: 'Second Item'
size_hint_y: None
height: 40
font_size: 18
on_release: root.select(self.text)
Button:
id: button3
text: 'Third Item'
size_hint_y: None
height: 40
font_size: 18
on_release: root.select(self.text)
<MyScreenManager>:
canvas.before:
Color:
rgba: 0.5, 0.5, 0.5, 0.5
Rectangle:
pos: 0,0
size: 800, 480
Notes:
id:Notes
name: 'Notes'
<Notes>:
orientation: "vertical"
FloatLayout:
size_hint: None, None
canvas.before:
Color:
rgba: 1, 1, 0, 1
Button:
id: mainbutton
text: "Menu name"
font_size: 20
size_hint: None, None
size: 150, 50
pos: 20,400
on_release: Factory.CustomDropDown().open(self)
答案 1 :(得分:0)
来自:$ Yourkivydir / kivy-examples / demo / showcase / data / screens
ShowcaseScreen:
fullscreen: True
name: 'DropDown'
# trick to not lost the Dropdown instance
# Dropdown itself is not really made to be used in kv.
__safe_id: [dropdown.__self__]
Button:
id: btn
text: '-'
on_release: dropdown.open(self)
size_hint_y: None
height: '48dp'
Widget:
on_parent: dropdown.dismiss()
DropDown:
id: dropdown
on_select: btn.text = 'Selected value: {}'.format(args[1])
Button:
text: 'Value A'
size_hint_y: None
height: '48dp'
on_release: dropdown.select('A')
Button:
text: 'Value B'
size_hint_y: None
height: '48dp'
on_release: dropdown.select('B')
Button:
text: 'Value C'
size_hint_y: None
height: '48dp'
on_release: dropdown.select('C')