kivy提供了Button按钮一系列属性来改变样式,下面列了常用的一些Button属性并用实操案例进行演练学习。

新建一个main.py,内容代码如下:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout class ButtonFloatLayout(FloatLayout):
def __init__(self,**kwargs):
super(ButtonFloatLayout, self).__init__(**kwargs) bt=Button(text='按钮', background_color=[1,.5,.5,1],on_release=self.release_button, pos=(300,0),size_hint=(.2,.15)) #添加一个按钮
bt.bind(on_press=self.press_button) #绑定触发事件
self.add_widget(bt) #添加到布局 def press_button(self,arg):
print('按下按钮事件已运行') def release_button(self,arg):
print('按下并释放时触发事件已运行') class ButtonApp(App):
def build(self):
return ButtonFloatLayout() if __name__ =='__main__':
ButtonApp().run()

再建一个button.kv文件,代码内容如下:

<MyButton@Button>:   #自定义按钮,设置按钮的公共属性
size_hint:.2,.15 #设置按钮大小 <ButtonFloatLayout>:
#设置布局背景色,大小
canvas:
Color:
rgba:[1,1,1,1]
Rectangle:
pos:self.pos
size:self.size Button: #使用原生按钮
text:'按钮00' #按钮显示文本
bold:10 size_hint:.2,.15 #设置按钮大小
pos:65,400 #设置x座标=65,y座标=400的位置显示此按钮
background_normal:'' #标准背景颜色
background_color:[.1,.5,.5,1] #背景颜色 MyButton: #自定义按钮
text:'按钮01'
pos:315,400
disabled:True #禁用状态为真 MyButton:
text:'按钮02'
color:[.8,.3,.3,1] #设置按钮字体颜色
pos:565,400
on_release:root.release_button(self) #触发事件 on_press:
root.press_button(self) #触发事件,在KV内要触发多个事件要换行写
print('laizl.com') MyButton:
text:'按钮03'
font_size:15
pos:65,150 MyButton:
text:'按钮04'
font_size:25
pos:315,150
state:'normal' #按钮状态 MyButton:
text:'按钮05'
pos:565,150
state:'down' #按下状态

建好文件,就可以运行main.py文件看到效果了。也可直接到这里下载案例源码学习。

最新文章

  1. NOIP模拟赛20161023
  2. javascript之AJAX学习
  3. [tp3.2.1]查询(2)
  4. 物料分类账 [COML] PART 2 - 总体流程
  5. 开始写github
  6. LOMO效果
  7. Windows2003计划任务设置操作手册
  8. CURL 宏定义列表
  9. java多线程:ReentrantReadWriteLock读写锁使用
  10. jQuery Validate【强大的表单验证】
  11. Combox下拉绑定DataGridView
  12. js制作ppt阅读文档类型
  13. css,解决文字与图片对齐的问题
  14. 设置Firefox(火狐)浏览器的中文菜单/界面
  15. VUE 全局变量的几种实现方式
  16. 开源通用爬虫框架YayCrawler-开篇
  17. Node http和express和mysql
  18. spring boot 使用拦截器 无法注入 配置值 、bean问题
  19. C#重新启动时,关闭较早的进程
  20. .net 高级写法总结

热门文章

  1. Linux:linux下解压*压缩tar.xz、tar、tar.gz、tar.bz2、tar.Z、rar、zip、war等文件方法
  2. 《计算机组成与体系结构:性能设计》读后小记 12、CPU的结构和功能
  3. 16 shell select in 循环
  4. 在spring boot使用总结(九) 使用yaml语言来写配置文件
  5. tr 字符转换命令
  6. Git远程操作详解(clone、remote、fetch、pull、push)
  7. 构造函数 析构函数的区别与联系 C#
  8. 手写Spring框架,是时候撸个AOP与Bean生命周期融合了!
  9. vue2.x移动端ui框架选型
  10. PGSQL数据库里物化视图【materialized view】