【问题标题】:Kivy, Python - self-updating label textKivy,Python - 自更新标签文本
【发布时间】:2015-12-30 12:41:48
【问题描述】:

我正在和我的朋友一起做一个项目。我的工作职责是制作一个可以工作且外观漂亮的 GUI...:) 我对 Kivy 还是很陌生,甚至是 oop。

我遇到了一个问题,即使经过扎实的研究,当我发现一些可能是我的问题的解决方案时,我不知道如何将它应用到我的代码中。

让我们进入正题。我创建了一个由多个屏幕组成的应用程序。在主屏幕上,有 3 个较大的部分:

  1. 存储主按钮的GridLayout
  2. ScrollView 最初为空
  3. 存储一些“操作按钮”的网格布局(更新、下载等)

我正在使用 kivy 语言来创建它们并将它们添加到屏幕上!

我的问题是我想在我的 第 1 部分 中添加自更新标签文本。通过单击第三部分按钮,我想更新文本。 第三部分 GridLayout 在 .py 文件中有一个单独的类,其中我有一些绑定到键的功能。

<MainScreen>:
name: 'main'

GridLayout:
    cols: 1
    spacing: 10

    GridLayout:
        id: menu_bar
        cols: 6
        MenuButton:
        MenuButton:
        Label:
           text: "I want to be auto-updated"

    ScrollView:
        (...)

    MainScreenButtons:
        id: main_buttons
        cols:4

        MainScreenButton:
            text: "UPDATE"
            on_release:
                (...)
        MainScreenButton:
            text: "DOWNLOAD"
            on_release:
                (...)
        MainScreenButton:
            text: "PAUSE"
            on_release:
                (...)

在 python 文件中,我创建了 MainScreenButtons 类,其中包含我使用这些键的所有功能。

class MainScreen(Screen):
    pass

class MainScreenButtons(GridLayout):
    def download(self):
         pass
    (...)

基本上,我想向 MainScreenButtons 类添加一个函数来更新一些变量,并且我想在单击这 3 个按钮之一时调用它。我不知道如何更新标签的文本,因为它甚至没有在 python 文件中提到,但它们都存储在 MainScreen 类中。我想不出任何工作的想法,我很困惑,请帮帮我:)

我知道我的解释可能有点不够,但我已尽力简化它。

【问题讨论】:

    标签: python oop kivy


    【解决方案1】:

    这个问题可以用Observer 模式解决。

    您的第 3 部分按钮是 subject(“观察到的”)对象。您在第一部分的标签是 observer 对象。

    每当主题被更新(即被点击)时,它应该通知它的观察者,以便他们也改变他们的状态(即显示的文本)。

    我将把它作为一个简单的代码示例,请根据您的需要进行调整:

    class Button:
        def __init__(self):
            self._observers = []
    
        def attach(self, obj):
            self._observers.append(obj)
    
        def detach(self, obj):
            self._observers.remove(obj)
    
        def notifyAll(self, msg):
            for o in self._observers:
                o.update(msg);
    
        # this is the method that will be called whenever you click the button
        def onClickHandler(self, evt):
            # do whatever you should do...
            self.notifyAll("Clicked!")
    
    class Label:
        def update(self, msg):
            # here you update your text based on the message you receive
    

    然后你做:

    label = Label("Some text")
    button = Button("Click")
    
    button.attach(label);
    
    # when you click the button, the label will be notified it 
    # was clicked and will update itself
    

    这是一般的想法。根据您的需要进行调整。

    【讨论】:

      【解决方案2】:

      您应该只使用 kivy 的内置绑定与 kv lang。

      您的代码稍作修改:

      from kivy.properties import StringProperty
      
      class MainScreen(Screen):
          pass
      
      class MainScreenButtons(GridLayout):
      
          some_update = StringProperty("default value")
      
          def download(self):
              self.some_update = "Downloading now...
      

      这是kv文件:

      <MainScreen>:
          name: 'main'
      
      GridLayout:
          cols: 1
          spacing: 10
      
          GridLayout:
              id: menu_bar
              cols: 6
              MenuButton:
              MenuButton:
              Label:
                 text: main_buttons.some_update #this binding will happen here
      
          ScrollView:
              (...)
      
          MainScreenButtons:
              id: main_buttons
              cols:4
              some_update: "" #our new propery to be used from the python file
              MainScreenButton:
                  text: "UPDATE"
                  on_release:
                      (...)
              MainScreenButton:
                  text: "DOWNLOAD"
                  on_release:
                      (...)
              MainScreenButton:
                  text: "PAUSE"
                  on_release:
                      (...)
      

      【讨论】:

        猜你喜欢
        • 2023-03-09
        • 2014-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-08
        相关资源
        最近更新 更多