3

Need help in figuring out code that will allow me to search something in the textinput and have all items that match the search appear in the boxlayout. I have only just started kivy (3 days ago).

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()

i dont know how many of the imports i need just kinda left them there from when i was playing around with kivy.

YuGiOh.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:

My kivy code so far.

Jacob Cook
  • 61
  • 1
  • 7
  • 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! – Jacob Cook Jan 30 '19 at 11:07
  • is this your complete code? right now you will run into an error because you have no code in your buttontest function in the Page class. Also, it looks like you want to have the float layout (id is searchpage) be displayed. To do that, get rid of the <> around the : and in your py file remove the build function. kivy should know to automatically load your kv file and will display FloatLayout as the root widget since it doesn't have <> – Erik Jan 30 '19 at 16:12
  • Oops sorry, started trying to work this out and realised i would need a fair bit of help, this must be code I missed when cleaning it up. – Jacob Cook Jan 31 '19 at 04:32

1 Answers1

1

You've written in a comment 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!, so here it is:

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()

It's fairy simple. You use add_widget method to add a newly created instance of any widget - button, label, layout and so on. Then you can remove it, by passing its id to remove_widget method. You can obtain it from children property, or store it yourself, e.g:

my_button = Button(text="Blah blah")
# ...
my_layout.add_widget(my_button)
# ...
my_layout.remove_widget(my_button)
Nykakin
  • 8,657
  • 2
  • 29
  • 42
  • Awesome, I'll have to properly look into stuff like super() and *args etc... but this points me in the right direction, thanks heaps! – Jacob Cook Jan 31 '19 at 04:31