Home > Backend Development > Python Tutorial > Python | Tkinter regular expression tool

Python | Tkinter regular expression tool

Release: 2023-08-09 16:04:10
forward
867 people have browsed it


##In this issue, I will share with you a simple set Tkinter regular expression tool, I hope it will be helpful to you. If you have any questions or areas that need improvement, you can send a private message to the editor.
overall arrangement:
Python | Tkinter regular expression tool

Function code introduction:
## ?️‍? 1. Import tkinter and re modules

from tkinter import *
from tkinter import messagebox, ttk
import re
Copy after login


?️‍? 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)
Copy after login


?️‍? 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)
Copy after login


?️‍? 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)
Copy after login


Python | Tkinter regular expression tool


?️‍? 5. 清空文本框

# 清空三个文本框
def clear_all():
    text1.delete(&#39;1.0&#39;, &#39;end&#39;)
    text2.delete(&#39;1.0&#39;, &#39;end&#39;)
    text3.delete(&#39;1.0&#39;, &#39;end&#39;)
Copy after login


?️‍? 6. 正则匹配结果

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


?️‍? 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)
Copy after login


?️‍? 8. 几个示例

number:

Python | Tkinter regular expression tool


Letters:

Python | Tkinter regular expression tool


#Chinese:

Python | Tkinter regular expression tool


uppercase letter:

Python | Tkinter regular expression tool


You can modify the above regular dictionary according to your own needs.

The above is the detailed content of Python | Tkinter regular expression tool. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Python当打之年
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template