Kivy - 单击按钮时编辑标签

2022-01-15 00:00:00 python kivy button refresh

问题描述

我希望 button1 在单击时编辑标签etykietka",但我不知道如何.你有什么想法吗?

I wish button1 to edit Label 'etykietka' when clicked, but i don't know how. Have you got some ideas?

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'
    #?


解决方案

你需要将你的标签传递给回调,一个很好的方法是使用偏函数

you need to pass your label to callback, a nice way to do it is to use the partial function

from functools import partial

更改回调签名

def callback(label, instance, *args):
    label.text='kkk'

然后像这样绑定回调

button1.bind(on_press=partial(callback, etykieta))

应该这样做.

相关文章