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()
#如果看的不习惯,就使用下面的代码。
# Button(root,text = 'anchor',width = 30,height =4).pack()
# Button(root,text = 'anchor',anchor = 'center',width = 30,height =4).pack() # Button(root,text = 'anchor',anchor = 'n',width = 30,height = 4).pack() # Button(root,text = 'anchor',anchor = 's',width = 30,height = 4).pack() # Button(root,text = 'anchor',anchor = 'e',width = 30,height = 4).pack() # Button(root,text = 'anchor',anchor = 'w',width = 30,height = 4).pack() # Button(root,text = 'anchor',anchor = 'ne',width = 30,height = 4).pack() # Button(root,text = 'anchor',anchor = 'nw',width = 30,height = 4).pack() # Button(root,text = 'anchor',anchor = 'se',width = 30,height = 4).pack() # Button(root,text = 'anchor',anchor = 'sw',width = 30,height = 4).pack()
root.mainloop()
'''7.改变Button的前景色与背景色 fg: 前景色 bg:背景色 '''
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,8 from Tkinter import * root = Tk()
for b in [0,1,2,3,4]: Button(root,
text = string(b), bd = b).pack() root.mainloop()
'''9.设置Button的风格
relief/raised/sunken/groove/ridge '''
from Tkinter import * root = Tk()
for r in ['raised','sunken','groove','ridge']: Button(root, text = r, relief = r,
width = 30).pack() root.mainloop()
'''10.设置Button状态 normal/active/disabled '''
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属性
'''11.绑定Button与变量
设置Button在textvariable属性 '''
from Tkinter import *
root = Tk()
def changeText():
if b['text'] == 'text': v.set('change') print 'change' else:
v.set('text') print 'text' v = StringVar()
b = Button(root,textvariable = v,command = changeText) v.set('text') b.pack()
root.mainloop() '''
将变量v与Button绑定,当v值变化时,Button显示的文本也随之变化 '''
Entry
#Tkinter教程之Entry篇 #Entry用来输入单行文本 '''1.第一个Entry程序''' from Tkinter import * root = Tk()
Entry(root,text = 'input your text here').pack() root.mainloop()
#上面的代码目的是创建一个Entry对象,并在Entry上显示'input your text here',运行此代码,并没有看到文本的显示,由此可知与Lable和Button不同,Entry的text属性不可以设置Entry的文本
'''2.在Entry中设定初始值,使用textvariable将变量与Entry绑定''' from Tkinter import *
root = Tk() e = StringVar()
entry = Entry(root,textvariable = e) e.set('input your text here') entry.pack() root.mainloop()
#上面的例子中将变量e与Entry绑定,然后将e的值设置为'input your text here',程序运行时的初始值便设置了。
'''3.设置为只读Entry.
Entry的另一个比较有用的属性,设置为只读,不允许用户对它的值改变。 设置state属性为'readonly' '''
from Tkinter import * root = Tk() e = StringVar()
entry = Entry(root,textvariable = e) e.set('input your text here') entry.pack()
entry['state'] = 'readonly' root.mainloop()
#实际上Entry的属性值可以使用的也为normal/active/disabled,'readonly'与disabled一样
'''4.设置为密码输入框
#将Entry作为一个密码输入框来使用,即不显示用户输入的内容值,用特定符号代替。使用用属性
show来指定。 '''
from Tkinter import * root = Tk() e = StringVar()
entry = Entry(root,textvariable = e) e.set('input your text here') entry.pack()
#使用*来显示输入的内容,如果喜欢可以改为其它字符 entry['show'] = '*'
#分别使用*#$显示输入的文本内容 for mask in ['*','#','$']: e = StringVar()
entry = Entry(root,textvariable = e) e.set('password') entry.pack()
entry['show'] = mask
root.mainloop()
'''5.验证输入的内容是否符合要求。 使用validate来校验输入的内容 使用validate方法来限制输入的内容
这是一个有问题的例子,无法调用validateText回调函数 ‘'''
from Tkinter import * root = Tk() e = StringVar()
def validateText(contents): print contents
return contents.isalnum()
entry = Entry(root,validate = 'key',textvariable = e,validatecommand = validateText) entry.pack()
root.mainloop() '''
文档中说明使用validate来接受的事件,使用validatecommand来确定输入的内容是否合法,但
如何传入参数?没找到相应的说明 '''
#还有其他的属性fg/bg/relief/width/height/justify/state使用方法与Button相同,不再举例。
CheckButton
#Tkinter教程之Checkbutton篇
#Checkbutton又称为多选按钮,可以表示两种状态:On和Off,可以设置回调函数,每当点击此按钮时回调函数被调用
'''1.一个简单的Checkbutton例子'''
#创建一个Checkbutton,显示文本为\ from Tkinter import * root = Tk()
Checkbutton(root,text = 'python').pack() root.mainloop()
'''2.设置Checkbutton的回调函数'''
from Tkinter import * def callCheckbutton():
print 'you check this button' root = Tk()
Checkbutton(root,text = 'check python',command = callCheckbutton).pack() root.mainloop()
#不管Checkbutton的状态如何,此回调函数都会被调用
'''3.通过回调函数改变Checkbutton的显示文本text的值''' from Tkinter import *