tkinter入门(5)--Text组件,Tag

1、text组件简介

  Text组件可以显示多行文本;
  Text组件可以手动修改文本内容;

2、Text组件使用,其中可以插入按钮、图片等

import tkinter as tk
root = tk.Tk()#生成顶层窗口
root.title("组件使用!")#设置图形用户界面标题
#Text组件
text = tk.Text(root,width = 30,height = 30)
text.pack()
text.insert('insert','aple')
def show():
    print("按钮被按!")
#text组件插入按钮
button = tk.Button(text,text = "点我点我",command = show)
text.window_create('insert',window = button)
#text组件插入图片
photoimage = tk.PhotoImage(file = r'C:\Users\lengxiaohua\Pictures\m.png')
text.image_create('end',image = photoimage)
root.mainloop()#重要步骤,进入主事件循环,由tkinter主管、监听

运行截图:
在这里插入图片描述

3、Tag组件,通常用于改变Text组件中内容的样式和功能

import tkinter as tk
root = tk.Tk()#生成顶层窗口
root.title("组件使用!")#设置图形用户界面标题

#Tag组件,通常用于改变Text组件中内容的样式和功能
text1 = tk.Text(root,width = 30,height = 20)
text1.pack()
text1.insert('insert','apple of name good')
text1.tag_add('tag1','1.7','1.10','1.13')
text1.tag_config('tag1',background = 'yellow',foreground = 'red')
root.mainloop()#重要步骤,进入主事件循环,由tkinter主管、监听

运行截图:
在这里插入图片描述

4、Tag组件事件绑定

  代码功能介绍,当鼠标移动或离开‘特殊文本链接(进入百度首)’时,鼠标形状发生改变,当点击链接时,跳转到百度首页。

import tkinter as tk
import webbrowser
root = tk.Tk()#生成顶层窗口
root.title("组件使用!")#设置图形用户界面标题

#Tag组件事件绑定
text2 = tk.Text(root,width = 30,height = 5)
text2.pack()
text2.insert('insert','进入百度首页')
text2.tag_add('link','1.0','1.5')
text2.tag_config('link',underline = True,foreground = 'blue')
def show_arrow_cursor(self):
    text2.config(cursor = 'arrow')
def show_xterm_cursor(self):
    text2.config(cursor = 'xterm')
def click(self):
    webbrowser.open('http://www.baidu.com')
text2.tag_bind('link','<Enter>',show_arrow_cursor)
text2.tag_bind('link','<Leave>',show_xterm_cursor)
text2.tag_bind('link','<Button-1>',click)
root.mainloop()#重要步骤,进入主事件循环,由tkinter主管、监听

5、检查text文本是否发生改变

import tkinter as tk
import hashlib
root = tk.Tk()#生成顶层窗口
root.title("组件使用!")#设置图形用户界面标题

#检查text文本是否发生改变
text2 = tk.Text(root,width = 30,height = 5)
text2.pack()
text2.insert('insert','进入百度首页')
text2.tag_add('link','1.0','1.6')
text2.tag_config('link',underline = True,foreground = 'blue')
contents = text2.get('1.0','end')
def getSig(contents):
    m = hashlib.md5(contents.encode())
    return m.digest()
sig = getSig(contents)
def check():
    contents = text2.get('1.0','end')
    if sig != getSig(contents):
        print("警报,内容已更改!")
    else:
        print("内容原封不动!")
tk.Button(root,text = '检查',command = check).pack()

root.mainloop()#重要步骤,进入主事件循环,由tkinter主管、监听

运行结果:
内容未改时检查 vs 内容已改时检查
在这里插入图片描述

6、在text文本中全文查找某字符

  代码介绍:由于使用Text变量名.search()只能查找第一个符合要求的位置,所以这里重新写函数,可以查找全局范围内此字符的位置。

import tkinter as tk
root = tk.Tk()#生成顶层窗口
root.title("组件使用!")#设置图形用户界面标题

#在text文本中全文查找某字符
text2 = tk.Text(root,width = 30,height = 5)
text2.pack()
text2.insert('insert','app, aply,apple,apology')
def getIndex(text,index):
    return tuple(map(int,str.split(text.index(index),'.')))

start = 1.0
while True:
    pos = text2.search('a',start,stopindex = 'end')#获取文本中找到的第一个位置
    if not pos:
        break
    print("找到,位置:",getIndex(text2,pos))
    start = pos + "+1c"

root.mainloop()#重要步骤,进入主事件循环,由tkinter主管、监听

在这里插入图片描述

7、text组件的撤销和恢复操作

  代码介绍:代码中包含两个text变量,不同点在于第二个text变量改变了Text组件在每次输入后自动添加换行符操作,改为‘当按键操作时,手动添加每次操作隔离符’。

import tkinter as tk
root = tk.Tk()#生成顶层窗口
root.title("组件使用!")#设置图形用户界面标题
#text组件的撤销和恢复操作
text2 = tk.Text(root,width = 30,height = 5,undo = True)
text2.pack()
text2.insert('insert','app, aply,apple,apology')
def show():
    text2.edit_undo()
tk.Button(root,text = '撤销',command = show).pack()
#默认text组件会给我们每次输入加入分隔符,我们可以自己设定自己手动在每次按键盘后加入分隔符
text3 = tk.Text(root,width = 30,height = 5,undo = True,autoseparator = False)
text3.pack()
text3.insert('insert','app, aply,apple,apology')
def callback(event):
    text3.edit_separator()
text3.bind('<Key>',callback)
def show():
    text3.edit_undo()
tk.Button(root,text = '撤销',command = show).pack()
root.mainloop()#重要步骤,进入主事件循环,由tkinter主管、监听
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值