赞
踩
Text
Text(文本)组件用于显示和处理多行文本。在 Tkinter 的所有组件中,Text 组件显得异常强大和灵活,适用于多种任务。虽然该组件的主要目的是显示多行文本,但它常常也被用于作为简单的文本编辑器和网页浏览器使用。
何时使用 Text 组件?
Text 组件用于显示文本文档,包含纯文本或格式化文本(使用不同字体,嵌入图片,显示链接,甚至是带 CSS 格式的 HTML 等)。因此,它常常也被用于作为简单的文本编辑器和网页浏览器使用。
用法
当你创建一个 Text 组件的时候,它里边是没有内容的。为了给其插入内容,你可以使用 insert() 方法以及 "insert" 或 "end" 索引号:
from tkinter import *
root = Tk()
# width 单行可见的字符
# height 显示的行数
text = Text(root, width=50, height=30, undo=True, autoseparators=False)
text.pack()
# INSERT 光标处插入
# END 末尾处插入
text.insert(INSERT, '插入文字')
root.mainloop()
插入对象
from tkinter import *
#在 Text 组件中插入对象,可以使用 window_create() 和 image_create() 方法
root = Tk()
# width 单行可见的字符
# height 显示的行数
text = Text(root, width=50, height=30, undo=True, autoseparators=False)
text.pack()
# INSERT 光标处插入
# END 末尾处插入
text.insert(INSERT, '插入组件')
def show():
print('我被点了一下~')
# 插入组件
bt = Button(root, text='点我', command=show, padx=10)
text.window_create(INSERT, window=bt)
text.insert(INSERT, '\n')
# 插入图片
text.insert(INSERT, '插入图片')
photo = PhotoImage(file='ico.png')
text.image_create(END, image=photo)
root.mainloop()
delete() 方法
from tkinter import *
# 删除 Text 组件中的内容可以用 delete() 方法,下边代码用于删除所有内容(也包含 window 和 image 对象,但不会删除 marks 的内容):
root = Tk()
text = Text(root, width=20, height=5)
text.pack()
text.insert(INSERT, "You are good!")
text.delete(1.0, END)
root.mainloop()
将 state 选项从默认的 "normal" 修改为 "disabled",使得 Text 组件中的内容为“只读”形式。不过需要注意的是,当你需要进行任何修改的时候,记得将 state 选项改回 "normal",否则 insert() 和 delete() 方法都会失效。
get() 方法(仅获取文本内容):
from tkinter import *
import hashlib
root = Tk()
# width 单行可见的字符
# height 显示的行数
text = Text(root, width=50, height=30, undo=True, autoseparators=False)
text.pack()
# INSERT 光标处插入
# END 末尾处插入
text.insert(INSERT, '插入组件')
# 检测内容是否改变
def getSig(contents):
m = hashlib.md5(contents.encode())
# 获取摘要 比较
return m.digest()
sig = getSig(text.get('1.0', END))
def check():
contents = text.get('1.0', END)
if sig != getSig(contents):
print('内容有变动')
else:
print('没有变动')
Button(root, text='检查', command=check).pack()
root.mainloop()
index() 方法
from tkinter import *
#用于将所有支持的“索引”格式(请参考下方【Indexes 用法】)转换为“行.列”格式的索引号
root = Tk()
text = Text(root, width=20, height=5)
text.pack()
print(text.index("insert"))
text.insert("insert", "You are good!")
print(text.index("insert"))
root.mainloop()
跟踪一个位置,标记
from tkinter import *
root = Tk()
text = Text(root, width=20, height=5)
text.pack()
text.insert(INSERT, "You are good!")
text.mark_set('here', '1.8')
text.insert('here', "very ")
root.mainloop()
search() 方法可以搜索 Text 组件中的内容。
from tkinter import *
#你可以提供一个确切的目标进行搜索(默认),也可以使用 Tcl 格式的正则表达式进行搜索(需设置 regexp 选项为 True)
root = Tk()
text = Text(root, width=20, height=5)
text.pack()
text.insert(INSERT, 'I love FishC.com')
# 搜索
start = '1.0'
while True:
pos = text.search('o', start, stopindex=END)
if not pos:
break
print("找到了,位置是:", pos)
start = pos + '+1c' # 指向下一个字符
root.mainloop()
# 如果忽略 stopindex 选项,表示直到文本的末尾结束搜索。
#设置 backwards 选项为 True,则是修改搜索的方向(变为向后搜索,那么 start 变量你应该设置为 END,stopindex 选项设置为 1.0,最后 "+1c" 改为 "-1c")
“恢复”和“撤销”操作
from tkinter import *
root = Tk()
text = Text(root, width=50, height=30, undo=True, autoseparators=False)
text.pack()
text.insert(INSERT, 'I love FishC.com')
# 撤销 恢复
# 撤销操作 需要打开 undo=True Text(root, width=50, height=30,undo=True)
def back():
text.edit_undo()
def callback(event):
text.edit_separator()
# 自动插入分隔符,撤销时一个字一个字的撤销,默认人工插入 autoseparators=False
# Text(root, width=50, height=30, undo=True, autoseparators=False)
# 每当有按键操作的时候 插入一个分割符
text.bind('', callback)
Button(root, text='撤销', command=back).pack()
root.mainloop()
Indexes 用法
Indexes(索引)是用来指向 Text 组件中文本的位置,跟 Python 的序列索引一样,Text 组件索引也是对应实际字符之间的位置。
Tkinter 提供一系列不同的索引类型:
"line.column"(行/列)
"line.end"(某一行的末尾)
"insert"
"current"
"end"
user-defined marks
user-defined tags("tag.first","tag.last")
selection(SEL_FIRST,SEL_LAST)
window coordinate("@x,y")
embedded object name(window,images)
expressions
"line.column"
from tkinter import *
root = Tk()
text = Text(root, width=50, height=30)
text.pack()
text.insert(INSERT, 'I love FishC.com')
# 使用 index() 方法可以将所有支持的“索引”格式转换为“行/列”格式的索引号。
# 行/列是最基础的索引方式,它们将索引位置的行号和列号以字符串的形式表示出来(中间
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。