【发布时间】:2021-09-13 17:42:29
【问题描述】:
我正在尝试使用DropDown 构建一个下拉列表,该小部件的行为应该类似于在 Windows 文件资源管理器中看到的。
我有一个TextInput 和一个Button,我将DropDown 与它们相关联。当我运行以下代码时,出现错误:
Exception has occurred: AttributeError
'NoneType' object has no attribute 'text'
File "C:\Users\hanne\Documents\_Educational\_Learning\Python\Kivy\DropDown\B.ii\main.py", line 34, in on_select
setattr(self.user_choice, 'text', data)
File "C:\Users\hanne\Documents\_Educational\_Learning\Python\Kivy\DropDown\B.ii\main.py", line 45, in on_touch_down
self.dropdown.select(self.text)
File "C:\Users\hanne\Documents\_Educational\_Learning\Python\Kivy\DropDown\B.ii\main.py", line 69, in <module>
MyApp().run()
main.py
import kivy
kivy.require('2.0.0')
from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
class TextEntryDropDown(BoxLayout):
user_choice = ObjectProperty(None)
drop_button = ObjectProperty(None)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.dropdown = DropDownList(self.user_choice)
for item in ['Option 1', 'Option 2', 'Option 3']:
label = DropDownItem(self.dropdown, text=item, size_hint_y=None, height=30)
self.dropdown.add_widget(label)
class DropDownList(DropDown):
def __init__(self, user_choice, **kwargs):
super().__init__(**kwargs)
self.user_choice = user_choice
def on_select(self, data):
setattr(self.user_choice, 'text', data)
class DropDownItem(Label):
def __init__(self, dropdown, **kwargs):
super().__init__(**kwargs)
self.dropdown = dropdown
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
self.dropdown.select(self.text)
return super().on_touch_down(touch)
class DropButton(Button):
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
dropdown = self.parent.dropdown
dropdown.open(self)
return super().on_touch_down(touch)
class MainApp(AnchorLayout):
pass
class MyApp(App):
def build(self):
return MainApp()
if __name__ == '__main__':
MyApp().run()
my.kv
<MainApp>:
anchor_x: 'left'
anchor_y: 'top'
TextEntryDropDown:
<TextEntryDropDown>
size_hint_y: None
height: 30
user_choice: user_choice
drop_button: drop_button
TextInput:
id: user_choice
text: 'Select an option..'
size_hint: (None, None)
height: 30
width: root.width - drop_button.width
DropButton:
id: drop_button
size_hint: (None, None)
height: user_choice.height
width: user_choice.width
我试图追查为什么user_choice 是None,我希望它是对my.kv 中定义的TextInput 的引用。知道为什么会这样吗?我不能指望它。
谢谢!
【问题讨论】:
标签: python kivy kivy-language