1.Entry输入框隐藏密码
#coding=UTF-8
import Tkinter as tk
import sys
reload(sys)
sys.setdefaultencoding('utf-8') #解决编码问题
root = tk.Tk()
label = tk.Label(root,text="账号")
label.grid(row=0)
label2 = tk.Label(root,text="密码")
label2.grid(row=1)
v1 = tk.StringVar()
v2 = tk.StringVar()
e1 = tk.Entry(root,textvariable=v1)
e1.grid(row=0,column=1,padx=10,pady=5)
e2 = tk.Entry(root,textvariable=v2,show="*")
e2.grid(row=1,column=1,padx=10,pady=5)
def show():
print "账号是:{}".format(e1.get())
print "密码是:{}".format(e2.get())
e1.delete(0,tk.END)
e2.delete(0,tk.END)
b1 = tk.Button(root,text="获取信息",width=10,command=show,font=("仿宋",10))
b1.grid(row=2,column=0,padx=10,pady=5,sticky=tk.W)
b2 = tk.Button(root,text="退出",width=10,command=root.quit,font=("仿宋",10))
b2.grid(row=2,column=1,padx=10,pady=5,sticky=tk.E)
root.mainloop()
#以下是运行结果
2.Entry验证输入框内容
#coding=UTF-8
import Tkinter as tk
import sys
reload(sys)
sys.setdefaultencoding('utf-8') #解决编码问题
root = tk.Tk()
def test():
if e1.get() == "特朗普":
print "正确"
return True
else:
print "错误"
e1.delete(0,tk.END)
return False
label = tk.Label(root,text="名字")
label.grid(row=0,sticky=tk.W,column=0)
label2 = tk.Label(root,text="密码")
label2.grid(row=1,sticky=tk.W,column=0)
v = tk.StringVar()
e1 = tk.Entry(root,textvariable=v,validate="focusout",validatecommand=test)
e1.grid(row=0,sticky=tk.E,column=1)
e2 = tk.Entry(root)
root.mainloop()
#以下是运行结果
3.Entry验证额外选项
#coding=UTF-8
import Tkinter as tk
import sys
reload(sys)
sys.setdefaultencoding('utf-8') #解决编码问题
root = tk.Tk()
v = tk.StringVar()
def test(content,reason,name):
if content == "特朗普":
print "正确"
print content,reason,name
return True
else:
print "不正确"
print content,reason,name
return False
testcmd = root.register(test)
e1 = tk.Entry(root,textvariable=v,validate="focusout",validatecommand=(testcmd,'%P','%v','%W'))# %P对应testcmd函数参数的content,%v对应testcmd函数的reason,%W对应testcmd函数的name
e1.grid(row=0,sticky=tk.W)
w2 = tk.Entry(root)
w2.grid(row=1,sticky=tk.W)
root.mainloop()
#以下是运行结果
4.listbox列表框
#coding=UTF-8
import Tkinter as tk
import sys
reload(sys)
sys.setdefaultencoding('utf-8') #解决编码问题
root = tk.Tk()
thelb = tk.Listbox(root,setgrid=True)
thelb.pack()
for item in ["鸡蛋","花生","玉米","李狗蛋"]:
thelb.insert(tk.END,item)
b = tk.Button(root,text="删除",command=lambda x = thelb: x.delete(tk.ACTIVE))
b.pack()
root.mainloop()
#以下是运行结果
5.text通过MD5值检测内容是否发生变化
#coding=UTF-8
import Tkinter as tk
import sys
import hashlib
reload(sys)
sys.setdefaultencoding('utf-8') #解决编码问题
root = tk.Tk()
text = tk.Text(root,width=30,height=5)
text.pack()
text.insert(tk.INSERT," i love you")
contents = text.get(1.0,tk.END)
def getSig(contents):
m = hashlib.md5(contents.encode('utf-8')) #开始加密
return m.digest() #返回加密值
sig = getSig(contents)
def check():
contents = text.get(1.0,tk.END)
if sig != getSig(contents):
print "内容发生了变化"
else:
print "无变化"
b = tk.Button(root,text="检查",command=check)
b.pack()
root.mainloop()
#以下是运行结果,点击检查按钮会在pycharm的输出框里显示是否内容有变化
6.Entry获取输入框的内容
# coding=UTF-8
import Tkinter as tk
def prin():
user = e.get()
print user
root = tk.Tk()
e = tk.Entry(root)
e.pack(padx=20,pady=20)
b = tk.Button(root,text="获取信息",command=prin)
b.pack()
root.mainloop()
#以下是运行结果
7.Entry获取信息-退出输入框
# coding=UTF-8
import Tkinter as tk
import sys
reload(sys)
sys.setdefaultencoding('utf-8') #解决编码问题
root = tk.Tk()
label = tk.Label(root,text="作品:")
label.grid(row=0)
label2 = tk.Label(root,text="作者:")
label2.grid(row=1)
e1 = tk.Entry(root)
e2 = tk.Entry(root)
e1.grid(row=0,column=1,padx=10,pady=5)
e2.grid(row=1,column=1,padx=10,pady=5)
def show():
print "作品:{}".format(e1.get())
print "作者:{}".format(e2.get())
e1.delete(0,tk.END)
e2.delete(0,tk.END)
b1 = tk.Button(root,text="获取信息",width=10,command=show)
b1.grid(row=3,column=0,stick=tk.W,padx=10,pady=5)
b2 = tk.Button(root,text="退出",width=10,command=root.quit)
b2.grid(row=3,column=1,sticky=tk.E,padx=10,pady=5)
root.mainloop()
#以下是运行结果
8.text文本撤销
#coding=UTF-8
import Tkinter as tk
import sys
reload(sys)
sys.setdefaultencoding('utf-8') #解决编码问题
root = tk.Tk()
text = tk.Text(root,width=30,height=5,undo=True)
text.pack()
text.insert(tk.INSERT," i love you")
def show():
text.edit_undo()
b = tk.Button(root,text="撤销",command=show)
b.pack()
root.mainloop()
#以下是运行结果
9.Text撤销单个字符
#encoding=UTF-8
import Tkinter as tk
import sys
reload(sys)
sys.setdefaultencoding('utf-8') #解决编码问题
root = tk.Tk()
text = tk.Text(root,width=30,height=5,undo=True,autoseparators=False,maxundo=10)
text.pack()
def callback(event):
text.edit_separator() #插入记号
text.bind('<Key>',callback) #只要按键被按下,就插入记号
text.insert(tk.INSERT,"i love you")
def show():
try:
x = text.get("1.0",tk.END)
if len(x)-1 == 1:
return
else:
text.edit_undo()
except Exception:
return
b = tk.Button(root,text="撤销",command=show)
b.pack()
root.mainloop()
#以下是运行结果
10.Text插入windows组件
#coding=UTF-8
import Tkinter as tk
import sys
reload(sys)
sys.setdefaultencoding('utf-8') #解决编码问题
root = tk.Tk()
text = tk.Text(root,width=30,height=5)
text.pack()
text.insert(tk.INSERT,"I love\n\t\t\t\t")
def show():
print "呦,我被点击了一下~"
b1 = tk.Button(root,text="点我点我",command=show,fg="blue")
text.window_create(tk.INSERT,window=b1)
root.mainloop()
#以下是运行结果