[Python] 纯文本查看 复制代码
import time
import random
import openpyxl
import threading
import tkinter as tk
from tkinter import messagebox
from pypinyin import pinyin
class xieyingeng(tk.Tk):
'''谐音梗'''
def __init__(self):
super().__init__()
# 多线程先加载打开词库,不影响窗口打开
threading.Thread(target=self.ciku).start()
# 运行窗口
self.window()
# 预设参数
self.ws = ''
self.lst_chazhao = []
self.zanting = 1
def window(self):
# 窗口名
self.title('谐音梗取名')
# 窗口大小
self.geometry('400x600')
# 标签、输入框
tk.Label(self, text='您的姓氏:').place(x=5, y=5)
self.entry_nindexingshi = tk.Entry(self, width=15)
self.entry_nindexingshi.place(x=65, y=5)
tk.Label(self, text='排除姓氏:').place(x=5, y=30)
self.entry_paichuxingshi = tk.Entry(self, width=15)
self.entry_paichuxingshi.place(x=65, y=30)
tk.Label(self, text='(多个之间用“|”隔开)').place(x=175, y=30)
# 单选框
tk.Label(self, text='选择排序:').place(x=5, y=55)
self.selection = tk.StringVar(value="first")
tk.Radiobutton(self, text="第一", variable=self.selection, value="first").place(x=65, y=55)
tk.Radiobutton(self, text="最后", variable=self.selection, value="last").place(x=125, y=55)
tk.Radiobutton(self, text="所有", variable=self.selection, value="all").place(x=185, y=55)
# 按钮
tk.Button(self, text="开始查找", width=26, fg='white', bg='green', command=self.kaishichazhao).place(x=5, y=80)
tk.Button(self, text="暂停查找", width=26, fg='black', bg='red', command=self.zantingchazhao).place(x=200, y=80)
tk.Button(self, text="随机取名", width=54, fg='white', bg='green', command=self.suijimingzi).place(x=5, y=450)
# 文本框
self.text_suoyoumingzi = tk.Text(self, height=25, width=55)
self.text_suoyoumingzi.place(x=5, y=115)
self.text_suijimingzi = tk.Text(self, height=8, width=55)
self.text_suijimingzi.place(x=5, y=485)
# 加载词库标签
self.load_ciku = tk.Label(self, text='稍等5秒,词库加载中...', fg='red')
self.load_ciku.place(x=270, y=5)
def ciku(self):
# 打开表格
wb = openpyxl.load_workbook('词库.xlsx')
self.ws = wb.active
# 更新加载词库标签
self.load_ciku.config(text='词库加载完毕啦!!!', fg='blue')
def kaishichazhao(self):
# 判断是否已经停止查找
if self.zanting != 1:
messagebox.showinfo('提示', '请先暂停查找')
return
# 判断加载词库是否完成
if self.ws == '':
messagebox.showinfo('提示', '词库还在加载中...')
return
# 获取
xingshi = self.entry_nindexingshi.get()
paichuxingshi = self.entry_paichuxingshi.get().split('|')
selection = self.selection.get()
if paichuxingshi == '':
paichuxingshi = 'None'
# 判断是否输入姓氏
if xingshi == '' and len(xingshi) != 1:
messagebox.showinfo('提示', '姓氏未填写或长度不为一')
return
# 重置
self.text_suoyoumingzi.delete('1.0', 'end')
self.lst_chazhao = []
self.zanting = 0
threading.Thread(target=self.thread_messagebox_chazhaozhong).start()
threading.Thread(target=self.thread_kaishichazhao, args=(xingshi, paichuxingshi, selection,)).start()
def thread_kaishichazhao(self, xingshi, paichuxingshi, selection):
if selection == 'first':
index = 1
for ci in self.ws['A']:
if pinyin(xingshi)[0] in pinyin(ci.value[0]) and not ci.value[0] in paichuxingshi:
self.lst_chazhao.append(f'{index}、{ci.value}')
self.text_suoyoumingzi.insert(tk.END, f'{index}、{ci.value}\n')
self.text_suoyoumingzi.see(tk.END)
index += 1
if self.zanting == 1:
return
elif selection == 'last':
index = 1
for ci in self.ws['A']:
if pinyin(xingshi)[0] in pinyin(ci.value[-1]) and not ci.value[-1] in paichuxingshi:
self.lst_chazhao.append(f'{index}、{ci.value}')
self.text_suoyoumingzi.insert(tk.END, f'{index}、{ci.value}\n')
self.text_suoyoumingzi.see(tk.END)
index += 1
if self.zanting == 1:
return
else:
index = 1
for ci in self.ws['A']:
if pinyin(xingshi)[0] in pinyin(ci.value) and set(paichuxingshi).isdisjoint(set(ci.value)):
self.lst_chazhao.append(f'{index}、{ci.value}')
self.text_suoyoumingzi.insert(tk.END, f'{index}、{ci.value}\n')
self.text_suoyoumingzi.see(tk.END)
index += 1
if self.zanting == 1:
return
def thread_messagebox_chazhaozhong(self):
messagebox.showinfo('提示', '正在查找中,请耐心等待...')
def zantingchazhao(self):
self.zanting = 1
def suijimingzi(self):
threading.Thread(target=self.thread_suijimingzi).start()
def thread_suijimingzi(self):
if self.lst_chazhao == []:
messagebox.showinfo('提示', '请先开始查找或未找到符合条件')
return
self.text_suijimingzi.delete('1.0', 'end')
self.text_suijimingzi.insert(tk.END, random.choice(self.lst_chazhao))
if __name__ == '__main__':
app = xieyingeng()
app.mainloop()