基于Python編寫一個學(xué)生姓名比較工具
本文介紹了一個基于Python Tkinter的學(xué)生姓名比較工具,在班級收集信息有時候是通過在線文檔或者微信群的接龍,會有查漏補缺的需求,一個個去比對太浪費時間,這個時候可以通過程序來實現(xiàn)名單快速的核對。
該工具通過圖形界面提供兩個文本框用于輸入兩組學(xué)生姓名,支持逗號、分號、頓號、回車等多種分隔符。點擊"比較"按鈕后,程序會解析輸入的姓名,統(tǒng)計并顯示兩組學(xué)生總數(shù)、共同擁有的學(xué)生數(shù),以及每組獨有的學(xué)生名單。界面設(shè)計采用網(wǎng)格布局,包含可滾動的文本區(qū)域和結(jié)果展示區(qū),窗口居中顯示且大小可調(diào)。該工具適用于快速對比兩組學(xué)生名單的差異,方便教育工作者進行數(shù)據(jù)核對。
完整代碼
import tkinter as tk
from tkinter import ttk, messagebox
import re
class StudentComparator:
def __init__(self):
self.root = tk.Tk()
self.root.title("學(xué)生姓名比較工具")
self.root.resizable(True, True)
# 設(shè)置窗口居中
self.center_window(900, 700)
# 創(chuàng)建界面
self.create_widgets()
def center_window(self, width, height):
"""將窗口居中顯示"""
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
x = (screen_width - width) // 2
y = (screen_height - height) // 2
self.root.geometry(f"{width}x{height}+{x}+{y}")
def create_widgets(self):
"""創(chuàng)建界面組件"""
# 主框架 - 使用grid布局以便更好地控制各區(qū)域大小
main_frame = ttk.Frame(self.root, padding="10")
main_frame.pack(fill=tk.BOTH, expand=True)
# 配置主框架的網(wǎng)格權(quán)重
main_frame.columnconfigure(0, weight=1)
main_frame.rowconfigure(1, weight=1) # 文本區(qū)域行
main_frame.rowconfigure(3, weight=1) # 結(jié)果區(qū)域行
# 說明標簽
instruction_label = ttk.Label(
main_frame,
text="請在下方兩個文本框中粘貼學(xué)生姓名列表(支持逗號、分號、頓號、回車等分隔符)",
wraplength=800
)
instruction_label.grid(row=0, column=0, sticky="w", pady=(30, 10))
# 創(chuàng)建左右兩個文本框的框架
text_frame = ttk.Frame(main_frame)
text_frame.grid(row=1, column=0, sticky="nsew", pady=(0, 10))
# 配置網(wǎng)格權(quán)重,使兩側(cè)等寬
text_frame.columnconfigure(0, weight=1)
text_frame.columnconfigure(1, weight=1)
text_frame.rowconfigure(0, weight=1)
# 左側(cè)文本框和標簽
left_frame = ttk.Frame(text_frame)
left_frame.grid(row=0, column=0, sticky="nsew", padx=(0, 5))
left_label = ttk.Label(left_frame, text="第一組學(xué)生姓名:")
left_label.pack(anchor=tk.W, pady=(0, 5))
self.left_text = tk.Text(left_frame, wrap=tk.WORD)
self.left_text.pack(fill=tk.BOTH, expand=True)
left_scrollbar = ttk.Scrollbar(left_frame, orient=tk.VERTICAL, command=self.left_text.yview)
left_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.left_text.config(yscrollcommand=left_scrollbar.set)
# 右側(cè)文本框和標簽
right_frame = ttk.Frame(text_frame)
right_frame.grid(row=0, column=1, sticky="nsew", padx=(5, 0))
right_label = ttk.Label(right_frame, text="第二組學(xué)生姓名:")
right_label.pack(anchor=tk.W, pady=(0, 5))
self.right_text = tk.Text(right_frame, wrap=tk.WORD)
self.right_text.pack(fill=tk.BOTH, expand=True)
right_scrollbar = ttk.Scrollbar(right_frame, orient=tk.VERTICAL, command=self.right_text.yview)
right_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.right_text.config(yscrollcommand=right_scrollbar.set)
# 按鈕框架
button_frame = ttk.Frame(main_frame)
button_frame.grid(row=2, column=0, sticky="ew", pady=10)
compare_button = ttk.Button(button_frame, text="比較", command=self.compare_names)
compare_button.pack(side=tk.LEFT, padx=(0, 10))
clear_button = ttk.Button(button_frame, text="清空", command=self.clear_text)
clear_button.pack(side=tk.LEFT)
# 結(jié)果框架 - 增加高度
result_frame = ttk.LabelFrame(main_frame, text="比較結(jié)果", padding="5")
result_frame.grid(row=3, column=0, sticky="nsew", pady=(10, 0))
# 配置結(jié)果框架內(nèi)部的網(wǎng)格權(quán)重
result_frame.columnconfigure(0, weight=1)
result_frame.rowconfigure(0, weight=1)
self.result_text = tk.Text(result_frame, wrap=tk.WORD, state=tk.DISABLED, font=("Arial", 10))
self.result_text.grid(row=0, column=0, sticky="nsew")
result_scrollbar = ttk.Scrollbar(result_frame, orient=tk.VERTICAL, command=self.result_text.yview)
result_scrollbar.grid(row=0, column=1, sticky="ns")
self.result_text.config(yscrollcommand=result_scrollbar.set)
def parse_names(self, text):
"""解析文本中的姓名,支持多種分隔符"""
# 使用正則表達式分割文本,支持中文標點和換行符
separators = r'[,,;;、\s\n]+'
names = re.split(separators, text.strip())
# 過濾空字符串并去除前后空格
names = [name.strip() for name in names if name.strip()]
return set(names)
def compare_names(self):
"""比較兩個文本框中的姓名"""
left_text = self.left_text.get("1.0", tk.END)
right_text = self.right_text.get("1.0", tk.END)
if not left_text.strip() and not right_text.strip():
messagebox.showwarning("警告", "兩個文本框都為空,請輸入學(xué)生姓名!")
return
# 解析姓名
left_names = self.parse_names(left_text)
right_names = self.parse_names(right_text)
# 找出不同的姓名
only_in_left = left_names - right_names
only_in_right = right_names - left_names
common_names = left_names & right_names
# 顯示結(jié)果
self.result_text.config(state=tk.NORMAL)
self.result_text.delete("1.0", tk.END)
self.result_text.insert(tk.END, f"第一組學(xué)生總數(shù): {len(left_names)}\n")
self.result_text.insert(tk.END, f"第二組學(xué)生總數(shù): {len(right_names)}\n")
self.result_text.insert(tk.END, f"共同擁有的學(xué)生數(shù): {len(common_names)}\n\n")
self.result_text.insert(tk.END, "僅在第一組中的學(xué)生:\n")
if only_in_left:
for name in sorted(only_in_left):
self.result_text.insert(tk.END, f" ? {name}\n")
else:
self.result_text.insert(tk.END, " 無\n")
self.result_text.insert(tk.END, "\n僅在第二組中的學(xué)生:\n")
if only_in_right:
for name in sorted(only_in_right):
self.result_text.insert(tk.END, f" ? {name}\n")
else:
self.result_text.insert(tk.END, " 無\n")
self.result_text.config(state=tk.DISABLED)
def clear_text(self):
"""清空所有文本框"""
self.left_text.delete("1.0", tk.END)
self.right_text.delete("1.0", tk.END)
self.result_text.config(state=tk.NORMAL)
self.result_text.delete("1.0", tk.END)
self.result_text.config(state=tk.DISABLED)
def run(self):
"""運行程序"""
self.root.mainloop()
if __name__ == "__main__":
app = StudentComparator()
app.run()結(jié)果圖如下

到此這篇關(guān)于基于Python編寫一個學(xué)生姓名比較工具的文章就介紹到這了,更多相關(guān)Python學(xué)生姓名比較內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch之關(guān)于PyTorch結(jié)構(gòu)介紹
這篇文章主要介紹了pytorch之關(guān)于PyTorch結(jié)構(gòu)的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
快速上手基于Anaconda搭建Django環(huán)境的教程
Django具有完整的封裝,開發(fā)者可以高效率的開發(fā)項目,Django將大部分的功能進行了封裝,開發(fā)者只需要調(diào)用即可,接下來通過本文給大家介紹基于Anaconda搭建Django環(huán)境的教程,需要的朋友可以參考下2021-10-10
PyTorch中的nn.ConvTranspose2d模塊詳解
nn.ConvTranspose2d是PyTorch中用于實現(xiàn)二維轉(zhuǎn)置卷積的模塊,廣泛應(yīng)用于生成對抗網(wǎng)絡(luò)(GANs)和卷積神經(jīng)網(wǎng)絡(luò)(CNNs)的解碼器中。該模塊通過參數(shù)如輸入輸出通道數(shù)、卷積核大小、步長、填充等,能控制輸出尺寸和避免棋盤效應(yīng)2024-09-09

