1.创建

from tkinter import *

root=Tk()

t=Text(root)
t.pack()

root.mainloop()

2.添加文本

from tkinter import *

root=Tk()

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

root.mainloop()

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

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

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

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

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

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

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

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

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

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

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的更多相关文章

  1. python tkinter Text

    """小白随笔,大佬勿喷""" '''tkinter —— text''' '''可选参数有: background(bg) 文本框背景色: ...

  2. Tkinter Text(文本)

         Tkinter Text(文本): 文本小部件提供先进的功能,让您编辑多行文本格式,如改变其颜色和字体的方式显示.   文本小部件提供先进的功能,让您编辑多行文本格式,如改变其颜色和字体的方 ...

  3. python3+tkinter实现的黑白棋,代码完整 100%能运行

    今天分享给大家的是采用Python3+tkinter制作而成的小项目--黑白棋 tkinter是Python内置的图形化模块,简单易用,一般的小型UI程序可以快速用它实现,具体的tkinter相关知识 ...

  4. Python3 tkinter基础 Text image 文本框中插入图片

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  5. Python3 tkinter基础 Text window 文本框中插入按钮

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. Python3 tkinter基础 Button text,fg 按钮上显示的文字 文字的颜色

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  7. python3 tkinter添加图片和文本

    在前面一篇文章基础上,使用tkinter添加图片和文本.在开始之前,我们需要安装Pillow图片库. 一.Pillow的安装 1.方法一:需要下载exe文件,根据下面图片下载和安装       下载完 ...

  8. python3 tkinter模块

    一.tkinter 1.tkinter--tool kit interface工具包接口,用于GUI(Graphical User Interface)用户图形界面, 2.python3.x把Tkin ...

  9. Python3 tkinter基础 TK title 设置窗体的标题

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  10. Python3 tkinter基础 Tk quit 点击按钮退出窗体

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

随机推荐

  1. django模板中如何导入js、css等外部文件

    本教程只适合Django1.4版本.(1.8版本之后不需要这么麻烦,详见 http://www.cnblogs.com/ryan255/p/5465608.html) html模板里面使用了css,但 ...

  2. 【oracle笔记3】多表查询

    *多表查询 分类:1.合并结果集 2.连接查询 3.子查询 *合并结果集:要求被合并的表中,列的类型和列数相同. *UNION,去除重复行.完全相同的行会被去除 *UNION ALL:不去除重复行. ...

  3. 数据库函数(Left、Right)

    MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr()  ...

  4. AJAX 动态加载后台数据 绑定select

    <select id="select"> <!--下拉框数据动态加载--> </select> js:(使用jquery) $(document ...

  5. 常用的JavaScript设计模式(二)Factory(工厂)模式

    Factory通过提供一个通用的接口来创建对象,同时,我们还可以指定我们想要创建的对象实例的类型. 假设现在有一个汽车工厂VehicleFactory,支持创建Car和Truck类型的对象实例,现在需 ...

  6. Blender2.79建模快捷键

    快捷键 基本操作 滚动鼠标中键滚轮:视图放大或缩小 按住鼠标中键滚轮:视图旋转 单独鼠标右键:选择物体 单独鼠标右键:放置物体 shift+鼠标中键:视图平移 小键盘数字1:前视图:ctrl+1:后视 ...

  7. for循环删除列表中元素遇到的漏删的问题(python)

    问题描述:python中通过for循环来删除列表中的两个相邻的元素,存在漏删的问题 比如说下面的例子,准备删掉2和3,但是结果是2删掉了,3没删掉 是因为把2删掉后3的下标就变成了1,但是原本下标为1 ...

  8. C语言基础——链表的相关操作

    1 #include <stdio.h> #include <malloc.h> #include <string.h> #include <math.h&g ...

  9. golang for循环里面创建协程问题的研究

    原本想在一个for里面创建10个协程,这些协程顺序拿到for的递增变量,把这10个递增变量都打印出来.但事与愿违,于是做实验,查书,思考,写出以下记录. golang里,在for循环里面起协程,如下代 ...

  10. 前端chrome调试技巧

    待更新:http://blog.csdn.net/xueer767/article/details/65936204?locationNum=8&fps=1