python操作excel实现成语接龙-Gui+pyinstaller打包
本帖最后由 鸭鸭鸭? 于 2021-1-5 17:35 编辑吾爱已经有很多类似的工具了,但是写了,还是发出来大家批评一下
一、主要功能:
1.成语解释,输入成语,可以匹配出对应的成语释义;
2.成语接龙,只取输入的最后一个字进行匹配,所以输入一个字还是一个成语不影响接龙;
3.根据输入文字的拼音匹配成语,包含多音字;
4.随机成语,不输入或直接按回车,会随机一个成语。
二、成语文件是自己爬取的,看数量是少了不到100个吧,以下截图。
30893
excel共3列,成语-拼音-解释。操作excel的时候,只要遍历第一列就好了。
运行截图:
原谅我这辣眼配色
三、最后附上代码,写的很烂,轻点喷。下个不知道要做什么来练手了。
# -*- 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 != s[-1]:
for k in cols:
if k != '' and s != '' and k == s[-1]:
p = sheet.cell(j, 1).value.split()
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 != s[-1]:
# for k in cols:
# if k != '' and s != '' and k == s[-1]:
# p = sheet.cell(j, 1).value.split()
# 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()
# 判断输入与第一列是否相等
if i != '' and s != '' and i == s:
txt1.insert(END, sheet.row_values(n) + ' ')
txt1.insert(END, sheet.row_values(n) + ' ')
txt1.insert(END, sheet.row_values(n))
l2.append(1)
txt3.delete(1.0, END)
# 判断输入的最后一个字与列第一个字是否相等
elif i != '' and s != '' and i == s[-1]:
txt2.insert(END, sheet.row_values(n) + ' ')
txt2.insert(END, sheet.row_values(n) + ' ')
txt2.insert(END, sheet.row_values(n) + '\n' + '--------------------' + '\n')
if sheet.row_values(n - 1) == '':
txt2.insert(END, sheet.row_values(n - 1) + ' ')
txt2.insert(END, sheet.row_values(n - 1) + '\n' + '--------------------' + '\n')
l1.append(1)
txt3.delete(1.0, END)
# 拼音判断
elif i != '' and s != '' and pinyin in p and i != s[-1]:
txt4.insert(END, sheet.row_values(n) + ' ')
txt4.insert(END, sheet.row_values(n) + ' ')
txt4.insert(END, sheet.row_values(n) + '\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) + ' ')
txt3.insert(END, sheet.row_values(rn) + ' ')
txt3.insert(END, sheet.row_values(rn) + '\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) 沙发支持一波 秀啊,想象力丰富!{:301_978:} 顶一下楼主呗 小有建树了 Python结合Excel一个号思路
页:
[1]