由于对网络这一部分不太了解,之前虽然接触过爬虫,但是对于接口接入和JS研究的还不够深入,这里就简单的调用别人写好的接口,运用到界面上,后期有时间再去学习一下这方面的知识。
2.设计
首先介绍一下整体设计,这里在之前的界面控制基础上添加了置顶功能,和界面透明功能,这样就可以让工作置顶的显示出来,透明是为了不妨碍用户的其他界面使用。
def window_init(): # Main window config translateWindow.title("EZ Translate Tool") translateWindow.geometry("300x200") translateWindow.resizable(0, 0) # not allow set window height and width translateWindow.wm_attributes('-topmost', True) # default window top translateWindow.attributes("-alpha", 0.8) framMain.pack() txtSource.pack(side="top") framConfig.pack() btnToChTranslate.pack(side="left") checkTop.pack(side="right") btnToEnTranslate.pack(side="right") txtResult.pack(side="bottom") pass
复制代码def txt_source_event(event): check_chinese = txtSource.get(1.0, "end") # print("txtLen:{}".format(len(check_chinese))) if len(check_chinese) > 1: # not null char if check_contain_chinese(check_chinese): ch_translate_en_opt() pass else: en_translate_cn_opt() pass pass else: # print("null char") pass pass
复制代码def en_translate_cn_opt(): # translate repeat check global translateStrLast currentStr = txtSource.get(1.0, "end") # read source data if currentStr == translateStrLast: return translateStrLast = currentStr txtResult.delete(1.0, "end") txtResult.insert("insert", "翻译中...") en_translate_cn_timer = threading.Timer(0.1, en_translate_cn_run) en_translate_cn_timer.start() pass
复制代码图1
我们来看一下效果把。
图2
图3
3.总结
其实这个小工具不难,难得在于如何调用谷歌翻译的接口,这个后续等我学完了之后再给大家来一个讲解,如果有会的童鞋也可以帮忙讲解一下,下面放出源码。
游客,如果您要查看本帖隐藏内容请回复