Tkinter教程之Button篇(1)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811298
#Tkinter教程之Button篇(1)
#Button功能触发事件
'''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()
'''
执行的结果:每次点击一次,程序向标准输出打印'hello button',以上为Button使用方法,可以
再做一下简化,如不设置Button的回调函数,这样也是允许的但这样的结果与Label没有什么太
大的区别,只是外观看起来有所不同罢了,失去了Button的作用。
from Tkinter import *
root = Tk()
#下面的relief = FLAT设置,就是一个Label了!!!
Button(root,text = 'hello button',relief=FLAT).pack()
root.mainloop()
'''
'''2.测试Button的relief属性'''
#运行下面的代码可以看到Button的各个不同效果,均没有回调函数。
from Tkinter import *
root = Tk()
#flat, groove, raised, ridge, solid, or sunken
Button(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()
'''
Button显示图像
image:可以使用gif图像,图像的加载方法img = PhotoImage(root,file = filepath
bitmap:使用X11 格式的bitmap,Windows的Bitmap没法显示的,在Windows下使用GIMP2.4将windows
Bitmap转换为xbm文件,依旧无法使用.linux下的X11 bitmap编辑器生成的bitmap还没有测试,但可
以使用内置的位图。
(1).使用位图文件
bp = BitmapImage(file = "c:/python2.xbm")
Button(root,bitmap = bp).pack()
(2).使用位图数据
BITMAP = """
#define im_width 32
#define im_height 32
static char im_bits[] = {
0xaf,0x6d,0xeb,0xd6,0x55,0xdb,0xb6,0x2f,
0xaf,0xaa,0x6a,0x6d,0x55,0x7b,0xd7,0x1b,
0xad,0xd6,0xb5,0xae,0xad,0x55,0x6f,0x05,
0xad,0xba,0xab,0xd6,0xaa,0xd5,0x5f,0x93,
0xad,0x76,0x7d,0x67,0x5a,0xd5,0xd7,0xa3,
0xad,0xbd,0xfe,0xea,0x5a,0xab,0x69,0xb3,
0xad,0x55,0xde,0xd8,0x2e,0x2b,0xb5,0x6a,
0x69,0x4b,0x3f,0xb4,0x9e,0x92,0xb5,0xed,
0xd5,0xca,0x9c,0xb4,0x5a,0xa1,0x2a,0x6d,
0xad,0x6c,0x5f,0xda,0x2c,0x91,0xbb,0xf6,
0xad,0xaa,0x96,0xaa,0x5a,0xca,0x9d,0xfe,
0x2c,0xa5,0x2a,0xd3,0x9a,0x8a,0x4f,0xfd,
0x2c,0x25,0x4a,0x6b,0x4d,0x45,0x9f,0xba,
0x1a,0xaa,0x7a,0xb5,0xaa,0x44,0x6b,0x5b,
0x1a,0x55,0xfd,0x5e,0x4e,0xa2,0x6b,0x59,
0x9a,0xa4,0xde,0x4a,0x4a,0xd2,0xf5,0xaa
};
"""
使用tuple数据来创建图像
bmp = BitmapImage(data = BITMAP)
Button(root,bitmap = bmp)
'''
'''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)
b3 = Button(root,text = 'Button3',command = cb3)
b1.pack()
b2.pack()
b3.pack()
b2.focus_set()
root.mainloop()
'''
上例中使用了bind方法,它建立事件与回调函数(响应函数)之间的关系,每当产生<Enter>事件
后,程序便自动的调用cb2,与cb1,cb3不同的是,它本身还带有一个参数----event,这个参数传递
响应事件的信息。
'''
from Tkinter import *
def printEventInfo(event):
print 'event.time = ' , event.time
print 'event.type = ' , event.type
print 'event.WidgetId = ', event.widget
print 'event.KeySymbol = ',event.keysym
root = Tk()
b = Button(root,text = 'Infomation')
b.bind("<Return>",printEventInfo)
b.pack()
b.focus_set()
root.mainloop() 
'''
犯了个错误,将<Return>写成<Enter>了,结果是:当鼠标进入Button区域后,事件printEventInfo
被调用。程序打印出了event的信息。
'''
Tkinter教程之Button篇(1)的更多相关文章
- Tkinter教程之Button篇(2)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811300 # Tkinter教程之Button篇(2)'''5.指定Button的宽度与高度 ...
- Tkinter教程之Event篇(3)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1823550 '''Tkinter教程之Event篇(3)''''''11.两个事件同时绑定到一 ...
- Tkinter教程之Event篇(2)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1823548 '''Tkinter教程之Event篇(2)''''''5.测试离开(Leave) ...
- Tkinter教程之Event篇(1)'
本文转载自:http://blog.csdn.net/jcodeer/article/details/1823544 ''Tkinter教程之Event篇(1)'''# 事件的使用方法'''1.测试鼠 ...
- Tkinter教程之Grid篇
本文转载自:http://blog.csdn.net/jcodeer/article/details/1813196 '''Tkinter教程之Grid篇'''# Tkinter参考中最推荐使用的一个 ...
- Tkinter教程之Canvas篇(4)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1812091 '''Tkinter教程之Canvas篇(4)''''''22.绘制弧形'''# ...
- Tkinter教程之Text篇(3)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811348 '''Tkinter教程之Text篇(3)''''''14.自定义tag的两个内置 ...
- Tkinter教程之Toplevel篇
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811341 '''Tkinter教程之Toplevel篇'''#TopLevel与Frame类 ...
- Tkinter教程之Radiobutton篇
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811308 #Tkinter教程之Radiobutton篇#Radiobutton为单选按钮, ...
随机推荐
- BSD历史
在UNIX问世之后(关于UNIX来历史这里不详述),UNIX拥有者AT&T公司以低廉甚至免费的许可将Unix源码授权给学术机构做研究或教学之用. 加州大学伯克利分校就是其中之一.在得到源码后, ...
- Git教程之管理修改(6)
Git比其他版本控制系统设计得优秀,因为Git跟踪并管理的是修改,而非文件.你会问,什么是修改?比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了 ...
- Java API —— 多线程(2)
1.JDK5中Lock锁的使用 虽然我们可以理解同步代码块和同步方法的锁对象问题,但是我们并没有直接看到在哪里加上了锁,在哪里释放了锁,为了更清晰的表达如何加锁和释放锁,JDK5以后提供了一个新的锁对 ...
- laravel加载javascript库
一篇文章: Generating a Link to a Javascript File Problem You want your Blade template to load an externa ...
- 用代码编辑数据库T-SQL语句
1.创建表 create table Car( Code varchar(50) primary key, Name varchar(50), Time date, Price float, Bran ...
- python判断文件目录是否存在
import os os.path.isfile('test.txt') # 如果不存在就返回False os.path.exists(directory) # 如果目录不存在就返回False o ...
- Android开发之三种动画
转载:http://www.cnblogs.com/angeldevil/archive/2011/12/02/2271096.html http://www.lightskystreet.com/2 ...
- Mybatis的if test字符串比较问题
1. Mybatis判断字符串是否为空的变态写法 <if test="bussSceneIsNull =='0'.toString() "> <![CDATA[ ...
- 【笨嘴拙舌WINDOWS】API
如今,相对于大行其道的对象,服务,API概念的提出要早很多,却依然经久不衰:所谓万变不离其宗,如今很多服务(Web Services,云服务)的提供方式和API如出一辙. Windows API(Ap ...
- iOS开发:本地数据存储-NSUserDefaults
Getting Default Values arrayForKey(_:) boolForKey(_:) dataForKey(_:) dictionaryForKey(_:) floatForKe ...