Kivy Change Label小部件来自另一个类的文本

时间:2018-03-27 06:19:43

标签: python class widget label kivy

我在从另一个类更新kivy Label Text运行时时遇到了一些麻烦。 我已尝试过类似问题的所有可能解决方案,但无法获得结果。 在我的应用程序中,我有一个主类和另一个弹出类。我想从弹出类更新主类窗口小部件的文本标签。

main.kv文件

<JKmain>:
    the_time: _id_lbl_time
    AnchorLayout:
        anchor_x: 'left'
        anchor_y: 'top'
        BoxLayout:

            orientation: 'vertical'
            id: _tool_box
            size_hint: None,0.75
            width: 300

            Label:
                id: _id_lbl_time
                text: "Total Layers : "

    AnchorLayout:
        anchor_x: 'right'
        anchor_y: 'top'
        GridLayout:
            rows:2
            ...
            ...
            BoxLayout:
                orientation: 'horizontal'                           
                Button: 
                    on_release: app.root.current = "main"
                    text: "SELECT"
                    size_hint: 1,0.2
                    background_color: (1.0, 1.0, 0.0, 1.0)
                    on_release: root.popup_func(self)
                Button: 
                    text: "START"
                    size_hint: 1,0.2
                    background_color: (1.0, 0.0, 1.0, 1.0)
                    on_release: root.change_text(100)
                Button: 
                    text: "EXIT"
                    size_hint: 1,0.2
                    background_color: (1.0, 0.0, 1.0, 1.0)
                    on_release: root.exit_app(self)
<ConfirmPopup>:
    BoxLayout:
        orientation: 'vertical'
        FileChooserIconView:
            id: filechooser
            filters: ['*.zip']

        GridLayout:
            cols: 2 
            size_hint: 1,0.2
            Button:
                text: 'OK'
                on_release: root.dispatch('on_answer',filechooser.selection)
                size_hint: 1,0.2
            Button:
                text: 'Cancel'
                on_release: root.dispatch('on_answer', 'Cancel')
                size_hint: 1,0.2

ConfirmPopup类:

class ConfirmPopup(BoxLayout):

    def __init__(self,**kwargs):
        self.register_event_type('on_answer')
        super(ConfirmPopup,self).__init__(**kwargs)
        ...
        ...
        self.total_images=0

    def on_answer(self, filename): 
        ...
        ...
        ...
        if not isdir_empty == "":
            folders = ([name for name in os.listdir(self.project_path)])
            targets = []
            for i in folders:
                if i.endswith('.png'):
                    targets.append(i)
                    self.total_images = len(targets)
        jk = JKMain()
        jk.change_text(self.total_images)

主类:

class JKMain(AnchorLayout):
    def __init__(self, **kwargs):
        super(JKMain, self).__init__(**kwargs)

    def change_text(self,layers):
        self.the_time.text = "Total Layers : " + str(layers)
        print "Total Layers = " + str(layers)

    def popup_func(self, instance):

        content = ConfirmPopup()
        content.bind(on_answer=self._on_answer)
        self.popup = Popup(title="Select .zip file",
                            content=content,
                            size_hint=(None, None),
                            size=(500,500),
                            auto_dismiss= False)
        self.popup.open()
    def _on_answer(self, instance, answer):
        self.popup.dismiss()

我可以使用 change_text 功能更新文字。从 ConfirmPopup 类开始,我创建 JKMain 的实例并访问 change_text 函数来更改Label,但它无法正常工作

1 个答案:

答案 0 :(得分:0)

问题

它无法正常工作,因为通过创建 JKMain 的另一个实例,现在您有两个 JKMain 实例,并且您没有访问第一个实例,即根。 / p>

解决方案

作为解决方案正常运行的演示, self.total_images 在方法 on_answer 中设置为 8 ,然后再调用方法 change_text

kv文件

  1. 调用 root.popup_func 时无需传递 self
  2. 将根的实例传递给方法 on_answer 。有关详细信息,请参阅示例和输出。
  3. Python脚本

    1. 将新参数 JKMain 添加到methd on_answer
    2. 向方法 _on_answer 添加新参数 obj
    3. 片段

      main.kv

      <JKmain>:
      ...
                          on_release: root.popup_func()
      ...
      <ConfirmPopup>:
      ...
                      on_release: root.dispatch('on_answer', filechooser.selection, app.root)
      

      main.py

      class ConfirmPopup(BoxLayout):
      
          ...
          def on_answer(self, filename, JKMain):
              self.total_images = 8
              print("JKMain=", JKMain)
              JKMain.change_text(self.total_images)
      
      class JKMain(AnchorLayout):
          ...
          def popup_func(self):
              ...
      
          def _on_answer(self, instance, answer, obj):
              self.popup.dismiss()
      

      实施例

      main.py

      from kivy.app import App
      from kivy.uix.anchorlayout import AnchorLayout
      from kivy.uix.boxlayout import BoxLayout
      from kivy.uix.popup import Popup
      
      
      class ConfirmPopup(BoxLayout):
      
          def __init__(self, **kwargs):
              self.register_event_type('on_answer')
              super(ConfirmPopup, self).__init__(**kwargs)
              self.total_images = 0
      
          def on_answer(self, filename, JKMain):
              self.total_images = 8
              print("JKMain=", JKMain)
              JKMain.change_text(self.total_images)
      
      
      class JKMain(AnchorLayout):
          def __init__(self, **kwargs):
              super(JKMain, self).__init__(**kwargs)
      
          def change_text(self, layers):
              self.the_time.text = "Total Layers : " + str(layers)
              print("Total Layers = " + str(layers))
      
          def popup_func(self):
      
              content = ConfirmPopup()
              content.bind(on_answer=self._on_answer)
              self.popup = Popup(title="Select .zip file",
                                 content=content,
                                 size_hint=(None, None),
                                 size=(500, 500),
                                 auto_dismiss=False)
              self.popup.open()
      
          def _on_answer(self, instance, answer, obj):
              self.popup.dismiss()
      
      
      class Main(App):
      
          def build(self):
              return JKMain()
      
      
      if __name__ == "__main__":
          Main().run()
      

      main.kv

      #: kivy 1.10.0
      
      <JKmain>:
          the_time: _id_lbl_time
          AnchorLayout:
              anchor_x: 'left'
              anchor_y: 'top'
              BoxLayout:
      
                  orientation: 'vertical'
                  id: _tool_box
                  size_hint: None,0.75
                  width: 300
      
                  Label:
                      id: _id_lbl_time
                      text: "Total Layers : "
      
          AnchorLayout:
              anchor_x: 'right'
              anchor_y: 'top'
              GridLayout:
                  rows:2
                  BoxLayout:
                      orientation: 'horizontal'
                      Button:
                          on_release: app.root.current = "main"
                          text: "SELECT"
                          size_hint: 1,0.2
                          background_color: (1.0, 1.0, 0.0, 1.0)
                          on_release: root.popup_func()
                      Button:
                          text: "START"
                          size_hint: 1,0.2
                          background_color: (1.0, 0.0, 1.0, 1.0)
                          on_release: root.change_text(100)
                      Button:
                          text: "EXIT"
                          size_hint: 1,0.2
                          background_color: (1.0, 0.0, 1.0, 1.0)
                          on_release: root.exit_app(self)
      
      <ConfirmPopup>:
          BoxLayout:
              orientation: 'vertical'
              FileChooserIconView:
                  id: filechooser
                  filters: ['*.zip']
      
              GridLayout:
                  cols: 2
                  size_hint: 1,0.2
                  Button:
                      text: 'OK'
                      on_release: root.dispatch('on_answer', filechooser.selection, app.root)
                      size_hint: 1,0.2
                  Button:
                      text: 'Cancel'
                      on_release: root.dispatch('on_answer', 'Cancel')
                      size_hint: 1,0.2
      

      输出

      Img01 - AppStartup Img02 - zip File Selected, OK Button Clicked Img03 - Label Total Images Updated