#最简单的输入框
import tkinter as tk root = tk.Tk() e = tk.Entry(root)#输入框的类
e.pack() e.delete(0,tk.END)#把输入框的内容清空
e.insert(0,"默认文本..")#默认内容 tk.mainloop()
import tkinter as tk #输入框
root = tk.Tk() tk.Label(root,text="网址: ").grid(row=0,column=0)#位于第0行,第0列
tk.Label(root,text="作者: ").grid(row=1,column=0)#位于第1行,第0列 e1 = tk.Entry(root)
e2 = tk.Entry(root)
e1.grid(row=0,column=1,padx=10,pady=5)#位于第0行,第1列
e2.grid(row=1,column=1,padx=10,pady=5)#位于第1行,第1列 def show():
print("作品:%s" %e1.get())
print("作者:%s" %e2.get()) tk.Button(root,text="获取信息",width=10,command=show)\
.grid(row=2,column=0,sticky = tk.W)
tk.Button(root,text="退出",width=10,command=root.quit)\
.grid(row=2,column=1,sticky = tk.E) tk.mainloop()
#密码输入框 import tkinter as tk root = tk.Tk() tk.Label(root,text="账号: ").grid(row=0,column=0)#位于第0行,第0列
tk.Label(root,text="密码: ").grid(row=1,column=0)#位于第1行,第0列 v1 = tk.StringVar()
v2 = tk.StringVar() e1 = tk.Entry(root,textvariable=v1)
e2 = tk.Entry(root,textvariable = v2,show="*")#show:以*显示 e1.grid(row=0,column=1,padx=10,pady=5)#位于第0行,第1列
e2.grid(row=1,column=1,padx=10,pady=5)#位于第1行,第1列 def show():
print("账号:%s" %e1.get())
print("密码:%s" %e2.get()) tk.Button(root,text="获取信息",width=10,command=show)\
.grid(row=2,column=0,sticky = tk.W)
tk.Button(root,text="退出",width=10,command=root.quit)\
.grid(row=2,column=1,sticky = tk.E) tk.mainloop()
import tkinter as tk root = tk.Tk() def test():
if e1.get()=="我很帅":
print("正确")
return True
else:
print("错误")
e1.delete(0,tk.END)#清空列表
return False def test2():
print("我只有在错误的情况下才会调用")
return True v = tk.StringVar() #validate 中的focusout是当移开光标的时候,触发test进行校对, validatecommand 校对函数
e1 = tk.Entry(root,textvariable=v,validate="focusout",validatecommand=test,\
invalidcommand=test2) e2 = tk.Entry(root)
e1.pack(padx=10)
e2.pack(padx=10) tk.mainloop()
import tkinter as tk root = tk.Tk() v1 = tk.StringVar()
v2 = tk.StringVar()
v3 = tk.StringVar() def test(content):
return content.isdigit()#数字会返回true 非数字返回false testCMD = root.register(test)
#validate触发方法是关键字触发
e1 = tk.Entry(root,width=10,textvariable=v1,validate='key',\
validatecommand = (testCMD,"%P")).grid(row=0,column=0) tk.Label(root,text="+").grid(row=0,column=1)#存放+号的 e2 = tk.Entry(root,width=10,textvariable=v2,validate='key',\
validatecommand = (testCMD,"%P")).grid(row=0,column=2) tk.Label(root,text="=").grid(row=0,column=3)#存放=号的 e3 = tk.Entry(root,textvariable=v3,state="readonly").grid(row=0,column=4)#只能读取 def calc():
result = int(v1.get())+int(v2.get())
v3.set(str(result)) tk.Button(root,text="计算结果",command=calc).grid(row=1,column=2) tk.mainloop()