Tkinter教程之Text(1)篇

来源:互联网 发布:minidao sql 编辑:程序博客网 时间:2024/06/01 08:34
'''Tkinter教程之Text篇(1)''''''1.创建第一个Text'''from tkinter import *root = Tk()t = Text(root)'''2.向Text中添加文本'''# 向第一行,第一列添加文本0123456789t.insert(1.0, '0123456789')# 向第一行第一列添加文本ABCDEFGHIJt.insert(1.0, 'ABCDEFGHIJ')t.pack()root.mainloop()# root中含有一Text控件,可以在这个控件内输入文本,可以使用Ctrl+C/V向Text内添加剪切板上的内容(文本),不接受Ctrl+Z执行操作

'''3.使用line.col索引添加内容'''# 使用indexes来添加Text的内容# -*- coding: utf-8 -*-from tkinter import *root = Tk()t = Text(root)# 向第一行,第一列添加文本0123456789t.insert(1.0, '0123456789')t.insert(1.0, ' ')# 向第一行第一列添加文本ABCDEFGHIJt.insert(1.0, 'ABCDEFGHIJ')t.pack()root.mainloop()# 可以看到使用indexes时,如果其值超过了Text的buffer值,程序不会抛出异常,它会使用向给定值靠近。

'''mark是用来表示在Text中位置的一类符号''''''4.使用内置的mark控制添加位置'''# 演示了内置的mark:INSERT/CURRENT/END/SEL_FIRST/SEL_LAST的用法# -*- coding: utf-8 -*-from tkinter import *root = Tk()t = Text(root)# 向Text中添加10行文本for i in range(1, 10):    t.insert(1.0, '0123456789 ')# 定义各个Button的回调函数,这些函数使用了内置的mark:INSERT/CURRENT/END/SEL_FIRST/SEL_LASTdef 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')# INSERTButton(root,       text='insert jcodeer at INSERT',       command=insertText       ).pack(fill=X)# CURRENTButton(root,       text='insert jcodeer at CURRENT',       command=insertText       ).pack(fill=X)# ENDButton(root,       text='insert jcodeer at END',       command=endText       ).pack(fill=X)# SEL_FIRSTButton(root,       text='insert jcodeer at SEL_FIRST',       command=selFirstText       ).pack(fill=X)# SEL_LASTButton(root,       text='insert jcodeer at SEL_LAST',       command=selLastText       ).pack(fill=X)t.pack()root.mainloop()# 几个内置的mark:# INSERT:光标的插入点# CURRENT:鼠标的当前位置所对应的字符位置# END:这个Text buffer的最后一个字符# SEL_FIRST:选中文本域的第一个字符,如果没有选中区域则会引发异常# SEL_LAST:选中文本域的最后一个字符,如果没有选中区域则会引发 异常

'''5.使用表达式来增强mark'''# 表达式(expression)可以个性任何的Indexes,如下:'''+ count chars :前移count字符- count chars :后移count字符+ count lines :前移count行- count lines :后移count行linestart:移动到行的开始linesend:移动到行的结束wordstart:移动到字的开始wordend:移动到字的结束'''# 演示修饰符表达式的使用方法,如何与当前可用的indexes一起使用# -*- coding: utf-8 -*-from tkinter import *root = Tk()t = Text()# 向第一行,第一列添加文本0123456789for i in range(1, 10):    t.insert(1.0, '0123456789 ')a = 'test_mark'def forwardChars():    # 直接连接字符串    # t.mark_set(a,CURRENT + '+ 5 chars')    t.mark_set(a, CURRENT + '+5c')def backwardChars():    # t.mark_set(a,CURRENT + '- 5 chars')    t.mark_set(a, CURRENT + '-5c')def forwardLines():    # t.mark_set(a,CURRENT + '+ 5 lines)    t.mark_set(a, CURRENT + '+5l')def backwardLines():    # t.mark_set(a,CURRENT + '- 5 lines)    t.mark_set(a, CURRENT + '-5l')def lineStart():    # 注意linestart前面的那个空格不可省略    t.mark_set(a, CURRENT + ' linestart')def lineEnd():    # 注意lineend前面的那个空格不可省略    t.mark_set(a, CURRENT + ' lineend')def wordStart():    # 移动到当前字的开始。    t.mark_set(a, CURRENT + ' wordstart')def wordend():    # 移动到当前字的结束    t.mark_set(a, CURRENT + ' wordend')# mark:test_mark默认值为CURRENTt.mark_set(a, CURRENT)Button(root, text='forward 5 chars', command=forwardChars).pack(fill=X)Button(root, text='backward 5 chars', command=backwardChars).pack(fill=X)Button(root, text='forward 5 lines', command=forwardLines).pack(fill=X)Button(root, text='backward 5 lines', command=backwardLines).pack(fill=X)Button(root, text='line start', command=lineStart).pack(fill=X)Button(root, text='line end', command=lineEnd).pack(fill=X)Button(root, text='word start', command=lineEnd).pack(fill=X)Button(root, text='word end', command=lineEnd).pack(fill=X)# 测试三个位置的不同,CURRENT可以得知是当前光标的位置;mark就表示mark的位置了,INSERT好像一植都在1.0处没有改变。def insertText():    t.insert(INSERT, 'insert')def currentText():    t.insert(CURRENT, 'current')def markText():    t.insert(a, 'mark')Button(root, text='insert jcodeer.cublog.cn', command=insertText).pack(fill=X)Button(root, text='current jcodeer.cublog.cn', command=currentText).pack(fill=X)Button(root, text='mark jcodeer.cublog.cn', command=markText).pack(fill=X)t.pack()root.mainloop()

'''Tkinter教程之Text(2)篇''''''6.使用tag来指定文本的属性'''# 创建一个指定背景颜色的TAG# -*- coding: utf-8 -*-from tkinter import *root = Tk()t = Text(root)# 创建一个TAG,其前景色为红色t.tag_config('a', foreground='red')# 使用TAG 'a'来指定文本属性t.insert(1.0, '0123456789', 'a')t.pack()root.mainloop()# 结果是文本颜色改变为红色了

'''7.同时使用两个文本指定同一个属性'''# 没有特别设置的话,最后创建的那个会覆盖掉其它所有的设置# -*- coding: utf-8 -*-from tkinter import *root = Tk()t = Text(root)# 创建一个TAG,其前景色为红色t.tag_config('a', foreground='red')t.tag_config('b', foreground='blue')# 使用TAG 'a'来指定文本属性t.insert(1.0, '0123456789', ('b', 'a'))t.pack()root.mainloop()# 结果是文本的颜色不是按照insert给定的顺序来设置,而是按照tag的创建顺序来设置的。

'''8.控制tag的级别'''# 使用tag_lower/tag_raise来降低或提高tag的级别# -*- coding: utf-8 -*-from tkinter import *root = Tk()t = Text(root)# 创建一个TAG,其前景色为红色t.tag_config('a', foreground='red')t.tag_config('b', foreground='blue')# 使用tag_lower来降低a的级别t.tag_lower('a')# 使用TAG 'a'来指定文本属性t.insert(1.0, '0123456789', ('b', 'a'))t.pack()root.mainloop()# 结果:文本内容颜色变为了蓝色,蓝色的作用级别大于红色了,即使是先创建了蓝色。

'''9.对文本块添加tag'''# tag_add方法的使用# -*- coding: utf-8 -*-from tkinter import *root = Tk()t = Text(root)# 创建一个TAG,其前景色为蓝色t.tag_config('b', foreground='blue')for i in range(10):    t.insert(1.0, '0123456789\n')t.tag_add('b', '2.5', '2.end')t.pack()root.mainloop()# 先向Text中添加了10行文本,创建一tag,将第2行第6列至第二行行尾使用使用此tag

0 0
原创粉丝点击