您的位置:首页 > 编程语言 > Python开发

python实现函数变量命名翻译工具 源码及工具下载

2020-07-14 06:11 274 查看

介绍

如题,为了解决一命名就词穷的问题,想搞一搞命名工具,正好刚接触python(2019年冬),就练练手,顺便爬一下有道翻译(爬有道翻译网上有教程,一搜一把,但我这个是最简单的版本),先看下效果。





大概就是这么个鬼东西,丑了点,但是我用了大半年,呵呵呵呵呵呵。。。然后有了他的升级版,详情请看我的另一篇文章:传送门

源码解析

1.首先,要做图形化界面必须安装tkinter,安装命令:

pip3 install tkinter

2.界面很简单,两个输入框,from负责输入要翻译的内容,to负责输出翻译结果,一个下拉列表负责控制命名规范,置顶按钮、清除按钮、复制按钮:

# 初始化
window = tkinter.Tk()
# 设置标题
window.title('翻译助手')
# 设置图标
window.iconbitmap(resource_path(os.path.join("ico","ico.ico")))
# 设置大小
window.geometry('300x120')
# 置顶窗口
window.wm_attributes('-topmost', True)
# 禁止缩放
window.resizable(width=False, height=False)
# 定义一个输入框from
from_value = tkinter.StringVar()
from_entry = tkinter.Entry(window, width=25, font=('宋体', 16), textvariable=from_value)
from_entry.place(x=10, y=10)

# 回车事件
def printkey(event):
if '\r' == event.char:
type_combobox_change_event()

# 为输入框绑定回车事件
from_entry.bind('<Key>', printkey)

# 定义一个输入框to
to_value = tkinter.StringVar()
to_entry = tkinter.Entry(window, width=25, font=('宋体', 16), textvariable=to_value)
to_entry.place(x=10, y=55)

# 下拉列表改变事件
def type_combobox_change_event(*args):
to_value.set(get_translate_date(from_value.get(), type_combobox.get()))
from_entry.focus_set()

# 定义一个下拉列表
type_combobox = tkinter.ttk.Combobox(window, width=10, font=('宋体', 12), state='readonly')
type_combobox['value'] = ("camelCase", "CamelCase", "lower_case", "UPPER", "normal")
# 为下拉列表绑定下拉列表改变事件
type_combobox.bind("<<ComboboxSelected>>", type_combobox_change_event)
# 下拉列表的默认值
type_combobox.current(0)
type_combobox.place(x=10, y=90)

def copy():
pyperclip.copy(to_value.get())
pyperclip.paste()
from_entry.focus_set()

def clear():
from_value.set("")
to_value.set("")
from_entry.focus_set()

copy_btn = tkinter.Button(window, text="复制", font=('宋体', 10), command=copy)
copy_btn.place(x=252, y=90)
copy_btn = tkinter.Button(window, text="清空", font=('宋体', 10), command=clear)
copy_btn.place(x=197, y=90)

# 翻译按钮,太丑,删掉了,改用回车触发
# def translate():
#     type_combobox_change_event()
#
#
# always_top_btn = tk.Button(window, text='点我翻译', width=45, font=('宋体', 8), command=translate)
# always_top_btn.place(x=10, y=37)

# 置顶按钮点击事件
def chang_top_status():
if always_top_btn['text'] == '置顶了':
window.wm_attributes('-topmost', False)
always_top_btn['text'] = '没置顶'
else:
window.wm_attributes('-topmost', True)
always_top_btn['text'] = '置顶了'

always_top_btn = tkinter.Button(window, text='置顶了', font=('宋体', 10), command=chang_top_status)
always_top_btn.place(x=130, y=90)

# 初始化焦点
from_entry.focus_set()
# 显示
window.mainloop()

3.翻译接口,word就是你要翻译的内容了,结果通过解析后的翻译结果在text里

# url地址
url = 'http://fanyi.youdao.com/translate'
# 发送的数据
from_data = {'i': word, 'from': 'AUTO', 'to': 'AUTO','doctype': 'json'}
# 请求表单数据
response = requests.post(url, data=from_data)
# 将Json格式字符串转字典
content = json.loads(response.text)
# 翻译后的结果
text = content['translateResult'][0][0]['tgt']

4.命名规范处理,就是将每个词汇的大小写转换一下,或者加个下划线,这个不难

# 如果是名词一般第一个字母是the,去掉这个the
if text[0:4].lower() == 'the ':
text = text[4:len(text)]
if translate_type == 'CamelCase':
text = text.title()
text = text.replace(" ", "")
if translate_type == 'camelCase':
text = text.title()
text = text.replace(" ", "")
text = text[0:1].lower() + text[1:len(text)]
elif translate_type == 'lower_case':
text = text.lower()
text = text.replace(" ", "_")
elif translate_type == 'UPPER':
text = text.upper()
text = text.replace(" ", "_")
# elif type == 'normal': do nothing

5.工具下载地址

6.至此就完成了,他除了帮助你命名,平时做一些小的翻译也是可以的,赶紧改改你们的代码吧,规范命名你我他,点个赞再走哇!!!

7.源码地址

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: