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. Oracle中order by case 用法

    select * from ly_familyinformation ' ' order by case when relation = '购房人/申请人' then when relation = ...

  2. python 面向对象之添加功能

    '''**#实现功能**案列 姓名:王飞 年龄:30 性别:男 工龄:5我承诺,我会认真教课.王飞爱玩象棋 姓名:小明 年龄:15 性别:男 学号:00023102我承诺,我会 好好学习.小明爱玩足球 ...

  3. 爬虫——Scrapy框架案例二:阳光问政平台

    阳光热线问政平台 URL地址:http://wz.sun0769.com/index.php/question/questionType?type=4&page= 爬取字段:帖子的编号.投诉类 ...

  4. js中回调函数写法

    第一种方式 function studyEnglish(who){ document.write(who+"学习英语</br>"); } function study( ...

  5. Oracle之多表查询

    -多表查询 1.交叉连接 select * from t_class for update; select * from t_student for update; select for update ...

  6. QP之QK原理

    QK是一个很小的抢占式微内核调度程序,它专用用QP中. QK的思想源于SST,Miro Samek重写了自己前期编的SST(Super Simple Task)代码. QK循环查询AO队列的状态表QK ...

  7. IA64与x86-64的区别

    win7 sp1下载地址:https://download.microsoft.com/download/0/A/F/0AFB5316-3062-494A-AB78-7FB0D4461357/wind ...

  8. mac下使用git的冲突的解决方案

    博主之前一直是在windows系统下进行软件代码的开发,window下有很多git的使用工具,如tortoisegit等是个很好的git项目管理工具.而再mac版下的git项目代码管理工具,本人找了好 ...

  9. [Real World Haskell翻译]第27章 网络通信和系统日志 Sockets and Syslog

    第27章 网络通信和系统日志 Sockets and Syslog 基础网络 在本书的前面几章,我们讨论了运转在网络上的服务.其中的两个例子是客户端/服务器架构的数据库和Web服务.当需要制定一个新的 ...

  10. Visual studio 2010 TFS地址解析,让团队资源管理器不再显示IP地址

    第一步: 找到名为hosts的配置文件(路径C:\Windows\System32\drivers\etc\hosts)用记事本打开并写入需要的配置,例如我用到的是TFS服务器的IP地址为192.16 ...