需要Python环境与customtkinter(Python的GUI库)运行,否则请自行下载.exe版本(现在没有)
本次对该文章进行更新时,挂的还是maliang库的版本
安装库:pip install 库名称(在python环境已经安装,且安装时装了pip的)
安装python: https://docs.python.org/zh-cn/3/using/index.html
maliang库:https://github.com/Xiaokang2022/maliang(该项目曾经使用,非常好用!现在暂时还是挂使用该库编写的版本)
maliang库官方教程:https://xiaokang2022.github.io/maliang-docs/3.0/tutorials/chapter_01/
customtkinter库官网:https://customtkinter.tomschimansky.com
源代码:
import maliang as m
from maliang.core import configs
import maliang.toolbox as toolbox
from difflib import get_close_matches
from collections import Counter
# 状态变量
page = 1
btp_1 = None # 主界面按钮
btp_2 = None # 页面2按钮
btp_3 = None
title = None # 主界面标题
search_box = None # 搜索页标题
subtitle = None # 版本标题
sort_btn = None
search_btn = None
btnh = None
search_key = None
text_hint_label = None
search_box = None
val_S_B_IS_O_C = None
orange_swich = None
list_msTcn = []
def count_common_items(str_a, str_b):
# 统计字符串B中每个字符的出现次数
counter_b = Counter(str_b)
count = 0
# 遍历字符串A中的字符
for char in str_a:
if counter_b.get(char, 0) > 0:
count += 1
counter_b[char] -= 1 # 减少B中该字符的计数
return count
def search(query,thelist:list,cutoff=0.6):
tp = ''
tp_index = 0
matches = get_close_matches(query,thelist,cutoff=cutoff)
if matches:
tp = matches[0]
tp_index = thelist.index(tp)
return tp, tp_index
def lookup(b):
if not b == '':
wt = 0
ind_phr = 0
extra = ''
tp = None
wt = None
phr = None
phr_cn = None
bool_phr_is_cn = 0
bool_b_is_cn = 0
# 如果是汉字
if 'A'<=b[0]<='z':
TP_INDEX_tuple = search(b,valuelist,0.6)
else:
TP_INDEX_tuple = search(b,keylist,0.6)
bool_b_is_cn = 1
if TP_INDEX_tuple: # 如果查出了值
if 'A' <= b[0] <='z':
tp_index = TP_INDEX_tuple[1]
tp = keylist[tp_index] # tp:被查出来的词语
else:
tp_index = TP_INDEX_tuple[1]
tp = valuelist[tp_index]
# 拼写是否错误判断
if b != valuelist[tp_index]:
extra = f'您的拼写可能有问题,正确拼写应为{valuelist[tp_index]}'
if 'A' <= tp <= 'z':
INDPHRtuple = search(tp,phraseslist,0.6)
else:
INDPHRtuple = search(tp,cn_phraseslist,0.6)
if INDPHRtuple: # 如果查出了值
if bool_phr_is_cn:
phr_cn ,ind_phr = INDPHRtuple[0],INDPHRtuple[1] # 与 tp 相关的词组(汉语)
phr = phraseslist[ind_phr]
else:
phr, ind_phr = INDPHRtuple[0],INDPHRtuple[1] # 与 tp 相关的词组
phr_cn = phraseslist[ind_phr]
wt = wordtypelist[tp_index] # tp 的词性
# 如果有搜索结果
if bool_b_is_cn:
ttp = search(b,keylist)[0]
else:
ttp = search(b,valuelist)[0]
if tp:
return create_example_sentence(tp,ttp,wt),extra,phr_cn,phr,wt,tp
else:
return "无无无无无 "
# 如果是初始化
elif b == '':
return ' '
def count_common_items(str_a, str_b):
from collections import Counter
# 统计字符串B中每个字符的出现次数
counter_b = Counter(str_b)
count = 0
# 遍历字符串A中的字符
for char in str_a:
if counter_b.get(char, 0) > 0:
count += 1
counter_b[char] -= 1 # 减少B中该字符的计数
return count
def create_example_sentence(word,word_2,wty):
if wty == 'ko':
if not 'A' <= word <= 'z':
word,word_2 = word_2,word
else:
return '暂无'
return f'Ber ini na {word}?\n例句意思为:你有{word_2}吗?'
####以上粘贴
def search_btnISon_click():
global aaa
aaa = search_box.get()
list_text = list(lookup(aaa))
betterLabel((110,610),(1820,680),list_text)
def betterLabel(the_position,the_size,text_list):
global empty_label,textOntheLabel
empty_label = m.Label(cvr, position= the_position, size= the_size)
textOntheLabel = m.Text(cvr, position=(130,620),
text=f"对应词汇:{text_list[5]}\n词性:{text_list[4]}\n相关词组:{text_list[2]}\n该词组意思:{text_list[3]}\n例句:{text_list[0]}\n{text_list[1]}"
,fontsize=70)
add_rw(empty_label)
add_rw(textOntheLabel)
def theinit():
'''初始化'''
# 设置字体
if toolbox.load_font("with_python/mesi_polo_P/fonts/LXGWWenKai-Regular.ttf"):
configs.Font.family = "LXGW WenKai"
def add_lw(widget):
'''添加左侧控件'''
lw.append(widget)
return widget
def add_rw(widget):
'''添加右侧控件'''
rw.append(widget)
return widget
def clear_canvas():
for widget in rw:
if widget and hasattr(widget, 'destroy'):
try:
widget.destroy()
except:
pass
rw.clear()
def createleft():
global btp_2,title,subtitle,title_main,btph,btp_3
btp_2 = m.IconButton(cvl, (70, 670), text=" 翻译", command=gtp2,
size=(500, 100), fontsize=60,image= m.PhotoImage(file=r"with_python\mesi_polo_P\images\2(1)(1).png"))
add_lw(btp_2)
title_main= m.Text(cvl, (70, 170), text="米水语大辞典",
fontsize=100)
add_lw(title_main)
subtitle = m.Text(cvl, (70, 320), text=" - version - 0.2.2",
fontsize=60)
add_lw(subtitle)
btph = m.IconButton(cvl,(70,540),text=" 登录/注册",size=(500,100),fontsize=60,command=gtp1)
#thell = m.Text(cvr,(0,0),text="ll") 调试左右画布分界线的工具
btp_3 = m.Button(cvl,(70,800),text='所有已收录词汇',size=(500,100),fontsize=60,command=gtp3)
def createp2():
clear_canvas()
global btp_1,title,search_btn,search_box,sort_btn,text_hint_label,aaa
# 页面2返回按钮
btp_1 = m.Button(cvr, (2800-lc, 50), text="Back", command=gtp1,
size=(130, 80), fontsize=50, anchor="ne")
add_rw(btp_1)
# 页面2的标题
title = m.Label(cvr, (size[0]//2-lc, 200), text="翻译页面",
fontsize=80, anchor="center")
add_rw(title)
# 米水词汇输入
search_box = m.InputBox(cvr, (800-lc, 400), placeholder="输入要翻译的米水/中文词汇,多个词汇以逗号分隔",
fontsize=40,size=(1300,70))
add_rw(search_box)
# 搜索按钮
search_btn = m.Button(cvr, (2110-lc, 400), text="开始翻译",
size=(200, 70), fontsize=40,command=search_btnISon_click)
add_rw(search_btn)
sort_btn = m.ComboBox(cvr,(1630,400),text=("模糊搜索","精确搜索"),size= (300,70),fontsize=32)
add_rw(sort_btn)
text_hint_label = m.Text(cvr,(110,475),text="翻译结果:(ko=名词,ka=动词,kiu=形容词和副词,koler=代词,koos=介词,\nkei=助词和疑问词),开始前需点击“开始翻译”按钮",fontsize=50)
add_rw(text_hint_label)
def createhomepage():
clear_canvas()
# 声明全局变量
global t_h,usernametext,usernameinput,passwordtext,passwordinput,loginbtn,logoutbtn,aaa
# 上方登录提示
t_h = m.Text(cvr,position=(600,200),fontsize=100,text="登录您的米水账号")
add_rw(t_h)
#用户名提示
usernametext = m.Text(cvr,(660,500),fontsize=50,text="用户名")
add_rw(usernametext)
#用户名输入框
usernameinput = m.InputBox(cvr,(660,590),placeholder="请输入用户名",size=(600,80),fontsize=35)
add_rw(usernameinput)
# 密码提示
passwordtext = m.Text(cvr,(660,700),text="密码",fontsize=50)
add_rw(passwordtext)
# 密码输入框
passwordinput = m.InputBox(cvr,(660,790),placeholder="请输入密码",size=(600,80),fontsize=35)
add_rw(passwordinput)
# 登录按钮
loginbtn = m.Button(cvr,(660,1100),size=(300,100),text="登录",fontsize=40)
add_rw(loginbtn)
# 注册按钮
logoutbtn = m.Button(cvr,(980,1100),size=(300,100),text="注册",fontsize=40,command=gtp3)
add_rw(logoutbtn)
def createp3():#备用
pass
def createp5():
pass
def gtp1():
global page
page = 1
createhomepage()
def gtp2():
#切换到页面2
global page
page = 2
createp2()
def gtp3():
#切换到页面3
global page
page = 3
createp3()
def gtp4():
global page
page = 4
def gtp5():
global page
page = 5
# 初始化
size = (2880, 1920)
lc = 690
root = m.Tk(size, title="米水大词典")
cvr = m.Canvas(auto_zoom=True)#右侧动态画布
cvr.place(width=2880-lc, height=size[1],x = lc)
cvl = m.Canvas(auto_zoom=True)#左侧静态画布
cvl.place(width= lc,height=1920)
# 左侧静态画布
lw = []
# 右侧动态画布
rw = []
theinit()
createleft()
createp2()
with open('with_python/mesi_polo_P/for_mesi/keys.csv' , 'r' , encoding = 'utf-8' ) as f:
keylist = list(f.read().strip().split(','))
with open('with_python/mesi_polo_P/for_mesi/values.csv' , 'r' , encoding = 'utf-8' ) as f:
valuelist = list(f.read().strip().split(','))
with open('with_python/mesi_polo_P/for_mesi/wordtype.csv' , 'r' , encoding = 'utf-8' ) as f:
wordtypelist = list(f.read().strip().split(','))
with open('with_python/mesi_polo_P/for_mesi/phrases.csv' , 'r' , encoding = 'utf-8' ) as f:
phraseslist = list(f.read().strip().split(','))
with open('with_python/mesi_polo_P/for_mesi/cn_phrases.csv' , 'r' , encoding = 'utf-8' ) as f:
cn_phraseslist = list(f.read().strip().split(','))
root.mainloop()需要可以自行粘贴。
注意:如单独粘贴程序需下载附件!附件下载链接:/upload/2(1)(1).png
下载教学示范:
或者直接下载项目仓库:/upload/mesi_polo_P.zip(点击即可)。源码还未更新,等寒假
注!!!!!现在还未完成,仓库里没有.exe文件,除非你有python解释器,否则打不开!