吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2075|回复: 39
收起左侧

[Python 原创] 谐音取名【有界面,已打包】

  [复制链接]
xhtdtk 发表于 2024-8-1 01:41
1、先上图

1.jpg

2、起因

带学生上python的暑假班,学生负责出主意,我负责加创意。在github上找到了词库,就开始教导学生制作了。

3、源码


[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()


4、下载


地址:https://www.123pan.com/s/loCcVv-9FrSv.html
视频:https://www.bilibili.com/video/BV1jHvxe7ECw/?vd_source=bdbfa2c9c81f801c03a186f99808e2ab

免费评分

参与人数 5吾爱币 +11 热心值 +3 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
liquidzj + 1 谢谢@Thanks!
yanglinman + 1 谢谢@Thanks!
zhangkaipan + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
PastorBonus + 1 + 1 有趣

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

chenzhuangzi 发表于 2024-8-1 01:45
这个不错 娱乐 起网名可以用
mmcc121 发表于 2024-8-1 04:11
xytwlh 发表于 2024-8-1 06:08
PastorBonus 发表于 2024-8-1 06:16
可以玩一玩
hchw1228 发表于 2024-8-1 08:00
挺有意思,起ID用正好,感谢分享
gaolinfeng 发表于 2024-8-1 08:03
有点意思
moshanglengyang 发表于 2024-8-1 08:11
给游戏ID起名挺好的
ltgb 发表于 2024-8-1 08:38
总结:起网
名和游戏ID
xgzhsd 发表于 2024-8-1 08:42
再把名字长度指定下就更好了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-12-15 04:05

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表