需要帮助找出代码,使我可以在textinput中搜索某些内容,并使与搜索匹配的所有项目都出现在boxlayout中。我才刚开始做奇异果(3天前)。
main.py
# import kivy & functions/widgets.
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
# import kivy layouts.
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
# Specify version or kivy needed.
kivy.require("1.10.1")
class Page(FloatLayout):
pass
class YuGiOhApp(App):
def build(self):
return Page()
YuGiOhApp().run()
我不知道我需要多少进口物品,因为我在玩奇异果时就把它们留在那里。
玉吉
#:kivy 1.10.1
<TestButton@Button>:
width: 177
height: 254
size_hint: None, None
background_normal: "pics/32864.jpg"
<FloatLayout>:
id: searchpage
display: entry
Button:
id: searchbutton
size_hint: 0.20, 0.10
pos_hint: {"x": 0.60, "top": 1}
text: "search"
Button:
id: collectionbutton
size_hint: 0.20, 0.10
pos_hint: {"x": 0.80, "top": 1}
text: "collection"
TextInput:
id: entry
multiline: False
font_size: 48
size_hint: 0.60, 0.10
pos_hint: {"x": 0, "top": 1}
ScrollView:
size_hint: 0.60, 0.90
StackLayout:
orientation: "lr-tb"
pos_hint: {"x": 0, "top": 0.88}
size_hint: 1, None
height: self.minimum_height
padding: 5
spacing: 5
TestButton:
TestButton:
TestButton:
TestButton:
TestButton:
TestButton:
TestButton:
TestButton:
TestButton:
TestButton:
TestButton:
TestButton:
到目前为止,我的猕猴桃代码。
最佳答案
您已经写了评论Even an example of how to add and remove the widgets would help greatly as i can fit it to my own program and needs!
,所以它是:
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
class MyWidget(BoxLayout):
def __init__(self, *args):
Clock.schedule_once(self.add_widgets, 1)
Clock.schedule_once(self.remove_widgets, 2)
return super().__init__(*args)
def add_widgets(self, *args):
# add two widgets
self.add_widget(Label(text="Hello"))
self.add_widget(Label(text="World"))
def remove_widgets(self, *args):
# remove a widget using children property
self.remove_widget(self.children[1])
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
这很简单。您可以使用
add_widget
方法添加任何小部件(按钮,标签,布局等)的新创建实例。然后,可以通过将其id传递给remove_widget
方法来删除它。您可以从children
属性获取它,也可以自己存储它,例如:my_button = Button(text="Blah blah")
# ...
my_layout.add_widget(my_button)
# ...
my_layout.remove_widget(my_button)
关于python - 在BoxLayout中动态添加和删除按钮小部件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54433268/