Search code examples
pythonpython-3.xpython-2.7kivykivy-language

python/Kivy : How to pass value of textbox in new window


I have two file demo.py and demo.kv.
When i run demo.py and click on +Add then screen shows looks like.I want value of name textbox in def add_row(self): function when click on button.When i click on button of first row then i want to get test1 in def add_row(self): which are in class RowsExtra(BoxLayout): and click on button of second row then get test2.I want to get test1 value in textName variable.After that i am fetching data from database according name textbox value.

def add_row(self):
    print("here,i want value of name(`test1`) when click on button")
    textName = 'test1'
    #cur.execute("SELECT `column_name` FROM `table_name` WHERE `name`=?", (textName,))
    #rows = cur.fetchone()
    rows = [('A1'), ('B1'), ('C1'), ('D1')]
    for row in rows:
        self.row_count += 1
        r = RowExtra(button_text=str(self.row_count))
        r.col_data[1] = row

        self.add_widget(r)

demo.py

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty, NumericProperty
from kivy.uix.popup import Popup

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (500, 400)

class User(Popup):
    total_value = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)

    def add_more(self):
        self.ids.rows.add_more()

class ExtraPopup(Popup):
    mode = StringProperty("")

    def __init__(self, obj, **kwargs):
        super(ExtraPopup, self).__init__(**kwargs)

    def add_extra(self):
        self.ids.rowsExtra.add_row()


class RowExtra(BoxLayout):
    col_data = ListProperty(["?", "?", "?", "?", "?", "?", "?", "?"])
    button_text = StringProperty("")
    mode = StringProperty("")

    def __init__(self, **kwargs):
        super(RowExtra, self).__init__(**kwargs)
        self.col_data[0] = ''
        self.col_data[1] = ''
        self.col_data[2] = ''


class RowsExtra(BoxLayout):
    #orientation = "vertical"
    row_count = 0
    button_text = StringProperty("")


    def __init__(self, **kwargs):
        super(RowsExtra, self).__init__(**kwargs)
        self.add_row()

    def add_row(self):
        print("here,i want value of name(`test1`) when click on button")
        textName = 'test1'
        #cur.execute("SELECT `column_name` FROM `table_name` WHERE `name`=?", (textName,))
        #rows = cur.fetchone()
        rows = [('A1'), ('B1'), ('C1'), ('D1')]
        for row in rows:
            self.row_count += 1
            r = RowExtra(button_text=str(self.row_count))
            r.col_data[1] = row

            self.add_widget(r)



class Row(BoxLayout):
    col_data = ListProperty(["?", "?", "?", "?", "?"])
    name = ObjectProperty(None)
    button_text = StringProperty("")
    col_data3 = StringProperty("")
    col_data4 = StringProperty("")

    def __init__(self, **kwargs):
        super(Row, self).__init__(**kwargs)

    def add_seller_expenses(self):
        self.mode = "Add"
        popup = ExtraPopup(self)
        popup.open()


class Rows(BoxLayout):
    row_count = 0

    def __init__(self, **kwargs):
        super(Rows, self).__init__(**kwargs)
        self.add_more()

    def add_more(self):
        self.row_count += 1
        self.add_widget(Row(button_text=str(self.row_count)))


class rv(BoxLayout):
    data_items = ListProperty([])
    mode = StringProperty("")

    def __init__(self, **kwargs):
        super(rv, self).__init__(**kwargs)


    def add(self):
        self.mode = "Add"
        popup = User()
        popup.open()


class MainMenu(BoxLayout):
    content_area = ObjectProperty()

    def display(self):
        self.rv = rv()
        self.content_area.add_widget(self.rv)

class demo(App):

    def build(self):
        return MainMenu()


if __name__ == '__main__':
    demo().run()

demo.kv

<Row>:
    size_hint_y: None
    height: self.minimum_height
    height: 40

    Button:
        text: root.button_text
        size_hint_x: None
        top: 200

    TextInput:
        id : name
        text: root.col_data3
        width: 300
    TextInput:
        id: number_input
        text: root.col_data4
        width: 300
        input_filter: 'int'

    Button:
        text: "Button"
        size_hint_x: None
        top: 200
        on_press: root.add_seller_expenses()


<Rows>:
    size_hint_y: None
    height: self.minimum_height
    orientation: "vertical"

<User>:
    id: user
    BoxLayout:
        orientation: "vertical"
        padding : 20, 5


        BoxLayout:
            orientation: "horizontal"
            #padding : 10, 10
            spacing: 10, 10
            size: 450, 40
            size_hint: None, None

            Label:
                size_hint_x: .2
                text: "Number"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

            Label:
                size_hint_x: .4
                text: "name"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

            Label:
                size_hint_x: .4
                text: "Value"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'




        ScrollView:
            Rows:
                id: rows


        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .2
            size_hint_y: .2

            Button:
                text: "+Add More"
                on_press: root.add_more()

<rv>:
    BoxLayout:
        orientation: "vertical"

        Button:
            size_hint: .25, .03
            text: "+Add"
            on_press: root.add()

        GridLayout:
            size_hint: 1, None
            size_hint_y: None
            height: 25
            cols: 3

        BoxLayout:
            orientation: "vertical"

<ExtraPopup>:
    title: " Extra"
    title_size: 20
    title_font: "Verdana"
    size_hint: None, None
    size: 400, 400
    auto_dismiss: False


    BoxLayout:
        orientation: "vertical"
        padding : 10, 5
        spacing: 10, 10

        BoxLayout:
            orientation: "horizontal"
            spacing: 10, 10
            size: 550, 30
            size_hint: 1, None

            Label:
                size_hint_x: .3
                text: "SN"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

            Label:
                size_hint_x: .7
                text: "Name"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

        ScrollView:
            RowsExtra:
                id: rowsExtra

        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .3
            size_hint: .15, .1

            Button:
                #size_hint_x: .2
                text: "+Add More"
                valign: 'bottom'
                on_press: root.add_extra()



        BoxLayout:
            orientation: "horizontal"
            padding : 10, 5
            spacing: 10, 10
            size_hint: .5, .2
            pos_hint: {'x': .25, 'y':.25}
            Button:
                text: 'Ok'
                id: ok_text
                on_release:
                    root.dismiss()

            Button:
                text: 'Cancel'
                #size_hint_x: .5
                on_release: root.dismiss()

<RowExtra>:
    size_hint_y: None
    height: self.minimum_height
    height: 30

    Button:
        text: root.button_text
        size_hint_x: .3
        #top: 200

    TextInput:
        text: root.col_data[1]
        size_hint_x: .7



<RowsExtra>:
    size_hint_y: None
    height: self.minimum_height
    orientation: "vertical"

<MenuButton@Button>:
    text_size: self.size
    valign: "middle"
    padding_x: 5
    size : (100, 40)
    size_hint : (None, None)
    background_color: 90 , 90, 90, 90
    background_normal: ''
    color: 0, 0.517, 0.705, 1
    border: (0, 10, 0, 0)

<MainMenu>:
    content_area: content_area

    BoxLayout:
        orientation: 'vertical'
        spacing : 10

        BoxLayout:
            canvas.before:
                Rectangle:
                    pos: self.pos
                    size: self.size

            size_hint_y: 2

            MenuButton:
                text: 'Menu'
                size : (50, 12)
                on_release: root.display()

        BoxLayout:
            id: content_area
            size_hint_y: 30

Solution

  • A possible solution is to access rowsExtra through the popup since it is your child, but one problem is that you call add_row() in the constructor before it is assigned a parent (the assignment of a parent is after the creation of the object), we must eliminate that instruction and call it again before opening the popup. To store the text we can create a property of type StringProperty.

    [...]
    class RowsExtra(BoxLayout):
        #orientation = "vertical"
        row_count = 0
        button_text = StringProperty("")
        textName = StringProperty("")
    
        def __init__(self, **kwargs):
            super(RowsExtra, self).__init__(**kwargs)
    
        def add_row(self):
            print("here,i want value of name(`test1`) when click on button")
            print(self.textName)
            #cur.execute("SELECT `column_name` FROM `table_name` WHERE `name`=?", (textName,))
            #rows = cur.fetchone()
            rows = [('A1'), ('B1'), ('C1'), ('D1')]
            for row in rows:
                self.row_count += 1
                r = RowExtra(button_text=str(self.row_count))
                r.col_data[1] = row
                self.add_widget(r)
    
    class Row(BoxLayout):
        col_data = ListProperty(["?", "?", "?", "?", "?"])
        name = ObjectProperty(None)
        button_text = StringProperty("")
        col_data3 = StringProperty("")
        col_data4 = StringProperty("")
    
        def __init__(self, **kwargs):
            super(Row, self).__init__(**kwargs)
    
        def add_seller_expenses(self):
            self.mode = "Add"
            popup = ExtraPopup(self)
            popup.ids.rowsExtra.textName = self.ids.name.text
            popup.ids.rowsExtra.add_row()
            popup.open()
    [...]