Python3 Tkinter-Text
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的更多相关文章
- python tkinter Text
"""小白随笔,大佬勿喷""" '''tkinter —— text''' '''可选参数有: background(bg) 文本框背景色: ...
- Tkinter Text(文本)
Tkinter Text(文本): 文本小部件提供先进的功能,让您编辑多行文本格式,如改变其颜色和字体的方式显示. 文本小部件提供先进的功能,让您编辑多行文本格式,如改变其颜色和字体的方 ...
- python3+tkinter实现的黑白棋,代码完整 100%能运行
今天分享给大家的是采用Python3+tkinter制作而成的小项目--黑白棋 tkinter是Python内置的图形化模块,简单易用,一般的小型UI程序可以快速用它实现,具体的tkinter相关知识 ...
- Python3 tkinter基础 Text image 文本框中插入图片
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Python3 tkinter基础 Text window 文本框中插入按钮
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Python3 tkinter基础 Button text,fg 按钮上显示的文字 文字的颜色
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- python3 tkinter添加图片和文本
在前面一篇文章基础上,使用tkinter添加图片和文本.在开始之前,我们需要安装Pillow图片库. 一.Pillow的安装 1.方法一:需要下载exe文件,根据下面图片下载和安装 下载完 ...
- python3 tkinter模块
一.tkinter 1.tkinter--tool kit interface工具包接口,用于GUI(Graphical User Interface)用户图形界面, 2.python3.x把Tkin ...
- Python3 tkinter基础 TK title 设置窗体的标题
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Python3 tkinter基础 Tk quit 点击按钮退出窗体
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
随机推荐
- div样式position:fixed,不随屏幕滚动而滚动,导致屏幕太小时弹出层被遮挡,无法滚动查看的解决办法
window.onscroll = function () { var sl = -Math.max(document.body.scrollTop, document.documentElement ...
- Spring的入门学习笔记 (AOP概念及操作+AspectJ)
AOP概念 1.aop:面向切面(方面)编程,扩展功能不通过源代码实现 2.采用横向抽取机制,取代了传统的纵向继承重复代码 AOP原理 假设现有 public class User{ //添加用户方法 ...
- mysql 8.0.12 日常出错
最近不知道怎么回事,数据库老是会输出一个: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and co ...
- Git安装配置(Windows)
下载Git并安装 下载地址:https://git-scm.com/ 安装一般默认即可 配置用户信息 配置之前最好已经有了Github的账号,如果没有可以先去注册一个 安装后打开Git Bash gi ...
- shell基础笔记
什么是shell脚本 我自己对shell脚本的理解就是一系列的shell命令加入逻辑关系,实现类似"批处理"的功能.而不是简单的命令的堆砌,那样的shell脚本bug重重. 脚本开 ...
- MongoDB入门---文档查询之$type操作符&limit方法&skip方法&简单排序(sort)操作
上一篇文章呢,已经分享过了一部分查询操作了,这篇文章呢?就来继续分享哈.接下来呢我们直接看MongoDB中的$type操作符哈.它呢是基于BSON类型来检索集合中匹配的数据类型,并且返回结果,在Mon ...
- 【Hadoop】Seondary NameNode不是备份NameNode!!
昨天和舍友聊天时无意中提起Secondary NameNode,他说这是备用NameNode.我当时就有点疑惑..之后查阅了相关资料和博客,算是基本理解了什么是Secondary NameNode. ...
- C#防止程序重新运行
//禁止重复运行 bool ret; Mutex mutex = new Mutex(true, Application.ProductName, out ret); if (ret) { Appli ...
- 全局脚手架了解一下【fle-cli】
本文来自网易云社区 介绍 fle-cli旨在帮助我们从复杂繁琐的编译配置中解放出来,全身心地投入业务开发中,提高开发效率. 它是真正意义上的全局脚手架,区别于市面上其他的全局脚手架,它不会在项目工程中 ...
- 几个常用的轻量级web服务
Node.js 安装:npm install http-server 使用:hs命令,可启动以当前目前为webroot的8080端口web服务,也可指定端口 Python 安装:内置 使用:pytho ...