My goal is a drop down list similar to those seen in Windows File Explorer:
.open(self) My goal is a drop down list similar to those seen in Windows File Explorer:
首页 > 解决方案 > Kivy DropDown 实现有错误“无法添加 to window, it already has a parent " when calling .open(self) My goal is a drop down list similar to those seen in Windows File Explorer: python - Kivy DropDown 实现有错误“无法添加 to window, it already has a parent " when calling .open(self) My goal is a drop down list similar to those seen in Windows File Explorer: 问题描述 My goal is a drop down list similar to those seen in Windows File Explorer: I'm experimenting with DropDown with the following code: main.py import kivy kivy.require('2.0.0') from kivy.app import App from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.dropdown import DropDown from kivy.uix.textinput import TextInput from kivy.properties import ObjectProperty class CustomDropDown(TextInput): droplist = ObjectProperty(None) def on_touch_down(self, touch): if self.collide_point(*touch.pos): self.droplist.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>: CustomDropDown: <CustomDropDown> text: 'Select an option..' size_hint_y: None height: 30 droplist: options DropDown: id: options Label: text: 'Option 1' Label: text: 'Option 2' Label: text: 'Option 3' I get the following error on the line self.droplist.open(self): Cannot add <kivy.uix.dropdown.DropDown object at 0x00000223791F39E0> to window, it already has a parent <__main__.CustomDropDown object at 0x00000223791BA580> I think this should be working, and after some time troubleshooting I still can't figure out why it doesn't. Does anyone spot my error? --UPDATE 9/12/2021 3:30PM-- Thank you @inclement for the suggestion, thanks to your response I was able to move forward. However, I now have a follow-up question: To implement the suggestion, I sub-classed DropDown and Label and overrode the on_select and on_touch_down methods, respectively. I also had to override the __init__ method of DropDown in my subclass, so that I could get access to the TextInput box and update its content on with on_select. This seems awkward and adds a lot of code, am I missing something that would cut down on the amount and complexity of this sort of code? updated main.py import kivy kivy.require('2.0.0') from kivy.app import App from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.dropdown import DropDown from kivy.uix.label import Label from kivy.uix.textinput import TextInput class CustomDropDown(TextInput): def __init__(self, **kwargs): super().__init__(**kwargs) self.dropdown = DropDownList(self) for item in ['Option 1', 'Option 2', 'Option 3']: lbl = DropDownItem(text=item, size_hint_y=None, height=30) self.dropdown.add_widget(lbl) def on_touch_down(self, touch): if self.collide_point(*touch.pos): self.dropdown.open(self) return super().on_touch_down(touch) 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 on_touch_down(self, touch): if self.collide_point(*touch.pos): dropdown = self.parent.parent dropdown.select(self.text) return super().on_touch_down(touch) class MainApp(AnchorLayout): pass class MyApp(App): def build(self): return MainApp() if __name__ == '__main__': MyApp().run() Updated my.kv <MainApp>: DropDownTextInput: <DropDownTextInput> text: 'Select an option..' size_hint_y: None height: 30 Thanks again! I faced the same error. I am using mongoDB Atlas and not the local one. What worked for me was to remove the options in the .connect method (I am using mongoose for connecting to mongodb). Previous code (that caused the error) mongoose.connect( "" + process.env.DB_URL, { useUnifiedTopology: true, useNewUrlParser: true, useFindAndModify: false }, () => { console.log("Connected to DB"); } ) Just remove the code inside { } in this method. mongoose.connect( "" + process.env.DB_URL, { }, () => { console.log("Connected to DB"); } ) 标签: pythonkivykivy-language 解决方案 DropDown 被设计为通过调用它们的open方法来打开,该方法将它们添加到小部件树中,使它们出现在所有内容的顶部并具有正确的位置,但是您已经通过将其放入您自己的方式将其添加到小部件树中kv 规则。这就是为什么它已经有了父母。 您不应将其声明为 的子级CustomDropDown,而应创建一个新实例并调用该实例的open方法。 推荐阅读 javascript - 如何转换 ReadonlyArray去任何[]?firebase - 从过期的 Firebase 身份验证令牌中获取 UID?karate - 空手道 - 从响应中获取所有特定字段值powershell - 将排序数据的排序部分输出到变量?javascript - 如何将 Javascript 变量带入模态angular - npm 错误!错误:ENOENT "...\\package.json"ruby-on-rails - 水豚期待*没有*警报sql - 最大和最小日期和时间 T-SQLlinux - 来自外部镶木地板项目的静态库的“未定义引用”错误ios - 如何将 NSExtension 和 UNNotificationExtensionCategory 键添加到 Info.plist?
python - Kivy DropDown 实现有错误“无法添加 to window, it already has a parent " when calling .open(self) My goal is a drop down list similar to those seen in Windows File Explorer: 问题描述 My goal is a drop down list similar to those seen in Windows File Explorer: I'm experimenting with DropDown with the following code: main.py import kivy kivy.require('2.0.0') from kivy.app import App from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.dropdown import DropDown from kivy.uix.textinput import TextInput from kivy.properties import ObjectProperty class CustomDropDown(TextInput): droplist = ObjectProperty(None) def on_touch_down(self, touch): if self.collide_point(*touch.pos): self.droplist.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>: CustomDropDown: <CustomDropDown> text: 'Select an option..' size_hint_y: None height: 30 droplist: options DropDown: id: options Label: text: 'Option 1' Label: text: 'Option 2' Label: text: 'Option 3' I get the following error on the line self.droplist.open(self): Cannot add <kivy.uix.dropdown.DropDown object at 0x00000223791F39E0> to window, it already has a parent <__main__.CustomDropDown object at 0x00000223791BA580> I think this should be working, and after some time troubleshooting I still can't figure out why it doesn't. Does anyone spot my error? --UPDATE 9/12/2021 3:30PM-- Thank you @inclement for the suggestion, thanks to your response I was able to move forward. However, I now have a follow-up question: To implement the suggestion, I sub-classed DropDown and Label and overrode the on_select and on_touch_down methods, respectively. I also had to override the __init__ method of DropDown in my subclass, so that I could get access to the TextInput box and update its content on with on_select. This seems awkward and adds a lot of code, am I missing something that would cut down on the amount and complexity of this sort of code? updated main.py import kivy kivy.require('2.0.0') from kivy.app import App from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.dropdown import DropDown from kivy.uix.label import Label from kivy.uix.textinput import TextInput class CustomDropDown(TextInput): def __init__(self, **kwargs): super().__init__(**kwargs) self.dropdown = DropDownList(self) for item in ['Option 1', 'Option 2', 'Option 3']: lbl = DropDownItem(text=item, size_hint_y=None, height=30) self.dropdown.add_widget(lbl) def on_touch_down(self, touch): if self.collide_point(*touch.pos): self.dropdown.open(self) return super().on_touch_down(touch) 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 on_touch_down(self, touch): if self.collide_point(*touch.pos): dropdown = self.parent.parent dropdown.select(self.text) return super().on_touch_down(touch) class MainApp(AnchorLayout): pass class MyApp(App): def build(self): return MainApp() if __name__ == '__main__': MyApp().run() Updated my.kv <MainApp>: DropDownTextInput: <DropDownTextInput> text: 'Select an option..' size_hint_y: None height: 30 Thanks again! I faced the same error. I am using mongoDB Atlas and not the local one. What worked for me was to remove the options in the .connect method (I am using mongoose for connecting to mongodb). Previous code (that caused the error) mongoose.connect( "" + process.env.DB_URL, { useUnifiedTopology: true, useNewUrlParser: true, useFindAndModify: false }, () => { console.log("Connected to DB"); } ) Just remove the code inside { } in this method. mongoose.connect( "" + process.env.DB_URL, { }, () => { console.log("Connected to DB"); } ) 标签: pythonkivykivy-language 解决方案 DropDown 被设计为通过调用它们的open方法来打开,该方法将它们添加到小部件树中,使它们出现在所有内容的顶部并具有正确的位置,但是您已经通过将其放入您自己的方式将其添加到小部件树中kv 规则。这就是为什么它已经有了父母。 您不应将其声明为 的子级CustomDropDown,而应创建一个新实例并调用该实例的open方法。 推荐阅读 javascript - 如何转换 ReadonlyArray去任何[]?firebase - 从过期的 Firebase 身份验证令牌中获取 UID?karate - 空手道 - 从响应中获取所有特定字段值powershell - 将排序数据的排序部分输出到变量?javascript - 如何将 Javascript 变量带入模态angular - npm 错误!错误:ENOENT "...\\package.json"ruby-on-rails - 水豚期待*没有*警报sql - 最大和最小日期和时间 T-SQLlinux - 来自外部镶木地板项目的静态库的“未定义引用”错误ios - 如何将 NSExtension 和 UNNotificationExtensionCategory 键添加到 Info.plist?
问题描述 My goal is a drop down list similar to those seen in Windows File Explorer: I'm experimenting with DropDown with the following code: main.py import kivy kivy.require('2.0.0') from kivy.app import App from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.dropdown import DropDown from kivy.uix.textinput import TextInput from kivy.properties import ObjectProperty class CustomDropDown(TextInput): droplist = ObjectProperty(None) def on_touch_down(self, touch): if self.collide_point(*touch.pos): self.droplist.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>: CustomDropDown: <CustomDropDown> text: 'Select an option..' size_hint_y: None height: 30 droplist: options DropDown: id: options Label: text: 'Option 1' Label: text: 'Option 2' Label: text: 'Option 3' I get the following error on the line self.droplist.open(self): Cannot add <kivy.uix.dropdown.DropDown object at 0x00000223791F39E0> to window, it already has a parent <__main__.CustomDropDown object at 0x00000223791BA580> I think this should be working, and after some time troubleshooting I still can't figure out why it doesn't. Does anyone spot my error? --UPDATE 9/12/2021 3:30PM-- Thank you @inclement for the suggestion, thanks to your response I was able to move forward. However, I now have a follow-up question: To implement the suggestion, I sub-classed DropDown and Label and overrode the on_select and on_touch_down methods, respectively. I also had to override the __init__ method of DropDown in my subclass, so that I could get access to the TextInput box and update its content on with on_select. This seems awkward and adds a lot of code, am I missing something that would cut down on the amount and complexity of this sort of code? updated main.py import kivy kivy.require('2.0.0') from kivy.app import App from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.dropdown import DropDown from kivy.uix.label import Label from kivy.uix.textinput import TextInput class CustomDropDown(TextInput): def __init__(self, **kwargs): super().__init__(**kwargs) self.dropdown = DropDownList(self) for item in ['Option 1', 'Option 2', 'Option 3']: lbl = DropDownItem(text=item, size_hint_y=None, height=30) self.dropdown.add_widget(lbl) def on_touch_down(self, touch): if self.collide_point(*touch.pos): self.dropdown.open(self) return super().on_touch_down(touch) 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 on_touch_down(self, touch): if self.collide_point(*touch.pos): dropdown = self.parent.parent dropdown.select(self.text) return super().on_touch_down(touch) class MainApp(AnchorLayout): pass class MyApp(App): def build(self): return MainApp() if __name__ == '__main__': MyApp().run() Updated my.kv <MainApp>: DropDownTextInput: <DropDownTextInput> text: 'Select an option..' size_hint_y: None height: 30 Thanks again!
I'm experimenting with DropDown with the following code:
DropDown
main.py
import kivy kivy.require('2.0.0') from kivy.app import App from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.dropdown import DropDown from kivy.uix.textinput import TextInput from kivy.properties import ObjectProperty class CustomDropDown(TextInput): droplist = ObjectProperty(None) def on_touch_down(self, touch): if self.collide_point(*touch.pos): self.droplist.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>: CustomDropDown: <CustomDropDown> text: 'Select an option..' size_hint_y: None height: 30 droplist: options DropDown: id: options Label: text: 'Option 1' Label: text: 'Option 2' Label: text: 'Option 3'
I get the following error on the line self.droplist.open(self):
self.droplist.open(self)
Cannot add <kivy.uix.dropdown.DropDown object at 0x00000223791F39E0> to window, it already has a parent <__main__.CustomDropDown object at 0x00000223791BA580>
I think this should be working, and after some time troubleshooting I still can't figure out why it doesn't. Does anyone spot my error?
--UPDATE 9/12/2021 3:30PM--
Thank you @inclement for the suggestion, thanks to your response I was able to move forward. However, I now have a follow-up question:
To implement the suggestion, I sub-classed DropDown and Label and overrode the on_select and on_touch_down methods, respectively. I also had to override the __init__ method of DropDown in my subclass, so that I could get access to the TextInput box and update its content on with on_select. This seems awkward and adds a lot of code, am I missing something that would cut down on the amount and complexity of this sort of code?
Label
on_select
on_touch_down
__init__
TextInput
updated main.py
import kivy kivy.require('2.0.0') from kivy.app import App from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.dropdown import DropDown from kivy.uix.label import Label from kivy.uix.textinput import TextInput class CustomDropDown(TextInput): def __init__(self, **kwargs): super().__init__(**kwargs) self.dropdown = DropDownList(self) for item in ['Option 1', 'Option 2', 'Option 3']: lbl = DropDownItem(text=item, size_hint_y=None, height=30) self.dropdown.add_widget(lbl) def on_touch_down(self, touch): if self.collide_point(*touch.pos): self.dropdown.open(self) return super().on_touch_down(touch) 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 on_touch_down(self, touch): if self.collide_point(*touch.pos): dropdown = self.parent.parent dropdown.select(self.text) return super().on_touch_down(touch) class MainApp(AnchorLayout): pass class MyApp(App): def build(self): return MainApp() if __name__ == '__main__': MyApp().run()
Updated my.kv
<MainApp>: DropDownTextInput: <DropDownTextInput> text: 'Select an option..' size_hint_y: None height: 30
Thanks again!
I faced the same error. I am using mongoDB Atlas and not the local one. What worked for me was to remove the options in the .connect method (I am using mongoose for connecting to mongodb).
.connect
Previous code (that caused the error)
mongoose.connect( "" + process.env.DB_URL, { useUnifiedTopology: true, useNewUrlParser: true, useFindAndModify: false }, () => { console.log("Connected to DB"); } )
Just remove the code inside { } in this method.
mongoose.connect( "" + process.env.DB_URL, { }, () => { console.log("Connected to DB"); } )
标签: pythonkivykivy-language
DropDown 被设计为通过调用它们的open方法来打开,该方法将它们添加到小部件树中,使它们出现在所有内容的顶部并具有正确的位置,但是您已经通过将其放入您自己的方式将其添加到小部件树中kv 规则。这就是为什么它已经有了父母。
open
您不应将其声明为 的子级CustomDropDown,而应创建一个新实例并调用该实例的open方法。
CustomDropDown