简体   繁体   English

如何使用 Kivy 和 Python 访问 class 之外的数据列表

[英]How do I access the data list outside the class with Kivy and Python

I am trying to create a grid and want to retrieve the data of users in a list I am parallelly working on mysql as well so as to add the data in it so I basically want to get access to the data list which I have used in the pressed method in the AppLayout Class so as to go ahead.我正在尝试创建一个网格并希望在列表中检索用户的数据我同时在 mysql 上工作,以便在其中添加数据,所以我基本上想访问我使用过的数据列表AppLayout Class 中的按下方法,以便在 go 前面。

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button


class Applayout(GridLayout):

    def __init__(self , **kwargs): 

        super(Applayout , self).__init__(**kwargs)
        

        self.cols = 1

        self.inside = GridLayout()
        self.inside.cols = 2

        self.inside.add_widget(Label(text = "First Name: "))
        self.first_name = TextInput(multiline = False)
        self.inside.add_widget(self.first_name)

        self.inside.add_widget(Label(text = "Last Name: "))
        self.last_name = TextInput(multiline = False)
        self.inside.add_widget(self.last_name)

        self.inside.add_widget(Label(text = "Email: "))
        self.email = TextInput(multiline = False)
        self.inside.add_widget(self.email)

        self.add_widget(self.inside)

        self.submit = Button(text = "Submit", font_size = 40)
        self.submit.bind(on_press = self.pressed) 
        self.add_widget(self.submit)

    def pressed(self , instance):
        first_name = self.first_name.text
        last_name = self.last_name.text
        email = self.email.text

        person_data = {"First Name": first_name , "Last Name": last_name , "E-Mail": email}
        data = []
        data.append(person_data)

        print(f"Name: {first_name} {last_name} , E-Mail: {email} ")
        print("Submitted")

        self.first_name.text = ""
        self.last_name.text = ""
        self.email = ""


class Granth_BagadiaApp(App):
    def build(self):
        return Applayout()



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

class MyApp(App):
    def build(self):
        return MyGrid()
    def datas(self):
        return MyGrid().data

This is what I did But now how do I print this Cause on typing print(MyApp().datas()) I get [{'Name': <ObjectProperty name=nameee>, 'E-Mail': <ObjectProperty name=emailll>}]这就是我所做的但是现在我如何打印这个原因在输入print(MyApp().datas())我得到[{'Name': <ObjectProperty name=nameee>, 'E-Mail': <ObjectProperty name=emailll>}]

All depends how you want to use it.一切都取决于你想如何使用它。


Normally I would run directly function with SQL and send data as argument.通常我会直接运行 function 和 SQL 并将data作为参数发送。

class Applayout()

    def pressed(self, instance):
        data = [{
            "First Name": self.first_name.text, 
            "Last Name": self.last_name.text, 
            "E-Mail": self.email.text
        }]

        function_with_sql(data)
        #other_object.function_with_sql(data)

        # ... other code ...

Eventually I would use queue to communicate with other process.最终我会使用queue与其他进程进行通信。

class Applayout()

    def pressed(self, instance):
        data = [{
            "First Name": self.first_name.text, 
            "Last Name": self.last_name.text, 
            "E-Mail": self.email.text
        }]

        queue_to_other_process.put(data)

        # ... other code ...

But if I would have to give access to data then first I would use self.data但是,如果我必须授予对数据的访问权限,那么首先我会使用self.data

class Applayout()

    def __init___(...):
        self.data = [] # default value at start 

        # ... other code ...

    def pressed(self, instance):
        self.data = [{
            "First Name": self.first_name.text, 
            "Last Name": self.last_name.text, 
            "E-Mail": self.email.text
        }]

        # ... other code ...

and I would assign class to global variable我会将 class 分配给全局变量

applayout = Applayout()

class Granth_BagadiaApp(App):
    def build(self):
        return applayout

and now everyone can access data as applayout.data .现在每个人都可以通过applayout.data访问数据。


Eventaully I would create data as external object and send data to Applayout and other classes as argument. Eventaully 我会将data创建为外部 object 并将data作为参数发送到Applayout和其他类。

data = []

class Applayout()

    def __init___(..., data):
        self.data = data   
        
        # ... other code ...
        
    def pressed(self, instance):
        self.data.clear()  # not `self.data = []` because it will remove access to external `data`
        
        self.data.append({
            "First Name": self.first_name.text, 
            "Last Name": self.last_name.text, 
            "E-Mail": self.email.text
        })

        # ... other code ...

applayout = Applayout(data)

class Granth_BagadiaApp(App):
    def build(self):
        return applayout

Of course with external data you could use data directly in classes - without sending it as argument - but as says The Zen of Python "Explicit is better than implicit."当然,对于外部data ,您可以直接在类中使用data - 无需将其作为参数发送 - 但正如Python 的禅宗所说"Explicit is better than implicit." - and it means data as argument. - 它意味着data作为论据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM