tkinter 改变按钮事件_Python之tkinter:tkinter带你进入GUI世界(Find/undo事件)

这篇博客介绍了如何使用Python的tkinter库创建GUI应用,重点在于实现文本框内的查找文字功能以及内容撤销(undo)功能。通过三个具体的应用案例,详细展示了在文本框控件中查找文字、开启undo功能以及点击按钮逐个字母撤销的操作过程。

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

Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(Find/undo事件)

导读
动态演示调用python库的tkinter带你进入GUI世界(Find/undo事件)

目录

tkinter应用案例—Find/undo事件

1、tkinter应用案例:在文本框控件内查找想要的文字

2、tkinter应用案例:在文本框控件内增加文本内容撤销(打开undo功能)功能

3、tkinter应用案例:对文本框的内容实现点击按钮逐个字母撤销


tkinter应用案例—Find/undo事件

1、tkinter应用案例:在文本框控件内查找想要的文字

7bdb94a5a1e30e1bd4f1394879c2ef6b.png

76677a1569f33c7a839b914688b7de42.png

#tkinter应用案例:在文本框控件内查找想要的文字
from tkinter import *
import hashlib  
 
root = Tk()
root.title("Jason niu工作室")
theLabel=tk.Label(root,text="进入GUI世界,请开始你的表演!n(点击下边链接即可访问我们官方网站)")  
theLabel.grid(row=1,column=0)
text = Text(root,width=40,height=5)
text.grid(row=2,column=0)

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

Label(root,text="请输入要查找的内容:").grid(row=3,column=0)
e1=Entry(root)
e1.grid(row=4,column=0,padx=10,pady=5)
CZ="niu"   

def getIndex(text,index): 
    return tuple(map(int,str.split(text.index(index),"."))) 

start = "1.0"  
while True: 
    pos = text.search(CZ,start,stopindex=END) 
    if not pos:  
        break    
    print ("找到啦,位置是:" + str(getIndex(text,pos))) 
    start = pos + "+1c"  

mainloop()

76677a1569f33c7a839b914688b7de42.png

2、tkinter应用案例:在文本框控件内增加文本内容撤销(打开undo功能)功能

503c41e46a06dd9566aa09ebae471f02.png

76677a1569f33c7a839b914688b7de42.png

#tkinter应用案例:在文本框控件内增加文本内容撤销(打开undo功能)功能
from tkinter import *
import hashlib  
 
root = Tk()
root.title("Jason niu工作室")
theLabel=tk.Label(root,text="进入GUI世界,请开始你的表演!n(点击下边链接即可访问我们官方网站)")  
theLabel.pack()

text = Text(root,width=40,height=5,undo=True)  
text.pack()

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

def show():
    text.edit_undo()

Button(root,text="撤销",command=show).pack()  

mainloop()

76677a1569f33c7a839b914688b7de42.png

3、tkinter应用案例:对文本框的内容实现点击按钮逐个字母撤销

eb255927a7bb5238d463ccde6396f34b.gif

76677a1569f33c7a839b914688b7de42.png

#tkinter应用案例:对文本框的内容实现点击按钮逐个字母撤销
from tkinter import *
import hashlib 
 
root = Tk()
root.title("Jason niu工作室")
theLabel=tk.Label(root,text="进入GUI世界,请开始你的表演!n(点击下边链接即可访问我们官方网站)")  
theLabel.pack()

text = Text(root,width=40,height=5,undo=True,autoseparators=False) 
text.pack()

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

def callback(event):
    text.edit_separator() 

text.bind('<Key>',callback) 

def show():
    text.edit_undo()

Button(root,text="撤销",command=show).pack()

mainloop()

76677a1569f33c7a839b914688b7de42.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值