Python | Tkinter正则表达式工具

简介: Python | Tkinter正则表达式工具

大家好,我是欧K~

本期给大家分享一套简易Tkinter正则表达式工具,希望对你有所帮助,如有疑问或者需要改进的地方可以私信小编。整体布局:

功能代码介绍:

🏳️‍🌈 1. 导入tkinter和re模块

from tkinter import *
from tkinter import messagebox, ttk
import re


🏳️‍🌈 2. 设置窗口居中

# 窗口居屏幕中央
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2)
root.geometry(alignstr)


🏳️‍🌈 3. 设置lable、text、button布局

font_size = 10
label1 = Label(root, text="常用正则表达式:",font=(StringVar(), font_size),pady=10)
label1.grid(row=1, column=0, sticky=W,padx=5, pady=5)
# 功能按钮
button2 = Button(root, width=15, text="测试", command=check_regex)
button3 = Button(root, width=15, text="清空所有", command=clear_all)
button2.grid(row=5, column=1, padx=5, pady=5)
button3.grid(row=5, column=2, padx=5, pady=5)
label2 = Label(root, text="正则表达式:", font=(StringVar(), font_size), pady=10)
label3 = Label(root, text="目标字符串:", font=(StringVar(), font_size), pady=10)
label4 = Label(root, text="匹配结果:", font=(StringVar(), font_size), pady=10)
label2.grid(row=2, column=0, sticky=W, padx=5)
label3.grid(row=3, column=0, sticky=W, padx=5)
label4.grid(row=4, column=0, sticky=W, padx=5)
# 设置默认项
text1 = Text(root, width=47, height=5, font=(StringVar(), font_size))
text1.insert('1.0', dic_tmp[comboxlist.get()])
text2 = Text(root, width=47, height=10, font=(StringVar(), font_size))
text3 = Text(root, width=47, height=10, font=(StringVar(), font_size))
text1.grid(row=2, column=1,columnspan=3, pady=10)
text2.grid(row=3, column=1,columnspan=3, pady=10)
text3.grid(row=4, column=1,columnspan=3, pady=10)


🏳️‍🌈 4. 设置下拉列表框

# 下拉选择框
def set_combox():
    comboxlist["values"] = list(dic_tmp.keys())
    comboxlist.current(0)
    comboxlist.bind("<<ComboboxSelected>>", get_combox)
    comboxlist.grid(row=1, column=1, pady=10)



🏳️‍🌈 5. 清空文本框

# 清空三个文本框
def clear_all():
    text1.delete('1.0', 'end')
    text2.delete('1.0', 'end')
    text3.delete('1.0', 'end')


🏳️‍🌈 6. 正则匹配结果

# 正则匹配
def check_regex():
    text3.delete('1.0', 'end')
    re_text = text1.get('1.0', '1.end')
    source_text = text2.get('1.0', END)
    try:
        pattern = re.compile(f'{re_text}')
        result = re.findall(pattern, source_text)
        if result:
            # 循环输出
            for res in result:
                if res != '':
                    text3.insert(END, res+'\n')
        else:
            text3.delete('1.0', 'end')
            text3.insert('insert', '匹配结果为空')
    except:
        text3.delete('1.0', 'end')
        text3.insert('insert', '匹配失败')


🏳️‍🌈 7. 设置滚动条

# 创建滚动条
scroll2 = Scrollbar(orient=VERTICAL,command=text2.yview)
text2.config(yscrollcommand = scroll2.set)
scroll2.grid(row=3,column=4, pady=10, sticky=S+W+E+N)
# 创建滚动条
scroll3 = Scrollbar(orient=VERTICAL,command=text3.yview)
text3.config(yscrollcommand = scroll3.set)
scroll3.grid(row=4,column=4, pady=10, sticky=S+W+E+N)


🏳️‍🌈 8. 几个示例

数字:


字母:


中文:


大写字母:


以上正则字典大家可以根据自己的需求进行修改。


🏳️‍🌈 更多内容

源码下载 | Python可视化系列文章资源(源码+数据)

网盘链接:https://pan.baidu.com/doc/share/Olj4d~aKuXT7AF0cq01MrQ-437060019167360 提取码: pyra


END


以上就是本期为大家整理的全部内容了,喜欢的朋友可以点赞、点在看也可以分享让更多人知道。

相关文章
|
11天前
|
Python
Python 内置正则表达式库re的使用
正则表达式是记录文本规则的代码,用于查找和处理符合特定规则的字符串。在Python中,常通过原生字符串`r&#39;string&#39;`表示。使用`re.compile()`创建正则对象,便于多次使用。匹配字符串有`match()`(从开头匹配)、`search()`(搜索首个匹配)和`findall()`(找所有匹配)。替换字符串用`sub()`,分割字符串则用`split()`。
29 3
|
4天前
|
Python
python tkinter 界面倒计时(用于监督页面返回)
python tkinter 界面倒计时(用于监督页面返回)
python tkinter 界面倒计时(用于监督页面返回)
|
5天前
|
数据安全/隐私保护 Python
Python进阶---正则表达式
Python进阶---正则表达式
10 2
|
8天前
|
程序员 开发者 Python
Python中的装饰器:优雅而强大的函数修饰工具
在Python编程中,装饰器是一种强大的工具,它可以简洁地实现函数的增强、扩展和重用。本文将深入探讨Python中装饰器的工作原理、常见应用场景以及如何自定义装饰器,帮助读者更好地理解和运用这一重要的编程概念。
|
9天前
|
SQL 物联网 关系型数据库
sqlmap工具的使用 (超详细附工具版)_python sqlmap
sqlmap工具的使用 (超详细附工具版)_python sqlmap
|
9天前
|
数据采集 Python
python中的正则表达式,Python实习面试经验汇总
python中的正则表达式,Python实习面试经验汇总
|
10天前
探索正则表达式:强大文本匹配与处理工具
探索正则表达式:强大文本匹配与处理工具
|
11天前
|
Python
python正则表达式小结
1. **其他函数:**  `re`模块还提供了其他一些函数,例如 `re.search()`查找字符串中第一个匹配的部分,`re.findall()`查找所有匹配的部分,`re.sub()`替换匹配的部分。
24 0
|
11天前
|
机器学习/深度学习 Python
正则表达式(Regular Expression,常简写为regex或regexp)是一种强大的文本处理工具
【5月更文挑战第12天】正则表达式是文本处理工具,Python的re模块支持其使用。元字符如.、*、+、?等在正则表达式中具有特殊含义,用于指定匹配规则。示例中,通过正则表达式模式匹配字符串中的电子邮件地址,并使用re.findall()找出所有匹配项。
27 4
|
11天前
|
SQL 测试技术 网络安全
Python之SQLMap:自动SQL注入和渗透测试工具示例详解
Python之SQLMap:自动SQL注入和渗透测试工具示例详解
32 0