在boxlayou中动态添加和删除按钮小部件

2024-09-30 12:34:37 发布

您现在位置:Python中文网/ 问答频道 /正文

需要帮助找出代码,让我在textinput中搜索某些内容,并让所有与搜索匹配的项都出现在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玩的时候留下的。在

在尤吉奥千伏在

^{pr2}$

我的kivy代码到目前为止。在


Tags: 代码frompyimportapp内容pagetextinput
1条回答
网友
1楼 · 发布于 2024-09-30 12:34:37

您已经写了一条评论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属性获取,也可以自己存储,例如:

^{pr2}$

相关问题 更多 >

    热门问题