Kivy:将数据传递给另一个类

2024-09-28 22:35:08 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,我试图用Kivy(1.9)制作一个简单的GUI,使用弹出窗口来更改列表中的一些选项并将其保存到数据库中。当我调用popup()时,Python(3.4.5)崩溃。。在

在主.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()

Tags: textfromimportselfappdatadefbuilder
1条回答
网友
1楼 · 发布于 2024-09-28 22:35:08

这里有几件事。在

首先,如果您要覆盖__init__,记得打电话给super
但在这个简单的例子中,您不需要__init__

那么,on_click上没有on_click事件。使用on_press或{}

最后但并非最不重要的是:您不需要在bind函数中调用该方法。只传递它(不带()

现在你的例子是这样的。在

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

相关问题 更多 >