Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kivy,Python-自更新标签文本_Python_Oop_Kivy - Fatal编程技术网

Kivy,Python-自更新标签文本

Kivy,Python-自更新标签文本,python,oop,kivy,Python,Oop,Kivy,我正在和我的朋友做一个项目。我工作的一部分是制作一个工作且美观的GUI…:)我对Kivy甚至是oop都很陌生 我遇到了一个问题,即使在进行了深入的研究之后,当我发现一些可能解决我问题的方法时,我也不知道如何将其应用到我的代码中 让我们开门见山吧。我创建了一个由多个屏幕组成的应用程序。在主屏幕上,有3个较大的部分: 存储主按钮的GridLayout ScrollView最初为空 GridLayout,其中存储了一些“操作按钮”(更新、下载等) 我正在使用kivy语言创建它们并将它们添加到屏幕上 我

我正在和我的朋友做一个项目。我工作的一部分是制作一个工作且美观的GUI…:)我对Kivy甚至是oop都很陌生

我遇到了一个问题,即使在进行了深入的研究之后,当我发现一些可能解决我问题的方法时,我也不知道如何将其应用到我的代码中

让我们开门见山吧。我创建了一个由多个屏幕组成的应用程序。在主屏幕上,有3个较大的部分:

  • 存储主按钮的GridLayout
  • ScrollView最初为空
  • GridLayout,其中存储了一些“操作按钮”(更新、下载等)
  • 我正在使用kivy语言创建它们并将它们添加到屏幕上

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


    我知道我的解释可能有点不充分,但我尽可能地简化了它。

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

    您的第三部分按钮是主题(“观察”)对象。第一部分中的标签是观察者对象

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

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

    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
    

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

    您应该使用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:
                    (...)
    
    :
    名称:'main'
    网格布局:
    科尔斯:1
    间距:10
    网格布局:
    id:菜单栏
    科尔斯:6
    菜单按钮:
    菜单按钮:
    标签:
    text:main_按钮。一些_更新#此绑定将在此处发生
    滚动视图:
    (...)
    主屏幕按钮:
    id:主按钮
    科尔斯:4
    一些更新:“我们的新属性将从python文件中使用
    主屏幕按钮:
    文本:“更新”
    发布时:
    (...)
    主屏幕按钮:
    文字:“下载”
    发布时:
    (...)
    主屏幕按钮:
    文本:“暂停”
    发布时:
    (...)
    
    <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:
                    (...)