Kivy - editing label when button clicked

妖精的绣舞 提交于 2020-01-04 02:03:06

问题


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

回答1:


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

from functools import partial

change callback signature for

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

then bind the callback like this

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

that should do it.




回答2:


Also, make sure you have the callback function indented and directly after the build function. Otherwise the callback function won be recognized in the bind statement.



来源:https://stackoverflow.com/questions/12339167/kivy-editing-label-when-button-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!