吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2727|回复: 3
收起左侧

[Python 转载] python操作excel实现成语接龙-Gui+pyinstaller打包

  [复制链接]
鸭鸭鸭? 发表于 2021-1-5 17:09
本帖最后由 鸭鸭鸭? 于 2021-1-5 17:35 编辑

吾爱已经有很多类似的工具了,但是写了,还是发出来大家批评一下

一、主要功能:

1.成语解释,输入成语,可以匹配出对应的成语释义;

2.成语接龙,只取输入的最后一个字进行匹配,所以输入一个字还是一个成语不影响接龙;

3.根据输入文字的拼音匹配成语,包含多音字;

4.随机成语,不输入或直接按回车,会随机一个成语。


二、成语文件是自己爬取的,看数量是少了不到100个吧,以下截图。

30893

3万成语

3万成语

excel共3列,成语-拼音-解释。操作excel的时候,只要遍历第一列就好了。

运行截图:

原谅我这辣眼配色

程序截图

程序截图

三、最后附上代码,写的很烂,轻点喷。下个不知道要做什么来练手了。

[Asm] 纯文本查看 复制代码
# -*- coding: utf-8 -*-
import os
import random
import sys
from tkinter import *
import xlrd


# 相对路径转化为绝对路径,网上抄的
def resource_path(relative_path):
    """返回资源绝对路径。"""
    if hasattr(sys, '_MEIPASS'):
        # PyInstaller会创建临时文件夹temp
        # 并把路径存储在_MEIPASS中
        base_path = sys._MEIPASS
    else:
        base_path = os.path.abspath('.')
    return os.path.join(base_path, relative_path)


# excel存放位置,读取第一个sheet
workbook = xlrd.open_workbook(resource_path('./成语大全.xls'))
sheet = workbook.sheet_by_index(0)

# 获取整行和整列的值(数组)
rows = sheet.row_values(0)  # 第一行
cols = sheet.col_values(0)  # 第一列
# 获取excel行数
row = sheet.nrows


def getinput():
    """
    :return: s,p 成语第一个字和拼音列表
    """
    n = 0
    j = 0
    s = inp.get().strip()
    l = []
    for i in cols:
        if s == '':
            s = ''
            p = ''
            return s, p
        elif i != '' and s != '' and i[0] != s[-1]:
            for k in cols:
                if k != '' and s != '' and k[0] == s[-1]:
                    p = sheet.cell(j, 1).value.split()[0]
                    l.append(p)
                j += 1
            break
        n += 1
    # 列表去重
    l = set(l)
    l = list(l)
    return s, l

    # for i in cols:
    #     if s == '':
    #         s = ''
    #         p = ''
    #         return s, p
    #     elif i != '' and s != '' and i[0] != s[-1]:
    #         for k in cols:
    #             if k != '' and s != '' and k[0] == s[-1]:
    #                 p = sheet.cell(j, 1).value.split()[0]
    #                 return s, p
    #             j += 1
    #         p = ''
    #         return s, p
    #     n += 1


def search():
    """
    匹配成语
    """
    s, p = getinput()
    l1 = []
    l2 = []
    l3 = []
    n = 0

    # 清空文本框
    txt1.delete(1.0, END)
    txt2.delete(1.0, END)
    txt4.delete(1.0, END)
    # 行1开始,列0开始
    # 判断文本框内容,超过10行清空文本框
    if txt3.get(11.0) != '':
        txt3.delete(1.0, END)

    # 遍历第一列
    for i in cols:
        pinyin = sheet.cell(n, 1).value.split()[0]
        # 判断输入与第一列是否相等
        if i != '' and s != '' and i == s:
            txt1.insert(END, sheet.row_values(n)[0] + ' ')
            txt1.insert(END, sheet.row_values(n)[1] + ' ')
            txt1.insert(END, sheet.row_values(n)[2])
            l2.append(1)
            txt3.delete(1.0, END)
        # 判断输入的最后一个字与列第一个字是否相等
        elif i != '' and s != '' and i[0] == s[-1]:
            txt2.insert(END, sheet.row_values(n)[0] + ' ')
            txt2.insert(END, sheet.row_values(n)[1] + ' ')
            txt2.insert(END, sheet.row_values(n)[2] + '\n' + '--------------------' + '\n')
            if sheet.row_values(n - 1)[0] == '':
                txt2.insert(END, sheet.row_values(n - 1)[1] + ' ')
                txt2.insert(END, sheet.row_values(n - 1)[2] + '\n' + '--------------------' + '\n')
            l1.append(1)
            txt3.delete(1.0, END)
        # 拼音判断
        elif i != '' and s != '' and pinyin in p and i[0] != s[-1]:
            txt4.insert(END, sheet.row_values(n)[0] + ' ')
            txt4.insert(END, sheet.row_values(n)[1] + ' ')
            txt4.insert(END, sheet.row_values(n)[2] + '\n' + '--------------------' + '\n')
            l3.append(1)
            txt4.see(END)
            txt3.delete(1.0, END)
        n += 1

    # 没有输入,就随机一个成语
    if s == '':
        rn = random.randint(0, row)
        txt3.insert(END, sheet.row_values(rn)[0] + ' ')
        txt3.insert(END, sheet.row_values(rn)[1] + ' ')
        txt3.insert(END, sheet.row_values(rn)[2] + '\n' + '--------------------' + '\n')
        txt3.see(END)

    # 错误提示
    if l1 == [] and s != '' and len(s) < 4:
        txt2.insert(END, '没有以%s结尾的成语' % s[-1] + '\n')
        txt3.delete(1.0, END)
    elif l2 == [] and len(s) >= 4:
        txt1.insert(END, '没有这个成语')
        txt3.delete(1.0, END)


def gettext():
    """
    可变标签
    """
    if inp.get().strip() != '':
        var.set('以%s结尾的成语' % inp.get().strip()[-1])
        inp.delete(0, END)
    else:
        var.set('以 结尾的成语')


def combination():
    """
    组合事件
    """
    search()
    gettext()


def combination1(self):
    """
    用来绑定按键
    """
    search()
    gettext()


# 实例化窗口
root = Tk()
# 屏幕高和宽
screen_height = root.winfo_screenheight() / 1.6
screen_width = root.winfo_screenwidth() / 3.1
root.geometry('%dx%d+600+200' % (screen_height, screen_width))
root.title('成语接龙')
# 窗口图标
root.iconbitmap(resource_path('./rex.ico'))
# 窗体背景色
root['background'] = 'Beige'
# 窗体透明度
# root.attributes('-alpha', 0.8)


# 定义标签动态变化,一个方法,不懂意思,照搬
var = StringVar()
var.set('以 结尾的成语')

# 输入框
inp = Entry(root, cursor='mouse', insertbackground='red', highlightcolor='black', highlightthickness=1)
inp.place(relx=0.15, rely=0.02, relheight=0.05, relwidth=0.3)

# 滚动条
scroll = Scrollbar(root)
scroll.place(relx=0.93, rely=0.28, relheight=0.35)

# 按钮
btn1 = Button(root, text='查询', command=combination)
root.bind('<Return>', combination1)

# 标签
lb1 = Label(root, bg='Beige', text='请输入:')
lb2 = Label(root, bg='Beige', text='成语解释')
lb3 = Label(root, bg='Beige', textvariable=var)
lb4 = Label(root, bg='Beige', text='随机成语')
lb5 = Label(root, bg='Beige', text='尾字音相同的成语')

# 文本框
txt1 = Text(root, font=('', 11), bg='GhostWhite', fg='IndianRed')
txt2 = Text(root, font=('', 11), bg='GhostWhite', fg='MediumVioletRed')
txt3 = Text(root, font=('', 11), bg='GhostWhite', fg='DodgerBlue')
txt4 = Text(root, font=('', 11), bg='GhostWhite', fg='MediumVioletRed')

# 绑定滚动条
scroll.config(command=txt2.yview)
txt2.config(yscrollcommand=scroll.set)

# 以下都是设置位置,大小信息
btn1.place(relx=0.46, rely=0.02, relheight=0.05, relwidth=0.05)

lb1.place(relx=0.066, rely=0.02, relheight=0.05, relwidth=0.08)
lb2.place(relx=0.066, rely=0.11, relheight=0.03, relwidth=0.076)
lb3.place(relx=0.066, rely=0.25, relheight=0.03, relwidth=0.125)
lb4.place(relx=0.066, rely=0.63, relheight=0.03, relwidth=0.076)
lb5.place(relx=0.5, rely=0.62, relheight=0.05, relwidth=0.15)

txt1.place(relx=0.07, rely=0.14, relheight=0.11, relwidth=0.863)
txt2.place(relx=0.07, rely=0.28, relheight=0.35, relwidth=0.863)
txt3.place(relx=0.07, rely=0.66, relheight=0.33, relwidth=0.43)
txt4.place(relx=0.5, rely=0.66, relheight=0.33, relwidth=0.43)

if __name__ == '__main__':
    root.mainloop()
附件:https://cloud.189.cn/t/yaQ3qmemyAJ3(访问码:7ao6)

免费评分

参与人数 4吾爱币 +4 热心值 +4 收起 理由
哥伦布的回声 + 1 + 1 你好,有成语的文件么?
Late_winner + 1 + 1 我很赞同!
hj170520 + 1 + 1 我很赞同!
尚渔 + 1 + 1 谢谢@Thanks!

查看全部评分

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

尚渔 发表于 2021-1-5 17:44
沙发  支持一波
hj170520 发表于 2021-1-5 18:32
寒冰流火 发表于 2021-1-6 07:56
顶一下楼主呗 小有建树了 Python结合Excel一个号思路
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

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

GMT+8, 2024-5-11 01:10

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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