0

In my program I have a button that adds a new box. When you press a button in this new box it adds a new box, and so on. In each box I have a label, and I want to be able to change this text from the same textinput field. But I dont want the same text in each box, so I want to select the box, write the text, and then press a button so that the text is passed from the input field to that specific box/label. I have removed everything else from my app, so I will show you a complete code so you can try the program to understand what I mean.

I have tried to use a button on the main widget, but then I dont know how to choose which box that should be updated. I have also tried to use a button in the box (called "R" in the code), but then I only gets an error.

If i use the code that is commented out i get this error:

"AttributeError: 'super' object has no attribute '__getattr__'"

I would really appreciate some help! Thanks a lot.

This is the python file:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout


class Home(FloatLayout):

    def first(self, *args):
        a = box()
        self.ids.frame.add_widget(a)
        b = a.ids.btn3
        b.bind(on_press=self.second)
        c = a.ids.btn1
        c.bind(on_press=self.textedit)

    def second(self, *args):
        d = box()
        e = d.ids.mains
        e.pos_hint={"right": .5, "top": .7}
        self.ids.frame.add_widget(d)
        f = d.ids.btn3
        f.bind(on_press=self.third)
        g = d.ids.btn1
        g.bind(on_press=self.textedit)

    def third(self, *args):
        h = box()
        i = h.ids.mains
        i.pos_hint = {"right": .3, "top": .9}
        self.ids.frame.add_widget(h)
        j = h.ids.btn1
        j.bind(on_press=self.textedit)

    def textedit(self, *args):
        print("Hello")
       #k = self.ids.tinput.text
       #l = self.ids.lab
       #l.text = k

    def textedit2(self, *args):
        print("hei")
       #This is the submitbutton on the main widget

class HiApp(App):
    def build(self):
        return Home()

class box(FloatLayout):
    pass

if __name__ == "__main__":
    HiApp().run()

This is the .kv file

<Home>:
    FloatLayout:
        id: frame
        size_hint_y: 1
        pos_hint:{"right":1,"top":1}
        canvas.before:
            Color:
                rgba: (1, 1, 1, 1)
            Rectangle:
                size: self.size
                pos: self.pos

    BoxLayout:
        id: boks
        orientation: "vertical"
        size_hint_x: .20
        size_hint_y: .15
        pos_hint:{"right":1,"top":1}
        canvas.before:
            Color:
                rgba: (0, 1, 0, 1)
            Rectangle:
                size: self.size
                pos: self.pos
        TextInput:
            id: tinput
            hint_text: "Frome here"

        Button:
            id: t_sub
            text: "Submit"
            on_press: root.textedit2()

    Button:
        id: start
        text: "Start"
        font_size: 20
        color: 0, 0, 0, 1
        background_normal: ''
        background_color: .88, .88, .88, 1
        size_hint: .1, .1
        pos_hint:{"right":.5,"top":.35}
        on_press: root.first()

<box>:

    BoxLayout:
        id: mains
        orientation: "vertical"
        size_hint_x: .18
        size_hint_y: .13
        pos_hint:{"right":.3,"top":.5}
        canvas.before:
            Color:
                rgba: (.20, .05, .0, 1)
            Rectangle:
                size: self.size
                pos: self.pos

        BoxLayout:
            id: c1
            size_hint_y: .25
            pos_hint:{"left":.1,"top":.5}

            GridLayout:
                rows: 1
                cols: 3
                padding: 4
                spacing: 4

                Button:
                    id: btn1
                    text: "R"
                    font_size: 30
                    color: 0, 0, 0, 1
                    background_normal: ''
                    background_color: .88, .88, .88, 1
                    size_hint: .3, .3
                    pos_hint:{"left":.5,"top":.5}
                    on_press:

                Button:
                    id: btn2
                    text: "-"
                    font_size: 30
                    color: 1, 0, 0, 1
                    background_normal: ''
                    background_color: .88, .88, .88, 1
                    size_hint: .3, .3
                    pos_hint:{"left":.5,"top":.5}
                    on_press:

                Button:
                    id: btn3
                    text: "+"
                    font_size: 30
                    color: 0, 1, 0, 1
                    background_normal: ''
                    background_color: .88, .88, .88, 1
                    size_hint: .3, .3
                    pos_hint:{"left":.5,"top":.5}
                    on_press:

        GridLayout:
            rows: 1
            cols: 2
            padding: 4
            spacing: 4

            Label:
                id: lab
                text: "Text here"

            TextInput:
                hint_text: "0"
ikolim
  • 15,721
  • 2
  • 19
  • 29
Nick
  • 57
  • 1
  • 7
  • are you meaning to call `super(FloatLayout, self).__init__`? This calls `FloatLayout`s base class' `__init__` rather than `FloatLayout`s `__init__`.. – nnolte Jul 06 '19 at 14:23
  • I dont know. I am struggling to understand "__init__" and "super" – Nick Jul 06 '19 at 14:28
  • https://stackoverflow.com/questions/222877/what-does-super-do-in-python this should help. If you are in python3, just call super() without arguments and that just refers you to the base class – nnolte Jul 06 '19 at 14:29
  • @Nick: Do you want the Label's text to change after finished typing in TextInput of box2 and pressing Enter key? – ikolim Jul 06 '19 at 15:03
  • i want to write the text in the inputfield whit the id: tinput (upper left corner) and then pres the button (R) so that the text goes from the inputfield to the label – Nick Jul 06 '19 at 15:13

1 Answers1

0

Problem

I want to write the text in the inputfield with the id: tinput (upper left corner) and then press the button (R) so that the text goes from the inputfield to the label.

Solution

The solution to is use the following keywords in kv file:

  1. app - always refers to the instance of your application
  2. root - refers to the base widget/template in the current rule

Snippets - kv file

            Button:
                id: btn1
                text: "R"
                font_size: 30
                color: 0, 0, 0, 1
                background_normal: ''
                background_color: .88, .88, .88, 1
                size_hint: .3, .3
                pos_hint:{"left":.5,"top":.5}
                on_press:
                    lab.text = app.root.ids.tinput.text

Output

Entered text Box's Label's text changed

ikolim
  • 15,721
  • 2
  • 19
  • 29
  • Ah! I did not know about this. Thanks! But i want to write the text in the other TexInput, ID: tinput – Nick Jul 06 '19 at 15:16