代码:
import kivy
kivy.require("1.9.1")
from kivy.app import App
from kivy.uix.button import Button
class ButtonApp(App):
def build(self):
btn = Button(text ="这时一个按钮",
font_size ="20sp",
background_color =(0, 0.5, 0.5, 1),
color =(0, 1, 1, 1),
size =(500, 500),
size_hint =(0.3, 0.3),
pos =(500, 500),
)
return btn
root = ButtonApp()
root.run()
执行结果:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/87c5a1126d52b22f9ebe5d96afd9f95c.png#pic_center)
绑定事件:
import kivy
kivy.require("1.9.1")
from kivy.app import App
from kivy.uix.button import Button
class ButtonApp(App):
def build(self):
btn = Button(text="这时一个按钮",
font_size="20sp",
background_color=(0, 0.5, 0.5, 1),
color=(0, 1, 1, 1),
size_hint=(.3, .3),
pos=(500, 500))
btn.bind(on_press=self.callback)
return btn
def callback(self, event):
print("按钮被点了")
root = ButtonApp()
root.run()
事件结果:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/07b84354a6b53009e7ca30933cdf090a.png#pic_center)