Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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
Python Kivy-标签文本未更改_Python_Kivy - Fatal编程技术网

Python Kivy-标签文本未更改

Python Kivy-标签文本未更改,python,kivy,Python,Kivy,我的第一个问题:),可能对你们来说是个简单的问题。。。 当我按下按钮时,标签文本更改有问题 代码如下。 我尝试了许多不同的新闻定义: self.on_press=lambda:YellowLabel.change_text(YellowLabel) self.on_press=lambda:YellowLabel.change_text 不起作用 self.on_press=YellowLabel.change_text(YellowLabel) 返回: KeyError:“文本” 我猜这可

我的第一个问题:),可能对你们来说是个简单的问题。。。 当我按下按钮时,标签文本更改有问题

代码如下。 我尝试了许多不同的新闻定义:

self.on_press=lambda:YellowLabel.change_text(YellowLabel)
self.on_press=lambda:YellowLabel.change_text
不起作用

self.on_press=YellowLabel.change_text(YellowLabel)
返回:

KeyError:“文本”

我猜这可能是由于冲突,因为函数是在初始化标签之前调用的。当我试着

self.on\u press=YellowLabel.change\u text

它返回:

TypeError:change_text()缺少1个必需的位置参数:“self”

我也尝试过许多其他的变化,但我就是不能得到它。 在下面的示例中,change_文本中的打印功能正在工作,但标签文本并没有改变。 我也试着把change_文本放到YellowLabel类之外,但仍然没有成功

我做错了什么? 现在,我在想,也许改变文本功能是不好的,但我不明白为什么

p、 我确实在这个网站上浏览了许多与kivy有关的问题,并尝试了它们,但我就是无法让它工作

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button

class RedButton(Button):
    def __init__(self,txt="RED LABEL",**kwargs):
        super(RedButton,self).__init__(txt="RED LABEL",**kwargs)
        self.text=(txt)
        self.color=[1,0,0,1]
        self.on_press=lambda:YellowLabel.change_text(YellowLabel)


class YellowLabel(Label):
    def __init__(self,**kwargs):
        super(YellowLabel,self).__init__(**kwargs)
        self.text=("yellow")
        self.color=[1,1,0,1]
        self.id="yelb"

    def change_text(self):
        print("printed")
        self.text=("new yellow text")   

class Window(BoxLayout):
    def __init__(self,**kwargs):
        super(Window,self).__init__(orientation="vertical",**kwargs)
        self.add_widget(RedButton(txt="new red button"))
        self.add_widget(YellowLabel())

class UpdateApp(App):
    def build(self):
        return Window()

if __name__=="__main__":
    UpdateApp().run()

我想说的问题可能是这里的
self.on\u press=lambda:YellowLabel.change\u text(YellowLabel)
缺少对您在Window
\uuuuu init\uuuu
中创建的YellowLable的引用。您应该尝试从Window对象获取YellowLable小部件,并调用
change\u text()

注释中的代码(毕竟是您自己弄明白的:-):


尝试
self.on\u press=lambda:YellowLabel.change\u text()
nope。。。我得到这个错误:TypeError:change\u text()缺少1个必需的位置参数:“self”我想问题可能是这里
self.on\press=lambda:YellowLabel.change\u text(YellowLabel)
缺少对您在窗口
中创建的YellowLable的引用。您应该尝试从窗口对象获取黄色标签小部件,并在thatha上调用
change\u text()
,您是对的。。。它起作用了。。你们会创造一个答案,这样我就可以通过了吗?或者它是如何进行的?这是我的窗口代码(我无法在注释中格式化)rb=RedButton(txt=“new red button”)self.add_widget(rb)yl=YellowLabel()self.add_widget(yl)rb.on_press=lambda:YellowLabel.change_text(yl)
class Window(BoxLayout):
        def __init__(self,**kwargs):
            super(Window,self).__init__(orientation="vertical",**kwargs)
            rb=RedButton(txt="new red button")
            self.add_widget(rb)
            yl=YellowLabel()
            self.add_widget(yl)
            rb.on_press=lambda:YellowLabel.change_text(yl)