Python GUI 03----Button
来源:互联网 发布:批发服装软件哪个好 编辑:程序博客网 时间:2024/05/07 19:40
1.一个简单的Button应用
from tkinter import *#定义Button的回调函数def helloButton(): print ('hello button')root = Tk()#通过command属性来指定Button的回调函数Button(root,text = 'Hello Button',command = helloButton).pack()root.mainloop()
2.测试Button的relief属性
运行下面的代码可以看到Button的各个不同效果。
from tkinter import *root = Tk()#flat, groove, raised, ridge, solid, or sunkenButton(root,text = 'hello button',relief=FLAT).pack()Button(root,text = 'hello button',relief=GROOVE).pack()Button(root,text = 'hello button',relief=RAISED).pack()Button(root,text = 'hello button',relief=RIDGE).pack()Button(root,text = 'hello button',relief=SOLID).pack()Button(root,text = 'hello button',relief=SUNKEN).pack()root.mainloop()
3.与Label一样,Button也可以同时显示文本与图像,使用属性compound
from tkinter import *root = Tk()#图像居下,居上,居右,居左,文字位于图像之上Button(root,text = 'botton',compound = 'bottom',bitmap = 'error').pack()Button(root,text = 'top',compound = 'top',bitmap = 'error').pack()Button(root,text = 'right',compound = 'right',bitmap = 'error').pack()Button(root,text = 'left',compound = 'left',bitmap = 'error').pack()Button(root,text = 'center',compound = 'center',bitmap = 'error').pack()root.mainloop()
4.控件焦点问题
创建三个Button,各自对应回调函数;将第二个Button设置焦点,程序运行是按“Enter”,判断程序的打印结果from tkinter import *def cb1(): print ('button1 clicked')def cb2(event): print ('button2 clicked')def cb3(): print ('button3 clicked') root = Tk()b1 = Button(root,text = 'Button1',command = cb1)b2 = Button(root,text = 'Button2')b2.bind("<Return>",cb2) #Return事件相应回车点击。Enter事件响应的是mouseoverb3 = Button(root,text = 'Button3',command = cb3)b1.pack()b2.pack()b3.pack()b2.focus_set() #将焦点定在按钮b2上root.mainloop()上例中使用了bind方法,它建立事件与回调函数(响应函数)之间的关系,每当产生<Enter>事件后,程序便自动的调用cb2,与cb1,cb3不同的是,它本身还带有一个参数----event,这个参数传递响应事件的信息。
5.指定Button的宽度与高度
width: 宽度
heigth: 高度
heigth: 高度
使用三种方式设置该属性:
1.在创建Button对象时,指定宽度与高度
2.使用属性width和height来指定宽度与高度
3.使用configure方法来指定宽度与高度
上述的三种方法同样也适合其他的控件
from tkinter import *root = Tk()b1 = Button(root,text = '30X1',width = 30,height = 2)b1.pack()b2 = Button(root,text = '30X2')b2['width'] = 30b2['height'] = 3b2.pack()b3 = Button(root,text = '30X3')b3.configure(width = 30,height = 3)b3.pack()root.mainloop()
6.设置Button文本在控件上的显示位置
anchor:使用的值为:n(north),s(south),w(west),e(east)和ne,nw,se,sw,就是地图上的标识位置了,使用width和height属性是为了显示各个属性的不同。from Tkinter import *root = Tk()#简单就是美!for a in ['n','s','e','w','ne','nw','se','sw']: Button(root, text = 'anchor', anchor = a, width = 30, height = 4).pack()
7.改变Button的前景色与背景色
from tkinter import *root = Tk()bfg = Button(root,text = 'change foreground',fg = 'red')bfg.pack()bbg = Button(root,text = 'change backgroud',bg = 'blue')bbg.pack()root.mainloop()
8.设置Button的边框bd(bordwidth):缺省为1或2个像素
# 创建5个Button边框宽度依次为:0,2,4,6,8from tkinter import *root = Tk()for b in [0,1,2,3,4]: Button(root, text = str(b), bd = b).pack()root.mainloop()
9.设置Button状态
from tkinter import *root = Tk()def statePrint(): print ('state')for r in ['normal','active','disabled']: Button(root, text = r, state = r, width = 30, command = statePrint).pack()root.mainloop()例子中将三个Button在回调函数都设置为statePrint,运行程序只有normal和active激活了回调函数,而disable按钮则没有,对于暂时不需要按钮起作用时,可以将它的state设置为disabled属性
10.绑定Button与变量设置Button在textvariable属性
from tkinter import *root = Tk()def changeText(): if b['text'] == 'text': v.set('change') else: v.set('text')v = StringVar()b = Button(root,textvariable = v,command = changeText)v.set('text')b.pack()root.mainloop()将变量v与Button绑定,当v值变化时,Button显示的文本也随之变化
如果有什么疑问欢迎到我的微信公众号提问~
1 0
- Python GUI 03----Button
- python中GUI,Label和Button案例
- GUI button
- EZ Gui 创建Button
- 【GUI组件】按钮Button
- GUI.Layout.Button与GUI.Button的区别
- Python GUI
- python gui
- Python GUI
- Python GUI
- python-gui
- python GUI
- unity 3d GUI.Button介绍
- Python GUI库
- Python的GUI编程
- GUI Programming in Python
- 6.python GUI 引库报错
- Python GUI (Tkinter)
- nux下导入、导出mysql数据库命令
- 从源码中Activity 的定义来理解 Activity
- unity3D中UGUI改变字体的颜色
- ava 使用 comet4j 主动向客户端推送信息 简单例子
- WinPcap编程调试解决办法
- Python GUI 03----Button
- 常量和约束访问
- OC 内存管理 --- 引用计数简单使用
- ORA-12545 因目标主机或对象不存在,连接失败
- UITabbar 中的 item的字体颜色 item点击后的tint颜色 和图片颜色的修改 UINavigationBar字体设置
- JQuery 1.11.1版本的live事件
- 关于jdbc保存后,返回自动增长的id,使用stmt.getGeneratedKeys();
- js 字符串去重
- 最简单的视音频播放示例1:总述