简体   繁体   中英

How can I have a button in Kivy that when pressed, creates a label?

我想制作一个按钮,以便在按下按钮时出现一个标签。

Simply define a function that creates a label and assign it to the button's on_press event. Consider the following code:

from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import Gridlayout
from kivy.app import App

layout = Gridlayout(cols = 1)
mybtn = Button(on_press = myfunction)
layout.add_widget(mybtn)

def myfunction():
    mylabel = Label(text = "This is a label")
    layout.add_widget(mylabel)

class myApp(App):
    def build(self):
        return mylayout

if __name__ == '__main__':
    myApp().run()

All of this, however, is covered in the kivy documentation, which is very comprehensive and has very good examples. Before you do anything, you should get the hang of it by reading it and trying out the examples by yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM