无法删除在 Kivy 中动态添加到屏幕的小部件
Not able to remove Widgets which were dynamically added to screen in Kivy
我正在尝试在文本输入中键入某些文本时动态添加按钮。为了在KIvy中实现搜索select组合框。
尽管我可以在输入一些文本时添加小部件,但在删除输入的文本时无法删除那些添加的小部件。
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix import dropdown
from kivy.uix.textinput import TextInput
# from kivymd.app import MDApp
kivy.require('1.9.0')
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.base import runTouchApp
from kivy.uix.screenmanager import ScreenManager,Screen
class ScreenOne(Screen):
def change_text(self,root,val):
pass
def create_dropdown(self,root,value):
print("value is :" + value)
if value == "":
result = ""
print(root.ids)
else:
lang = ["aa","aaa", "bsa","cds","dds", "ddyus"]
result = list(filter(lambda x: x.startswith(value), lang))
for i in range (0,len(result)):
self.textinput = Button()
self.textinput.id = 'textinput'+ str(i)
self.textinput.text = result[i]
self.textinput.size_hint= (.3, .06)
self.textinput.font_size= '14sp'
self.textinput.pos_hint= {'center_x': 0.5, 'center_y': 0.8-(i+1)/10, }
root.ids.screen1.add_widget(self.textinput)
sm = Builder.load_string("""
ScreenManager:
ScreenOne:
id: screen1
name: "screen_one"
TextInput:
id: input1
hint_text:'Select input Language'
size_hint: (.3,.06)
font_size: '14sp'
pos_hint:{'center_x':0.5,'center_y':0.8,}
on_text: root.ids.screen1.create_dropdown(root,input1.text)
""")
class demo(App):
def build(self):
return sm
app=demo()
# app.run()
runTouchApp(sm)
in UI 当输入 a 时,请参见下图
但是当我清除输入文本时,我仍然能够看到之前添加的小部件
当输入框中输入的文本被清除时,我需要一些逻辑来删除添加的小部件。
尝试添加这个:
result = list(filter(lambda x: x.startswith(value), #from your cod
lang)) #from your code
if result == [] :
for widget in root.ids.screen1.children:
root.ids.screen1.remove_widget(widget)
我正在尝试在文本输入中键入某些文本时动态添加按钮。为了在KIvy中实现搜索select组合框。 尽管我可以在输入一些文本时添加小部件,但在删除输入的文本时无法删除那些添加的小部件。
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix import dropdown
from kivy.uix.textinput import TextInput
# from kivymd.app import MDApp
kivy.require('1.9.0')
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.base import runTouchApp
from kivy.uix.screenmanager import ScreenManager,Screen
class ScreenOne(Screen):
def change_text(self,root,val):
pass
def create_dropdown(self,root,value):
print("value is :" + value)
if value == "":
result = ""
print(root.ids)
else:
lang = ["aa","aaa", "bsa","cds","dds", "ddyus"]
result = list(filter(lambda x: x.startswith(value), lang))
for i in range (0,len(result)):
self.textinput = Button()
self.textinput.id = 'textinput'+ str(i)
self.textinput.text = result[i]
self.textinput.size_hint= (.3, .06)
self.textinput.font_size= '14sp'
self.textinput.pos_hint= {'center_x': 0.5, 'center_y': 0.8-(i+1)/10, }
root.ids.screen1.add_widget(self.textinput)
sm = Builder.load_string("""
ScreenManager:
ScreenOne:
id: screen1
name: "screen_one"
TextInput:
id: input1
hint_text:'Select input Language'
size_hint: (.3,.06)
font_size: '14sp'
pos_hint:{'center_x':0.5,'center_y':0.8,}
on_text: root.ids.screen1.create_dropdown(root,input1.text)
""")
class demo(App):
def build(self):
return sm
app=demo()
# app.run()
runTouchApp(sm)
in UI 当输入 a 时,请参见下图
但是当我清除输入文本时,我仍然能够看到之前添加的小部件
当输入框中输入的文本被清除时,我需要一些逻辑来删除添加的小部件。
尝试添加这个:
result = list(filter(lambda x: x.startswith(value), #from your cod
lang)) #from your code
if result == [] :
for widget in root.ids.screen1.children:
root.ids.screen1.remove_widget(widget)