1、tags用法学习
from tkinter import *
root=Tk()
text=Text(root,width=30,height=5)
text.pack()
text.insert(INSERT,“从前车马很慢,一生只爱一人”)
text.tag_add(“tag1”,“1.7”,“1.12”,“1.14”)
text.tag_config(“tag1”,background=“yellow”,foreground=“red”)
mainloop()
结果图:
颜色修改:
from tkinter import *
root=Tk()
text=Text(root,width=30,height=5)
text.pack()
text.insert(INSERT,“从前车马很慢,一生只爱一人”)
text.tag_add(“tag1”,“1.7”,“1.12”,“1.14”)
text.tag_add(“tag2”,“1.7”,“1.12”,“1.14”)
text.tag_config(“tag1”,background=“yellow”,foreground=“red”)
text.tag_config(“tag2”,foreground=“blue”)
mainloop()
(1)打开网页
text.pack()
text.insert(INSERT,“I love FishC.com!”)
text.tag_add(“link”,“1.7”,“1.16”)
text.tag_config(“link”,foreground=“blue”,underline=True)
def show_arrow_cursor(event):
text.config(cursor=“arrow”)
def show_xterm_cursor(event):
text.config(cursor=“xterm”)
def click(event):
webbrowser.open(“http://www.fishc.com”)
text.tag_bind(“link”,"",show_arrow_cursor)
text.tag_bind(“link”,"",show_xterm_cursor)
text.tag_bind(“link”,"",click)
mainloop()
(2)检查
from tkinter import *
import hashlib
root=Tk()
text=Text(root,width=30,height=5)
text.pack()
text.insert(INSERT,“I love FishC.com!”)
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()
(3)查找
from tkinter import *
import hashlib
root=Tk()
text=Text(root,width=30,height=5)
text.pack()
text.insert(INSERT,“I love FishC.com!”)
def getIndex(text,index):
return tuple(map(int,str.split(text.index(index),".")))
start=“1.0”
while True:
pos=text.search(“o”,start,stopindex=END)
if not pos:
break
print(“找到了,位置是:”,getIndex(text,pos))
start=pos+"+1c"
mainloop()
(4)恢复与撤销
from tkinter import *
root=Tk()
text=Text(root,width=30,height=5,undo=True)
text.pack()
text.insert(INSERT,“I love FishC.com!”)
def show():
text.edit_undo()
Button(root,text=“撤销”,command=show).pack()
mainloop()
(5)逐个撤销
from tkinter import *
root=Tk()
text=Text(root,width=30,height=5,undo=True,autoseparators=False)
text.pack()
text.insert(INSERT,“I love FishC.com!”)
def callback(event):
text.edit_separator()
text.bind(’’,callback)
def show():
text.edit_undo()
Button(root,text=“撤销”,command=show).pack()
mainloop()
2、Canvas用法
(1)画图
from tkinter import *
root=Tk()
w=Canvas(root,width=200,height=100)
w.pack()
w.create_line(0,50,200,50,fill=“yellow”)
w.create_line(100,0,100,100,fill=“red”,dash=(4,4))
w.create_rectangle(50,25,150,75,fill=“blue”)
mainloop()
(2)删除
from tkinter import *
root=Tk()
w=Canvas(root,width=200,height=100)
w.pack()
line1=w.create_line(0,50,200,50,fill=“yellow”)
line2=w.create_line(100,0,100,100,fill=“red”,dash=(4,4))
rect1=w.create_rectangle(50,25,150,75,fill=“blue”)
w.coords(line1,0,25,200,25)
w.itemconfig(rect1,fill=“red”)
w.delete(line2)
Button(root,text=“删除全部”,command=(lambda x=ALL:w.delete(x))).pack()
mainloop()
(3)
from tkinter import *
root=Tk()
w=Canvas(root,width=200,height=100)
w.pack()
w.create_line(0,0,200,100,fill=“green”,width=3)
w.create_line(200,0,0,100,fill=“green”,width=3)
w.create_rectangle(40,20,160,80,fill=“green”)
w.create_rectangle(65,35,135,65,fill=“yellow”)
w.create_text(100,50,text=“我爱你”)
mainloop()
(4)画椭圆
from tkinter import *
root=Tk()
w=Canvas(root,width=200,height=100)
w.pack()
w.create_rectangle(40,20,160,80,dash=(4,4))
w.create_oval(40,20,160,80,fill=“pink”)
w.create_text(100,50,text=“我爱你”)
mainloop()
(5)画五角星
from tkinter import *
import math as m
root=Tk()
w=Canvas(root,width=200,height=100)
w.pack()
center_x=100
center_y=50
r=50
points=[
#左上点
center_x-int(rm.sin(2 m.pi/5)),
center_y-int(rm.cos(2m.pi/5)),
#右上点
center_x+int(rm.sin(2 m.pi/5)),
center_y-int(rm.cos(2m.pi/5)),
#左下角
center_x-int(rm.sin(m.pi/5)),
center_y+int(rm.cos(m.pi/5)),
#定点
center_x,
center_y-r,
#右下角
center_x+int(rm.sin(m.pi/5)),
center_y+int(rm.cos(m.pi/5))
]
w.create_polygon(points,outline="",fill=“red”)
mainloop()
#fill=""