Python3 Tkinter-Text

1.创建

from tkinter import *

root=Tk()

t=Text(root)
t.pack()

root.mainloop()

Python3 Tkinter-Text

2.添加文本

from tkinter import *

root=Tk()

t=Text(root)
t.insert(1.0,'0123456789')
t.insert(1.0,'ABDCEFGHIJ')
t.pack()

root.mainloop()

Python3 Tkinter-Text

3.设置添加位置

from tkinter import *

root=Tk()

t=Text(root)

for i in range(1,10):
    t.insert(1.0,'0123456789\n')

def insertText():
    t.insert(INSERT,'jcodeer')
def currentText():
    t.insert(CURRENT,'jcodeer')
def endText():
    t.insert(END,'jcodeer')
def selFirstText():
    t.insert(SEL_FIRST,'jcodeer')
def selLastText():
    t.insert(SEL_LAST,'jcodeer')

Button(root,text='insert jcodeer at INSERT',command=insertText).pack(fill=X)
Button(root,text='insert jcodeer at CURRENT',command=insertText).pack(fill=X)
Button(root,text='insert jcodeer at END',command=endText).pack(fill=X)
Button(root,text='insert jcodeer at SEL_FIRST',command=selFirstText).pack(fill=X)
Button(root,text='insert jcodeer at SEL_LAST',command=selLastText).pack(fill=X)

t.pack()

root.mainloop()

Python3 Tkinter-Text

SEL_FIRST和SEL_LAST没有选中区域会引发异常

4.文本属性

from tkinter import *

root=Tk()

t=Text(root)

t.tag_config('a',foreground='red')
t.insert(1.0,'0123456789','a')

t.pack()

root.mainloop()

Python3 Tkinter-Text

5.使用相同属性

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()

Python3 Tkinter-Text

后创建的会覆盖掉其他的设置

6.控制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')

t.insert(1.0,'0123456789',('b','a'))

t.pack()

root.mainloop()

Python3 Tkinter-Text

7.对文本块添加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')

t.tag_add('b','2.5','2.end')

t.pack()

root.mainloop()

Python3 Tkinter-Text

8.使用自定义mark添加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')

t.mark_set('ab','3.1')
t.mark_set('cd',END)
t.tag_add('b','ab','cd')

t.pack()

root.mainloop()

Python3 Tkinter-Text

9.使用get获取内容

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)
t.tag_add('b','ab','cd')
print(t.get('ab','cd'))
t.pack()

root.mainloop()

Python3 Tkinter-Text

10.删除文本

t.delete(1.0,4.0)

1.0:第一行第零列

4.0:第四行第零列

11.删除属性

t.tag_delete(‘b’)

12.插入内置属性

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)
t.tag_add('b','ab','cd')
print(t.get('ab','cd'))
t.insert('b.first','first')
t.insert('b.last','last')
t.pack()

root.mainloop()

Python3 Tkinter-Text

13.插入按钮

from tkinter import *

root=Tk()

t=Text(root)

for i in range(10):
    t.insert(1.0,'0123456789\n')

def printText():
    print('button in text')

bt=Button(t,text='button',command=printText)
t.window_create(2.0,window=bt)

t.pack()

root.mainloop()

Python3 Tkinter-Text

14.绑定事件

from tkinter import *

root=Tk()

t=Text(root)

for i in range(10):
    t.insert(1.0,'0123456789\n')

t.tag_config('a',foreground='blue',underline=1)
def enterTag(event):
    print('Enter event')
t.tag_bind('a','<Enter>',enterTag)
t.insert(2.0,'Enter event\n','a')

t.pack()

root.mainloop()

Python3 Tkinter-Text

 
上一篇:在带有NVIDIA GPU的环境中使用tvm


下一篇:CMAKE学习笔记