学习python的第九天笔记

46、067
Text用于显示多行文本
text.get("1.2","1.end")#1.2表示第一行第三列,1.end表示第一行的末尾,行是从第一行开始算起,列是从第零列算起。
INSERT#表示插入光标的位置
CURRENT#表示对应于鼠标坐标最接近的位置。
END#表示对应的Text组件的文本缓冲区最后一个字符的下一个位置。
Marks(标记)
text.mark_set("here","1.2")#here表示标记,但不显示。1.2表示插入的位置,mark会记住1.2是什么字符,标记的位置会随着字符的改变而变化。
text.mark_unset("here")#表示删除here这个标记
text.mark_gravity("here",LEFT)#表示改变插入的位置,默认是插入左边,现在改为插入右边,这样标记就会记住前一个字符是什么,也就是一直在1.2的位置上。

47、068
Text(文本)
tags(标签)#用于改变文本的字体,颜色
text.tag_add("tag1","1.7","1.12","1.14")#用于添加标签,tag1是标签的名字,后面的是范围要标签的范围。
text.tag_config("tag1",background="yellow",foreground="red")#用于设置标记字体的背景颜色和字体颜色。如果重复使用config而且里面的内容相同的话会覆盖前的内容。
例1:
from tkinter import *
import webbrowser#用于链接网络的模块

root = Tk()

text = Text(root,width=30,height=5)
text.pack()

text.insert(INSERT,"baidu.com")
text.tag_add("link","1.0","1.9")#设置标签,取0-8列的做标记
text.tag_configure("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.baidu.com")

text.tag_bind("link","<Enter>",show_arrow_cursor)#当鼠标进入时调用函数
text.tag_bind("link","<Leave>",show_xterm_cursor)#当鼠标离开时就调用一个函数
text.tag_bind("link","<Button-1>",click)#当鼠标点击时调用click函数,调用网站

mainloop()

#search(查找)用于查找文本中的内容。
例2:
rom tkinter import *
import webbrowser#用于链接网络的模块

root = Tk()

text = Text(root,width=30,height=5)
text.pack()

text.insert(INSERT,"baidu.com")
def getIndex(text,index):#将得到的位置转换成你看懂的格式
return tuple(map(int,str.split(text.index(index),".")))

start = "1.0"
while True:
pos = text.search("0",start,stopindex=END)#查找文本中是否有o在
if not pos:
break
print("找到了,位置是:",getIndex(text,pos))
start = pos + "+1c"#加一个字符的位置

mainloop()

#undo方法在Text中默认是关闭的,要打开需要设置例如text = Text(root,width=30,height=5,undo = True)
例3:
from tkinter import *
import hashlib#用于Key事件

root = Tk()

text = Text(root,width=30,height=5,undo = True,autoseparator = False)#将autoseparato关闭,让系统不自都插入分割符
text.pack()

text.insert(INSERT,"baidu.com")
def callback(event):
text.edit_separator()

text.bind('<Key>',callback)

def show():
text.edit_undo()

Button(root,text = "撤销",command = show).pack()

mainloop()

48、069
Canvas(画布)
canvas.coords(对象名,位置)#用于移动对象的位置,例如line1 =canvas.create_line(0,50,200,50,fill="yellow")#设置一条横线,颜色为黄色
canvas.coords(line,0,25,200,25)#将line1的线移动
canvas.itemconfig(对象名,要改的选项)#用于修改对象的一些项,例如rect1 = canvas.create_rectangle(50,25,150,75,fill="blue")#设置一个矩形,颜色为蓝色
canvas.itemconfig(rect1,fill = "red")#将矩形的颜色修改成红色
canvas.delete(对象名)#用于删除对象
.create_oval()#用于画椭圆形。
.create_polygon()#用于画多边形。

49、070
Canvas指定对象可以用Item handls(对象的ID),Tags(给对象命名,然后指定),All(所有画布对象),CURRENT(鼠标指针下的画布对象)
例如:实现一个画板
from tkinter import *

root = Tk()

w = Canvas(root,width = 400,height = 200)
w.pack()

def paint(event):
x1,y1 =(event.x - 1),(event.y - 1)#将鼠标的位置设置成一个小圆形
x2,y2 =(event.x + 1),(event.y + 1)
w.create_oval(x1,y1,x2,y2,fill = "green")

w.bind("<B1-Motion>",paint)#将鼠标左键于画布绑定


mainloop()

猜你喜欢

转载自www.cnblogs.com/dcpb/p/11649419.html