kivy将小部件传递给不同的班级

时间:2018-05-02 06:10:52

标签: python kivy

我是kivy的新手。我真的很想做'#34;是" " popup_add"弹出窗口内的按钮class来读取"发票"中的listview小部件。类。

我搜索了许多关于将小部件传递给不同类的帖子,但是所有帖子都要求在父类中包含该类,我不想显示弹出窗口,除非用户单击该按钮。

我在这里提取了重要的代码:

python文件:

class invoice(BoxLayout):
    shop_list_input = ObjectProperty()
    shop_name_input = ObjectProperty()

    def add_shop(self):
        add_popup = popup_add()
        add_popup.open()

class popup_add(Popup):

    def pop_yes(self, text_input,listView):
        store_name = text_input
        listView.adapter.data.extend([store_name])
        listView._trigger_reset_populate()

kivy文件:

#: import main testKivy
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ListItemButton kivy.uix.listview.ListItemButton

invoice:

<invoice>:

    orientation: "vertical"
    padding: 10
    spacing: 10
    shop_name_input: shop_name
    shop_list_input: shop_list

    BoxLayout:
        orientation: "vertical"
        size_hint_y: None
        height: "60dp"

        TextInput:
# i found on google to use app to allow other class to access text, is there 
# a way to make listview global similar to app?
            on_text: app.input_sentence=self.text
            id: shop_name
            size_hint: 1,1

    BoxLayout:
        size_hint_y: None
        height: "40dp"

        Button:
            text: "Add Store"
        size_hint_x: 15
        on_press: root.add_shop()

# anyway to make listview  widget accessible to other classes?
    ListView:
        id: shop_list
        adapter:
            ListAdapter(data=["Shop A","Shop B","Shop C"], cls=main.ShopListButton)

<popup_add>:
    title: 'notification'
    auto_dismiss: False
    size_hint: .6,.3
    FloatLayout:
        Button:
            text: "Yes"
# here is where i want to insert listview, inside root.pop_yes.
            on_press: root.pop_yes(app.input_sentence)
            pos_hint: {"x": .2, 'y':.25}
            size_hint: .2, .2
        Button:
            text: "No"
            on_press: root.dismiss()
            pos_hint: {"x": .6, 'y':.25}
            size_hint: .2, .2
        Label:
            text: "Are you sure to add " + app.input_sentence
            pos_hint: {"x": .2, 'y':.65}
            size_hint: .6, .3

提前致谢!

2 个答案:

答案 0 :(得分:0)

好的,所以很明显你是Kivy的新手,但没问题。

好的,所以不要为弹出窗口创建一个单独的类,而是直接在发票类中创建一个弹出窗口,程序的python部分我将给你一个例子。

在我的代码中,我做了这个

def success(self):
            popup18 = Popup(title='Loading Screen',content=Label(text='SENT',font_size=40,pos_hint={'center_y': 0.5, 'center_y': .5}),size_hint=(None, None), size=(400, 400),auto_dismiss=True)
            popup18.open()

你可以在https://kivy.org/docs/api-kivy.uix.popup.html

处编写这些内容的语法

我尽量减少课程的数量,但即便如此,在我的代码中,我有120多个课程,所以我希望我帮助

不要忘记从Kivy导入弹出窗口

答案 1 :(得分:0)

您可以为__init__定义popup_add函数并让它接受变量。

class invoice(BoxLayout):
    shop_list_input = ObjectProperty()
    shop_name_input = ObjectProperty()

    def add_shop(self):
        add_popup = popup_add(self.ids.shop_list)
        add_popup.open()

class popup_add(Popup):
    def __init__(self, shop_list_widget, **kwargs):
        super(popup_add, self).__init__(**kwargs):
        self.shop_list_widget = shop_list_widget

    def pop_yes(self, text_input,listView):
        store_name = text_input
        listView.adapter.data.extend([store_name])
        listView._trigger_reset_populate()

这样做仍然可以让你在kv文件中定义pop的结构。