【问题标题】:kivy passing widget to different classkivy 将小部件传递给不同的类
【发布时间】:2018-05-09 20:08:18
【问题描述】:

我是 kivy 的新手。我真的想在“popup_add”类的弹出窗口中制作“yes”按钮来读取“invoice”类中的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

提前致谢!

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    好的,很明显你是 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 个类,所以这取决于你,伙计,我希望我能帮上忙

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

    【讨论】:

      【解决方案2】:

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

      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 的结构。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-21
        • 2013-01-31
        • 2015-08-07
        • 1970-01-01
        • 1970-01-01
        • 2020-12-23
        相关资源
        最近更新 更多