Tkinter极简例子——Text篇

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()
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ALittleHigh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值