Javier Stacul
Javier Stacul

Reputation: 63

Kivy: pass data to another class

I`m trying to make a simple GUI with Kivy(1.9) using a popup to change some options from a list and save it to a db, for example. When i call popup(), Python(3.4.5) crash..

main.py:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.properties import ListProperty
from kivy.lang import Builder

Builder.load_string('''
<PopView>:
    title: 'Popup'
    size_hint: (.8, .8)
    Button:
        text: 'Save'
''')

class MainApp(App):

    def build(self):
        b = Button(text='click to open popup')
        b.bind(on_click=self.view_popup())
        return b

    def view_popup(self):
        a=PopView()
        a.data=[1,2,3,4] #e.g.
        a.open()

class PopView(Popup):

    def __init__(self):
        self.data = ListProperty()

    def save_data(self):
        #db.query(self.data)
        pass


if __name__ in ('__main__', '__android__'):
    MainApp().run()

Upvotes: 3

Views: 1291

Answers (1)

el3ien
el3ien

Reputation: 5415

Here are a couple of things.

First, if you are going to overite __init__ remember to call super
But in this simple case you dont need __init__

Then, there is no on_click event on Button. Use on_press or on_release

And last but not least: You dont need to call the method in the bind function. Only pass it (without ())

So now your example looks like this.

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.properties import ListProperty
from kivy.lang import Builder

Builder.load_string('''
<PopView>:
    title: 'Popup'
    size_hint: (.8, .8)
    Button:
        text: 'Save'
''')

class MainApp(App):

    def build(self):
        b = Button(text='click to open popup')
        b.bind(on_release=self.view_popup)
        return b

    def view_popup(self,*args):
        a = PopView()
        a.data=[1,2,3,4] #e.g.
        a.open()

class PopView(Popup):
    data = ListProperty()

    def save_data(self):
        #db.query(self.data)
        pass


if __name__ in ('__main__', '__android__'):
    MainApp().run()

Upvotes: 2

Related Questions