Skip to main content
 首页 » 编程设计

python之在框布局中动态添加和删除按钮小部件

2024年12月31日7bluestorm

需要帮助来找出允许我在文本输入中搜索某些内容并使所有与搜索匹配的项目出现在 boxlayout 中的代码。我才刚刚开始 kivy(3 天前)。

主要.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 时把它们留在那里了。

游戏王.kv

#: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: 

到目前为止我的 kivy 代码。

请您参考如下方法:

您在评论中写道即使是如何添加和删除小部件的示例也会有很大帮助,因为我可以将其适合我自己的程序和需要!,所以这里是:

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)