Tkinter教程.docx

上传人:b****6 文档编号:4789942 上传时间:2022-12-09 格式:DOCX 页数:87 大小:71.21KB
下载 相关 举报
Tkinter教程.docx_第1页
第1页 / 共87页
Tkinter教程.docx_第2页
第2页 / 共87页
Tkinter教程.docx_第3页
第3页 / 共87页
Tkinter教程.docx_第4页
第4页 / 共87页
Tkinter教程.docx_第5页
第5页 / 共87页
点击查看更多>>
下载资源
资源描述

Tkinter教程.docx

《Tkinter教程.docx》由会员分享,可在线阅读,更多相关《Tkinter教程.docx(87页珍藏版)》请在冰豆网上搜索。

Tkinter教程.docx

Tkinter教程

Tkinter教程

这系列教程完全以代码的形式来写的,目标是:

读者看代码和注释就可以理解代码的意思。

但这里的读者需要具备的几项技能:

1熟悉python语言的基础,如果还没有,先看一下python的教程吧,英文官方(docs.python.org/tut/tut.html);

2对界面编程有一定的了解,知道基本的概念就可以了;

3对Tk有兴趣,别以为她是已经过时的技术,如果丧失了学习的兴趣,那肯定无法完成了;

4不要以Ctrl+C/Ctrl+V的方式使用本教程(虽然它可以这样直接运行),自己输入,你会发现自己原来也会犯这样的错误;

5安装了python2.5且确认安装了Tkinter模块(默认就安装了,如果你没有强制的把它去掉的话),下载python2.5(.python.org/download/);

6如果在阅读教程中有不明白的,不要强迫自己,直接跳过去,继续下一个容。

Tkinter教程系列教程的特点:

7他不是一本经过文字润色的文章,全部是代码,作者在必要的时候使用注释来解释;

8以组件为章节进行介绍,每个组件又分为不同的例子,各个例子可以单独使用,分别使用序号标注;

9各个例子的使用“注释+序号”的格式表示开始,下一个例子的开始为上一个例子的结束;

10全部使用结构化编程(SP),没有面向对象的概念(OO);

11基本上包含了TKinter的所有的控件,根据每个控件的使用方法,选择性的介绍了其属性和方法,没有全部介绍,全部的介绍查看Tkinter的官方参考(.pythonware./library/tkinter/introduction/);

12例子不是百分百的完美,甚至有的例子没有按照Tkinter参考的描述完成,原因由于作者没有看懂:

13参考书籍:

.pythonware./library/tkinter/introduction/,如有冲突以Tkinter参考为准

最后祝各位Tk一路快乐!

Label

#Tkinter教程之Label篇

'''1.Label的第一个例子

text属性使用方法

'''

#要使用Tk模块,除非你不想使用这个模块,那整个教程就不需要看了

fromTkinterimport*

#初始化Tk

root=Tk()

#创建一个label,使用编码,到现在为止还没有使用过直接通过“drag-and-drop”就可以完成的IDE。

label=Label(root,text='HelloTkinter')

#显示label,必须含有此语句

label.pack()

#root.pack()

#但root是不需要(严格地说是必须不这样使用),否则解释器抱怨

#进入消息循环

root.mainloop()

#控件的显示步骤:

#1.创建这个控件

#2.指定这个空间的master,即这个控件属于哪一个

#3.告诉GM(geometrymanager)有一个控件产生了

'''

还有更简单的一个例子:

将‘HelloTkinter’打印到标题上,Label也不用创建了

fromTkinterimport*

root=Tk()

root.title('helloTkinter')

root.mainloop()

再没法儿简化了,就这样吧

'''

'''2.在label上使用置位图

bitmap的使用方法

'''

fromTkinterimport*

#初始化Tk

root=Tk()

#创建一个label,使用编码,到现在为止还没有使用过直接通过“drag-and-drop”就可以完成的IDE。

label=Label(root,bitmap='error')

#上面的代码使用了置位图error

#显示label,必须含有此语句

label.pack()

#进入消息循环

root.mainloop()

'''

其他可用的位图:

*error

*hourglass

*info

*questhead

*question

*warning

*gray12

*gray25

*gray50

*gray75

若要查看各自的效果,可以使用相应的名称将bitmpa='error'替换。

据说还可以使用自己指定的位图文件,网上找了一下,格式如下:

Label(root,bitmap="/path/bitmapname")

不过我试了一下,从来没有成功过,我已经将位图该为单色的了:

另:

还有的网上的文章说明如何使用PhotoImage和BitmapImage显示bmp或gif文件,提到一点

防止图像文件被python自动回收(garbagecollected),应将bmp或gif放到全局(global)或实体

(instance)中,使用如下两种方法,仍未奏效:

'''

#使用image属性

#bm=PhotoImage(file='c:

\\python.gif')

#label=Label(root,image=bm)

#label.bm=bm

#错误信息:

#TclError:

image"pyimageXX"doesn'texist

#使用bitmap属性

#bm=BitmapImage(file='c:

\\python2.bmp')

#label=Label(root,bitmap=bm)

#label.bm=bm

#label.pack()

#错误信息:

#TclError:

formaterrorinbitmapdata

'''

虽然二者均没有起作用,还是要说明一下,bitmap与image的关系,如果同时指定这两参数,image

优先。

'''

'''3.改变控件的前景色和背景色

fg:

前景色

bg:

背景色

设置背景色的一个大的用处是:

可以判断控件的大小(不同的控件使用不同的颜色,后续容

可以使用此特性来调试container)

'''

fromTkinterimport*

root=Tk()

#在创建Label时指定各自使用的颜色

'''可以使用的颜色值:

'''

#使用颜色名称

Label(root,fg='red',bg='blue',text='HelloIamTkinter').pack()

#使用颜色值#RRGGBB

Label(root,fg='red',bg='#FF00FF',text='HelloIamTkinter').pack()

#使用系统相关的颜色值(Windows),不建议使用这样的值,不利于平台移植

Label(root,fg='red',bg='SystemButtonShadow',text='HelloIamTkinter').pack()

root.mainloop()

'''

(1).使用颜色名称

Red

Green

Blue

Yellow

LightBlue

......

(2).使用#RRGGBB

label=Label(root,fg='red',bg='#FF00FF',text='HelloIamTkinter')

指定背景色为绯红色

(3).除此之外,Tk还支持与OS相关的颜色值,如Windows支持

SystemActiveBorder,

SystemActiveCaption,

SystemAppWorkspace,

SystemBackground,

......

'''

'''4.设置宽度与高度

width:

宽度

height:

高度

'''

fromTkinterimport*

root=Tk()

#创建三个Label,分别显示red,blue,yellow

#注意三个Label的大小,它们均与文本的长度有关

Label(root,text='red',bg='red').pack()

Label(root,text='blue',bg='blue').pack()

Label(root,text='yellow',bg='yellow').pack()

#再创建三个Label,与上次不同的是这三个Label均使用width和heigth属性

#三个Label的大小由width和height指定

Label(root,bg='red',width=10,height=3).pack()

Label(root,bg='blue',width=10,height=3).pack()

Label(root,bg='yellow',width=10,height=3).pack()

root.mainloop()

'''5.同时使用图像与文本

compound:

指定文本(text)与图像(bitmap/image)是如何在Label上显示,缺省为None,

当指定image/bitmap时,文本(text)将被覆盖,只显示图像了。

可以使用的值:

left:

图像居左

right:

图像居右

top:

图像居上

bottom:

图像居下

center:

文字覆盖在图像上

bitmap/image:

显示在Label上的图像

text:

显示在Label上的文本

label=Label(root,text='Error',compound='left',bitmap='error')

'''

fromTkinterimport*

root=Tk()

#演示compound的使用方法

#图像与文本在Label中的位置

#图像居下

Label(root,text='botton',compound='bottom',bitmap='error').pack()

#图像居上

Label(root,text='top',compound='top',bitmap='error').pack()

#图像居右

Label(root,text='right',compound='right',bitmap='error').pack()

#图像居左

Label(root,text='left',compound='left',bitmap='error').pack()

#文字覆盖在图像上

Label(root,text='center',compound='center',bitmap='error').pack()

#消息循环

root.mainloop()

'''6.文本的多行显示

在Tk004中,使用width和heigth来指定控件的大小,如果指定的大小无法满足文本的要,会出现

什么现象呢?

如下代码:

Label(root,bg='welcometo',width=10,height=3).pack()

运行程序,超出Label的那部分文本被截断了,常用的方法是:

使用自动换行功能,及当文本长度大于

控件的宽度时,文本应该换到下一行显示,Tk不会自动处理,但提供了属性:

wraplength:

指定多少单位后开始换行

justify:

指定多行的对齐方式

ahchor:

指定文本(text)或图像(bitmap/image)在Label中的显示位置

可用的值:

e

w

n

s

ne

se

sw

sn

center

布局如下图

nwnne

wcentere

swsse

'''

fromTkinterimport*

root=Tk()

#左对齐,文本居中

Label(root,text='welcometo',bg='yellow',width=40,height=3,wraplength=80,justify='left').pack()

#居中对齐,文本居左

Label(root,text='welcometo',bg='red',width=40,height=3,wraplength=80,anchor='w').pack()

#居中对齐,文本居右

Label(root,text='welcometo',bg='blue',width=40,height=3,wraplength=80,anchor='e').pack()

root.mainloop()

'''

运行一下程序就可以直观的看出,justify与anchor的区别了:

一个用于控制多行的对齐;另一个用于

控制整个文本块在Label中的位置

''

Button

(1)

#JTkinter教程之Button篇

(1)

#Button功能触发事件

'''1.一个简单的Button应用'''

fromTkinterimport*

#定义Button的回调函数

defhelloButton():

print'hellobutton'

root=Tk()

#通过command属性来指定Button的回调函数

Button(root,text='HelloButton',command=helloButton).pack()

root.mainloop()

'''

执行的结果:

每次点击一次,程序向标准输出打印'hellobutton',以上为Button使用方法,可以

再做一下简化,如不设置Button的回调函数,这样也是允许的但这样的结果与Label没有什么太

大的区别,只是外观看起来有所不同罢了,失去了Button的作用。

fromTkinterimport*

root=Tk()

#下面的relief=FLAT设置,就是一个Label了!

Button(root,text='hellobutton',relief=FLAT).pack()

root.mainloop()

'''

'''2.测试Button的relief属性'''

#运行下面的代码可以看到Button的各个不同效果,均没有回调函数。

fromTkinterimport*

root=Tk()

#flat,groove,raised,ridge,solid,orsunken

Button(root,text='hellobutton',relief=FLAT).pack()

Button(root,text='hellobutton',relief=GROOVE).pack()

Button(root,text='hellobutton',relief=RAISED).pack()

Button(root,text='hellobutton',relief=RIDGE).pack()

Button(root,text='hellobutton',relief=SOLID).pack()

Button(root,text='hellobutton',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下的X11bitmap编辑器生成的bitmap还没有测试,但可

以使用置的位图。

(1).使用位图文件

bp=BitmapImage(file="c:

\\python2.xbm")

Button(root,bitmap=bp).pack()

(2).使用位图数据

BITMAP="""

#defineim_width32

#defineim_height32

staticcharim_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'''

fromTkinterimport*

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”,判断

程序的打印结果

'''

fromTkinterimport*

defcb1():

print'button1clicked'

defcb2(event):

print'button2clicked'

defcb3():

print'button3clicked'

root=Tk()

b1=Button(root,text='Button1',command=cb1)

b2=Button(root,text='Button2')

b2.bind("",cb2)

b3=Button(root,text='Button3',command=cb3)

b1.pack()

b2.pack()

b3.pack()

b2.focus_set()

root.mainloop()

'''

上例中使用了bind方法,它建立事件与回调函数(响应函数)之间的关系,每当产生事件

后,程序便自动的调用cb2,与cb1,cb3不同的是,它本身还带有一个参数----event,这个参数传递

响应事件的信息。

'''

fromTkinterimport*

defprintEventInfo(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("",printEventInfo)

b.pack()

b.focus_set()

root.mainloop()

'''

犯了个错误,将写成了,结果是:

当鼠标进入Button区域后,事件printEventInfo

被调用。

程序打印出了event的信息。

'''

Button

(2)

#Tkinter教程之Button篇

(2)

'''5.指定Button的宽度与高度

width:

宽度

heigth:

高度

使用三种方式:

1.创建Button对象时,指定宽度与高度

2.使用属性width和height来指定宽度与高度

3.使用configure方法来指定宽度与高度

'''

fromTkinterimport*

root=Tk()

b1=Button(root,text='30X1',width=30,height=2)

b1.pack()

b2=Button(root,text='30X2')

b2['width']=30

b2['height']=3

b2.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,就是地图上的标识位置了,使用

wi

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 高考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1