Python Tkinter Text控件

原文地址: http://blog.csdn.net/bemorequiet/article/details/54743889

这篇博客主要是简单的说一下Tkinter中的Text控件的相关知识。

Text文本组件用于显示和处理多行文本。在Tkinter的所有组件中,Text组件显得异常强大和灵活,它适用于处理多任务,虽然该组件的主要目的是显示多行文本,但它常常被用于作为简单的文本编辑器和网页浏览器使用。

当创建一个Text组件的时候里面是没有内容的。为了给其插入内容,可以使用insert()以及INSERT或END索引号。

1.普通的Text组件

from tkinter import *
root = Tk()

text1 = Text(root,width=30,height=4)
#INSERT索引表示在光标处插入
text1.insert(INSERT,'I Love')
#END索引号表示在最后插入
text1.insert(END,' you')
text1.pack()
mainloop()

2.插入Button之后的Text组件

from tkinter import *

root = Tk()

text1 = Text(root,width=30,height=2)
text1.pack()
text1.insert(INSERT,'I love you')

def show():
     print('吆喝,我被点了一下')
#text还可以插入按钮  图片等
b1 = Button(text1,text='点我点我',command=show)
#在text创建组件的命令
text1.window_create(INSERT,window=b1)

mainloop()

3.插入图片之后的Text组件

from tkinter import *

root = Tk()

text1 = Text(root,width=100,height=30)

text1.pack()

photo = PhotoImage(file='text.gif')

def show():
     #添加图片用image_create
     text1.image_create(END,image=photo)

b1 = Button(text1,text='点我点我',command=show)
     #添加插件用window_create
text1.window_create(INSERT,window=b1)

mainloop()

4.Text中的Indexes

Indexes(索引)是用来指向Text组件中文本的位置,跟python的序列索引一样,Text的组件索引也是对应实际字符之间的位置。值得注意的是: 行号以1开始 列号以0开始


from tkinter import *
root = Tk()
text1=Text(root,width=30,height=3)
text1.insert(INSERT,'index的练习')
#1.2到1.5的范围之间
print(text1.get(1.2,1.5))

5.Text中的Marks

Marks(标记)通常是嵌入到Text组件文本中的不可见的对象。事实上,Marks是指定字符间的位置,并跟随相应的字符一起移动。Marks有INSERT,CURRENT,和user-defined marks(用户自定义的Marks), 
其中,INSERT和CURRENT是Tkinter预定义的特殊Marks,它们是不可能被删除的 
INSERT(或insert)用于指定当前插入光标的位置,Tkinter会在该位置绘制一个闪烁的光标(因此并不是所有的Marks都不可见) 
CURRENT用于指定与鼠标坐标坐标最近最接近的位置,不过,如果你按紧鼠标任何一个按钮,它会直到你松开它才响应 
使用mark_set()方法创建和移动Marks 
使用mark_unset()方法删除Marks 
Mark事实上就是索引,用于表示位置

from tkinter import *

root = Tk()

text1 =Text(root,width=30,height=4)

text1.insert(INSERT,'I Love FishC.com')
text1.mark_set('here',1.2)
#插入是指在前面插入
text1.insert('here','插')
text1.pack()

mainloop()

6.Text中的Tags

Tags通常用于改变Text组件中内容的样式和功能,你可以修改文本的字体,尺寸和颜色,另外Tags还允许你将文本、嵌入的组件和图片与键盘相关联,除了user-defined tags(用户自定义的Tags),还有 
一个预定义的特殊Tag:SEL

from tkinter import *

root = Tk()

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

text1.insert(INSERT,'I Love FishC.com!')
#第一个参数为自定义标签的名字
#第二个参数为设置的起始位置,第三个参数为结束位置
#第四个参数为另一个位置
text1.tag_add('tag1','1.7','1.12','1.14')
#用tag_config函数来设置标签的属性
text1.tag_config('tag1',background='yellow',foreground='red')
#新的tag会覆盖旧的tag
mainloop()

7.Tags的事件绑定 
Tags还支持事件的绑定,绑定事件使用的是tag_bind()方法,下面代码实现了将文本与鼠标事件进行绑定,当鼠标进入该文本时,鼠标样式切换为‘arrow’形态,离开文本时切换回‘xterm’形态,当触发鼠标‘左键单击操作’事件的时候,使用默认浏览器打开百度。

from tkinter import *
import webbrowser

root = Tk()

text = Text(root,width=30,height=5)
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://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)

mainloop()

8.使用Text组件中文本的MD5摘要来判断内容是否发生改变

from tkinter import *
import hashlib

root = Tk()

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

text1.insert(INSERT,'I Love FishC.com!')
contents = text1.get('1.0',END)

def getSig(contents):
     m=hashlib.md5(contents.encode())
     return m.digest()

sig=getSig(contents)

def check():
     contents = text1.get('1.0',END)
     if sig !=getSig(contents):
          print('警报,内容发生改变')
     else:
          print('风平浪静')

Button(root,text="检查",command=check).pack()

mainloop()

关于Text的组件的相关内容就先介绍到这里,之后会再进行补充。

原文地址:https://www.cnblogs.com/hackpig/p/8196455.html