Kivy,Python - 自我更新标签文本

时间:2015-12-30 12:41:49

标签: python oop kivy

我和朋友一起做项目。我的工作就是制作一个工作且漂亮的GUI ...... :)我对Kivy甚至是oop都很陌生。

我遇到了一个问题,即使经过深入研究,当我找到可能解决问题的方法时,我也不知道如何将其应用到我的代码中。

让我们说明问题。我创建了一个由几个屏幕组成的应用程序。在主屏幕上,有3个较大的部分:

  1. 存储主按钮的GridLayout
  2. ScrollView最初为空
  3. GridLayout,其中有一些"操作按钮"存储(更新,下载等)
  4. 我使用kivy语言创建它们并将它们添加到屏幕上!

    我的问题是我想在第1部分中添加自我更新的标签文字。通过单击第3部分按钮,我想更新文本。 第3节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类中添加一个函数来更新一些变量,我想在单击这三个按钮之一时调用它。我不知道如何更新标签的文本,因为它在python文件中甚至没有提到,但它们都存储在MainScreen类中。我无法提出任何工作的想法,我很困惑,请帮帮我:)

    我知道我的解释可能有点不足,但我尽我所能尽量简化它。

2 个答案:

答案 0 :(得分:0)

可以使用Observer模式解决此问题。

您的第3部分按钮是主题(&#34;观察&#34;)对象。第1部分中的标签是 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

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

答案 1 :(得分:0)

你应该只使用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:
                (...)