python / Kivy:如何在新窗口中传递文本框的值

时间:2018-01-22 13:03:57

标签: python python-3.x python-2.7 kivy kivy-language

我有两个文件demo.pydemo.kv 当我运行demo.py并点击+Add时,屏幕上会显示。我想在点击namedef add_row(self):函数中button文本框的值。当我点击第一行的按钮,然后我想在test1def add_row(self):获取class RowsExtra(BoxLayout):,然后点击第二行按钮,然后获取test2。我想获得{ {1}} test1变量中的值。之后我根据textName文本框值从数据库中获取数据。

name

demo.py

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.kv

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

1 个答案:

答案 0 :(得分:1)

一个可能的解决方案是通过弹出窗口访问rowsExtra,因为它是你的孩子,但是一个问题是你在构造函数中调用add_row()之前它被分配了父级(父级的赋值)在创建对象之后),我们必须在打开弹出窗口之前消除该指令并再次调用它。要存储文本,我们可以创建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()
[...]