python在文本添加超链接_Python之tkinter:tkinter带你进入GUI世界(text.insert/link)

本文通过Python的tkinter库展示了如何在文本框中插入内容并添加超链接,包括设置背景色、前景色,以及实现超链接的点击事件,用于打开网页。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(text.insert/link各种事件)导读 动态演示调用python库的tkinter带你进入GUI世界(text.insert/link各种事件)

目录

tkinter应用案例—text.insert/link各种事件

1、tkinter应用案例:利用(line,colum)行列从(1,0)开始

#tkinter应用案例:利用(line,colum)行列从(1,0)开始

from tkinter import *

from PIL.ImageTk import PhotoImage

root = Tk()

root.title("Jason niu工作室")

theLabel=tk.Label(root,text="进入GUI世界,请开始你的表演!\n点击下方按钮即可获得币分类")

theLabel.pack()

text=Text(root,width=30,height=5)

text.pack()

text.insert(INSERT,"欢迎进入Jason niu工作室\n")

text.tag_add("tag1","1.4","1.13","1.15")

text.tag_add("tag2","1.4","1.13","1.15")

text.tag_config("tag1",background="blue",foreground="yellow")

# text.tag_config("tag2",foreground="black")

mainloop()

2、tkinter应用案例:文本框

from tkinter import *

from PIL.ImageTk import PhotoImage

root = Tk()

root.title("Jason niu工作室")

theLabel=tk.Label(root,text="进入GUI世界,请开始你的表演!")

theLabel.pack()

text = Text(root,width=30,height=5)

text.pack()

text.tag_config("tag1",background="blue",foreground="yellow")

text.tag_config("tag2",foreground="red")

text.tag_lower("tag2")

text.insert(INSERT,"欢迎进入Jason niu工作室\n",("tag2","tag1"))

mainloop()

3、tkinter应用案例:给文本框指定的内容加入超链接

#tkinter应用案例:给文本框指定的内容加入超链接

from tkinter import *

import webbrowser

root = Tk()

root.title("Jason niu工作室")

theLabel=tk.Label(root,text="进入GUI世界,请开始你的表演!\n(点击下边链接即可访问我们官方网站)")

theLabel.pack()

text = Text(root,width=33,height=5)

text.pack()

text.insert(INSERT,"欢迎访问Jason niu工作室官方网站")

text.tag_add("link","1.4","1.15")

text.tag_config("link",foreground="blue",underline=True)

def show_arrow_cursor(event):

text.config(cursor="arrow")

def show_xterm_cursor(event):

text.config(cursor="xterm")

def click(event):

webbrowser.open("http://jason-niu.com")

text.tag_bind("link","",show_arrow_cursor)

text.tag_bind("link","",show_xterm_cursor)

text.tag_bind("link","",click)

mainloop()

4、tkinter应用案例:通过验证digest摘要值来判断文本内容是否发生改变

#tkinter应用案例:通过验证digest摘要值来判断文本内容是否发生改变

from tkinter import *

import hashlib

root = Tk()

root.title("Jason niu工作室")

theLabel=tk.Label(root,text="进入GUI世界,请开始你的表演!\n(点击下边链接即可访问我们官方网站)")

theLabel.pack()

text = Text(root,width=33,height=5)

text.pack()

text.insert(INSERT,"欢迎访问Jason niu工作室官方网站")

contents = text.get("1.0",END)

def getSig(contents):

m = hashlib.md5(contents.encode())

return m.digest()

sig = getSig(contents)

def check():

contents = text.get("1.0",END)

if sig != getSig(contents):

print("警报:内容发生变动!")

else:

print("风平浪静~")

Button(root,text="检查",command=check).pack()

mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值