Tkinter Text
Text极简例子
from tkinter import *
root = Tk()
t = Text(root)
t.pack()
root.mainloop()
向Text中添加内容
from tkinter import *
root = Tk()
t = Text(root)
t.insert(1.0, '0123456789')
t.insert(1.0, 'ABCDEFGHIJ')
t.pack()
root.mainloop()
使用索引添加内容
from tkinter import *
root = Tk()
t = Text(root)
t.insert(1.0, '0123456789')
t.insert(1.0, 'ABCDEFGHIJ')
t.insert('2.end', '\n')
t.insert(2.5, 'ABCDEFGHIJ')
t.pack()
root.mainloop()
内置mark
INSERT:光标插入位置
CURRENT:当前的光标插入位置
END:整个 Buffer 的最后
SEL_FIRST:选中文本的开始
SEL_LAST:选中文本的最后
表达式
- count chars :前移 count 字符
- count chars :后移 count 字符
- count lines :前移 count 行
- count lines :后移 count 行
linestart:移动到行的开始
linesend:移动到行的结束
wordstart:移动到字的开始
wordend:移动到字的结束
tag指定文本属性
from tkinter import *
root = Tk()
t = Text(root)
t.tag_config('a', foreground='red')
t.insert(1.0, '0123456789', 'a')
t.pack()
root.mainloop()
tag的优先级
from tkinter import *
root = Tk()
t = Text(root)
t.tag_config('a', foreground='red')
t.tag_config('b', foreground='blue')
t.insert(1.0, '0123456789', ('b', 'a'))
t.pack()
root.mainloop()
降低tag的优先级
from tkinter import *
root = Tk()
t = Text(root)
t.tag_config('a', foreground='red')
t.tag_config('b', foreground='blue')
t.tag_lower('b')
for i in range(10):
t.insert(1.0, '0123456789\n', ('b', 'a'))
t.pack()
root.mainloop()
对文本块添加tag
from tkinter import *
root = Tk()
t = Text(root)
t.tag_config('a', foreground='red')
t.tag_config('b', foreground='blue')
for i in range(10):
t.insert(1.0, '0123456789\n','a')
t.tag_add('b', '2.5', '2.end')
t.pack()
root.mainloop()
自定义mark添加tag
from tkinter import *
root = Tk()
t = Text(root)
t.tag_config('b', foreground='blue')
t.tag_lower('b')
for i in range(10):
t.insert(1.0, '0123456789\n')
t.mark_set('ab', '3.1')
t.mark_set('cd', END)
t.tag_add('b', 'ab', 'cd')
t.pack()
root.mainloop()
使用indexes获取文本内容
from tkinter import *
root = Tk()
t = Text(root)
for i in range(10):
t.insert(1.0, '0123456789\n')
print(t.get('1.0', '2.3'))
t.mark_set('ab', '3.1')
t.mark_set('cd', END)
print(t.get('ab', 'cd'))
t.pack()
root.mainloop()
delete对tag的影响
from tkinter import *
root = Tk()
t = Text(root)
t.tag_config('b', foreground='blue')
for i in range(10):
t.insert(1.0, '0123456789\n')
t.mark_set('ab', '3.1')
t.mark_set('cd', END)
t.tag_add('b', 'ab', 'cd')
t.delete('1.0', '4.0')
t.pack()
root.mainloop()
tag_delete对文本属性的影响
from tkinter import *
root = Tk()
t = Text(root)
t.tag_config('b', foreground='blue')
for i in range(10):
t.insert(1.0, '0123456789\n')
t.mark_set('ab', '3.1')
t.mark_set('cd', END)
t.tag_add('b', 'ab', 'cd')
t.tag_delete('b')
t.pack()
root.mainloop()
tag的两个内置函数
from tkinter import *
root = Tk()
t = Text(root)
t.tag_config('b', foreground='blue')
for i in range(10):
t.insert(1.0, '0123456789\n')
t.mark_set('ab', '3.1')
t.mark_set('cd', END)
t.tag_add('b', 'ab', 'cd')
t.insert('b.first', 'first')
t.insert('b.last', 'last')
t.pack()
root.mainloop()
Text中添加Button
from tkinter import *
def printtext():
print('button in text')
root = Tk()
t = Text(root)
t.tag_config('b', foreground='blue')
for i in range(10):
t.insert(1.0, '0123456789\n')
bt = Button(t, text='button', command=printtext)
t.window_create('2.0', window=bt)
t.pack()
root.mainloop()
tag与事件绑定
from tkinter import *
def entertag(event):
print('enter event')
root = Tk()
t = Text(root)
for i in range(10):
t.insert(1.0, '0123456789\n')
t.tag_config('a', foreground='blue', underline=1)
t.tag_bind('a', '<Enter>', entertag)
t.insert(2.0, 'Enter event\n', 'a')
t.pack()
root.mainloop()