<GUI>Tkinter
1.初识Tkinter(python的图形化界面)
关键字:pack,command,bg,fg
import tkinter as tk class APP: def __init__(self, master): frame = tk.Frame(master) frame.pack(side=tk.BOTTOM, padx=15, pady=15) # 自动调整位置 LEFT/TOP/RIGHT/BOTTOM self.hi_there = tk.Button(frame, text="打招呼", fg='white', bg='red', command=self.say_hi) # 前景色fg,背景色bg self.hi_there.pack() def say_hi(self): print("hi hello!!!") root = tk.Tk() app = APP(root) # 实例化 root.mainloop()
结果
2.lable和Button
关键字:显示图片,文字换行,左对齐,文字图片同一行
from tkinter import * root = Tk() textlable = Label(root, text="第二个DEMO\n这里是第二行", justify=LEFT, # 左对齐 padx=10, bg='blue', fg='white') textlable.pack(side=LEFT) photo = PhotoImage(file='1.gif') imgLable = Label(root, image=photo) imgLable.pack(side=RIGHT) mainloop()
结果:
========================================================================================
关键字:图片背景,文字在图片上显示
from tkinter import * root = Tk() photo = PhotoImage(file='1.gif') thelable = Label(root, text="第三个DEMO\n这里是第二行", justify=LEFT, # 左对齐 image=photo, compound=CENTER, # 文字在图片正上方显示 font=("宋体",20), fg="black") thelable.pack() mainloop()
结果
======================================================================================================
from tkinter import * def callback(): var.set("金甲战士,张益达参见!") root = Tk() frame1 = Frame(root) frame2 = Frame(root) var = StringVar() var.set("第四个DEMO\n这里是第二行\n没错,第三行") photo = PhotoImage(file='1.gif') thelable = Label(frame1, textvariable=var, justify=LEFT, # 左对齐 image=photo, compound=CENTER, # 文字在图片正上方显示 font=("宋体", 20), fg="black") thelable.pack() thebutton = Button(frame2, text="斯内克,变身!", command=callback) thebutton.pack() frame1.pack() frame2.pack() mainloop()
结果:
3.Checkbutton、Radiobutton和LabelFrame
选择按钮
from tkinter import * root = Tk() v = IntVar() c = Checkbutton(root, text="测试一下", variable=v) c.pack() l = Label(root, textvariable=v) l.pack() mainloop()
结果:
===============================================================================================================
翻牌子程序
from tkinter import * root = Tk() GIRLS = ["妮可", "雅典娜", "小滴", "晴子", "天使彦"] v = [] for girl in GIRLS: v.append(IntVar()) b = Checkbutton(root, text=girl, variabl=v[-1]) b.pack(anchor=W) # 地理上的W 西边 mainloop()
结果:
=================================================================================================================================
单选框(基础版)
from tkinter import * root = Tk() v = IntVar() Radiobutton(root, text="one", variable=v, value=1).pack(anchor=W) Radiobutton(root, text="twp", variable=v, value=2).pack(anchor=W) Radiobutton(root, text="three", variable=v, value=3).pack(anchor=W) Radiobutton(root, text="four", variable=v, value=4).pack(anchor=W) Radiobutton(root, text="five", variable=v, value=5).pack(anchor=W) mainloop()
结果:
==========================================================================
单选框(使用循环)
from tkinter import * root = Tk() LANGS = [("python",1), ("perl",2), ("Ruby",3), ("java",4)] v = IntVar() v.set(1) for lang,num in LANGS: b = Radiobutton(root,text=lang,variable=v,value=num) b.pack(anchor=W) mainloop()
结果:
======================================================================
单选框(去圆点)
from tkinter import * root = Tk() LANGS = [("python", 1), ("perl", 2), ("Ruby", 3), ("java", 4)] v = IntVar() v.set(1) for lang, num in LANGS: b = Radiobutton(root, text=lang, variable=v, value=num, indicatoron=False) b.pack(fill=X) # 横向填充,Y为纵向填充 mainloop()
结果:
========================================================================================
使用labelFrame分组
from tkinter import * root = Tk() group = LabelFrame(root, text="最好的编程语言是?", padx=5, pady=5) group.pack(padx=10, pady=10) LANGS = [("python", 1), ("perl", 2), ("Ruby", 3), ("java", 4)] v = IntVar() v.set(1) for lang, num in LANGS: b = Radiobutton(group, text=lang, variable=v, value=num, indicatoron=False) b.pack(fill=X) # 横向填充,Y为纵向填充 mainloop()
结果:
========================================================
简单在修改后
from tkinter import * root = Tk() group = LabelFrame(root, text="最好的编程语言是?", padx=5, pady=5) group.pack(padx=10, pady=10) LANGS = [("python", 1), ("perl", 2), ("Ruby", 3), ("java", 4)] v = IntVar() v.set(1) for lang, num in LANGS: b = Radiobutton(group, text=lang, variable=v, value=num) b.pack(anchor=W) mainloop()
结果:
4.Entry输入框
from tkinter import * root = Tk() Label(root, text="作品:").grid(row=0, column=0) Label(root, text="作者:").grid(row=1, column=0) e1 = Entry(root) e2 = 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("作品:《%s》" % e1.get()) # 获取输入框的内容 print("作者:《%s》" % e2.get()) # 获取输入框的内容 pass Button(root, text="获取信息", width=10, command=show) \ .grid(row=3, column=0, sticky=W, padx=10, pady=5) Button(root, text="退出", width=10, command=root.quit) \ .grid(row=3, column=1, sticky=E, padx=10, pady=5) mainloop()
结果:
==================================================================================
账号密码输入框
from tkinter import * root = Tk() Label(root, text="账号:").grid(row=0, column=0) Label(root, text="密码:").grid(row=1, column=0) v1 = StringVar() # 存放账号在变量 v2 = StringVar() # 存放密码在变量 e1 = Entry(root, textvariable=v1) e2 = Entry(root, textvariable=v2,show="*") e1.grid(row=0, column=1, padx=10, pady=5) e2.grid(row=1, column=1, padx=10, pady=5) def show(): print("账号:《%s》" % e1.get()) # 获取输入框的内容 print("密码:《%s》" % e2.get()) # 获取输入框的内容 pass Button(root, text="登录", width=10, command=show) \ .grid(row=3, column=0, sticky=W, padx=10, pady=5) Button(root, text="退出", width=10, command=root.quit) \ .grid(row=3, column=1, sticky=E, padx=10, pady=5) mainloop()
结果:
=========================================================
焦点移出的时候验证
from tkinter import * root = Tk() def test(): if e1.get() == "西游记": print("正确") return True else: print("错误") e1.delete(0, END) # 清空输入框 return False v1 = StringVar() e1 = Entry(root, textvariable=v1, validate="focusout", validatecommand=test) e2 = Entry(root) e1.grid(padx=10, pady=5) e2.grid(padx=10, pady=5) def show(): print("账号:《%s》" % e1.get()) # 获取输入框的内容 print("密码:《%s》" % e2.get()) # 获取输入框的内容 pass Button(root, text="登录", width=10, command=show) \ .grid(row=3, column=0, sticky=W, padx=10, pady=5) Button(root, text="退出", width=10, command=root.quit) \ .grid(row=3, column=1, sticky=E, padx=10, pady=5) mainloop()
结果:
5.Listbox、Scrollbar
删除选定在元素
from tkinter import * root = Tk() theLB = Listbox(root) theLB.pack() # theLB.insert(0, "猪") # theLB.insert(END, "猪2") for item in ["1", "2", "3", "4", "5"]: theLB.insert(END, item) theButton = Button(root, text="删除", command=lambda x=theLB: x.delete(ACTIVE)) # 传参数 theButton.pack() mainloop()
结果:
===================================================
滚动条
from tkinter import * root = Tk() # 滚动条 sb = Scrollbar(root) sb.pack(side=RIGHT, fill=Y) # 靠右边,Y轴填充 lb = Listbox(root, yscrollcommand=sb.set) #里面滚动,调用set方法 for item in range(1000): lb.insert(END, item) lb.pack(side=LEFT, fill=BOTH) sb.config(command=lb.yview) # 滚动条滚动后的显示效果 mainloop()
结果:
==================================================================
获取显示进度条位置
from tkinter import * root = Tk() s1 = Scale(root, from_=0, to=100) s1.pack() s2 = Scale(root, from_=0, to=100, orient=HORIZONTAL) s2.pack() # 水平 def show(): print(s1.get(), s2.get()) Button(root, text="获取位置", command=show).pack() mainloop()
结果:
6.Text、mark、Tags
可以插入文本、按钮、图片
from tkinter import * root = Tk() text = Text(root, width=90, height=30) text.pack() photo = PhotoImage(file="001.gif") def show(): # 插入图片 text.image_create(END, image=photo) # 插入文本 text.insert(INSERT, "女儿国\n") text.insert(END, "波雅汉库克\n") # 插入按钮 b1 = Button(text, text="点我", command=show) text.window_create(INSERT, window=b1) mainloop()
结果
================================================================================
索引
from tkinter import * root = Tk() text = Text(root, width=20, height=6) text.pack() # 插入文本 text.insert(INSERT, "女儿国国王\n") text.insert(END, "波雅汉库克\n") # 设置标记,在标记位置插入 text.mark_set("here", 2.0) # 2行1列 text.insert("here", "九蛇岛岛主\n") # mark标记的偏向(标记左边字符),默认记住标记右边一个字符 # text.mark_gravity("here",LEFT) mainloop()
结果:
=================================================================
tag标签修改
from tkinter import * # 用于打开网页 import webbrowser root = Tk() text = Text(root, width=20, height=6) text.pack() # 插入文本 text.insert(INSERT, "女儿国国王!!!\n") text.insert(END, "波雅汉库克\n") # 设置tags_add设置位置,tag_config设置前景色和背景色 text.tag_add("tag1", 1.0, 1.5, 1.7) # 1行1-7,1.9 text.tag_config("tag1", background="yellow", foreground="red") # 绑定事件 def show_hand_cursor(event): text.config(cursor="arrow") def show_xterm_cursor(event): text.config(cursor="xterm") def click(event): webbrowser.open( "https://baike.baidu.com/item/%E6%B3%A2%E9%9B%85%C2%B7%E6%B1%89%E5%BA%93%E5%85%8B/6305958?fr=aladdin") text.tag_add("link", 2.0, 2.5) text.tag_config("link", foreground="blue", underline=True) # 鼠标移入和离开 text.tag_bind("link", "<Enter>", show_hand_cursor) text.tag_bind("link", "<Leave>", show_xterm_cursor) # 鼠标点击事件 text.tag_bind("link", "<Button-1>", click) mainloop()
结果:
=====================================================
验证前后文本的MD5
from tkinter import * import hashlib root = Tk() text = Text(root, width=20, height=6) text.pack() # 插入文本 text.insert(INSERT, "女儿国国王!!!\n") text.insert(END, "波雅汉库克\n") contents = text.get("1.0", END) def getsig(contents): m = hashlib.md5(contents.encode()) return m.digest() sig = getsig(contents) def check(): contents = text.get("1.0", END) if sig != getsig(contents): print("该文本经过修改!") else: print("匹配成功") Button(root, text="检查", command=check).pack() mainloop()
结果:
==========================================
全文搜索