首页 > 解决方案 > 访问 StringProperty 数据并使用它 (KIVY)

问题描述

首先我想指出的是,我已经检查了许多类似的问题,这些问题最终使用StringProperty()这些问题似乎只有在按钮的 on_release 或与其他 UI 小部件交互之后访问其中的数据时才能按预期工作。

我能够在屏幕登录中获取字符串属性并成功为其设置数据,如下所示

登录.py

 self.parent.parent.parent.admin_widget.mylocation =  str(self.ulocation) #DAta from textinput

这里我的 admin.py 文件如下所示: 我只是启动字符串属性,现在我有一个方法get_users,该方法有一个 DataTable 并将小部件添加到屏幕,问题是最初加载时字符串属性为空,稍后在 textinput 提交后更新,我希望更改屏幕应该调用 get_users 来捕获 stringproperty 中的当前位置值。如何获得这个值 mylocation 并在第一次正确使用它来加载表中的数据?请帮帮我

class AdminWindow(BoxLayout):
data_items = ListProperty([])
mylocation = StringProperty()

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

    print(self.mylocation)

    content = self.ids.scrn_contents
    users = self.get_users()
    userstable = DataTable(table=users)
    content.add_widget(userstable)

def get_users_new(self):
    #Here is where I access the stringProperty value
    mycursor.execute("SELECT * FROM products WHERE location =%s", (self.mylocation,))
    rows = mycursor.fetchall()
    arrows = str(mycursor.rowcount)
    self.ids.total_inventory.text = arrows + " Products"
    # create data_items
    for row in rows:
        for col in row:\
            self.data_items.append(col)

那么最初如何创建具有位置值的 UI 数据表呢?

标签: pythonkivy

解决方案


我想通了,首先我必须将 init(这是加载表格的地方)部分中的所有内容放入一个方法中。因此,我所要做的就是在单击按钮后从 login.py 窗口调用此方法,因此当用户访问管理页面时,数据已经被重新加载。


推荐阅读