Python Kivy-单击按钮时编辑标签
我希望按钮1在单击时编辑标签“etykietka”,但我不知道如何编辑。你有什么想法吗Python Kivy-单击按钮时编辑标签,python,button,refresh,kivy,Python,Button,Refresh,Kivy,我希望按钮1在单击时编辑标签“etykietka”,但我不知道如何编辑。你有什么想法吗 class Zastepstwa (App): def build(self): lista=WebOps().getList() layout = BoxLayout(orientation='vertical') etykietka = Label(text='aa', font_size=10) button1 = Button(text='aa') butto
class Zastepstwa (App):
def build(self):
lista=WebOps().getList()
layout = BoxLayout(orientation='vertical')
etykietka = Label(text='aa', font_size=10)
button1 = Button(text='aa')
button1.bind(callback)
layout.add_widget(etykietka)
layout.add_widget(button)
return layout
def callback (instance):
newLabelText='kkk'
#?
您需要将标签传递给回调函数,一个很好的方法是使用分部函数
from functools import partial
更改的回调签名
def callback(label, instance, *args):
label.text='kkk'
然后像这样绑定回调
button1.bind(on_press=partial(callback, etykieta))
应该这样做。另外,确保回调函数缩进并直接位于构建函数之后。否则,将无法在bind语句中识别回调函数 嗯,我在同一个类中有回调方法,所以我写了这样的东西:“button1.bind(on_press=partial(self.callback,etykieta)),(在回调中还添加了'self'参数),python给出了mi错误。为什么?很好,当我面对这样的问题时我不会哭。Kip感知:-)