这个软件是一个英语小助手应用程序,是我为了帮助自己背单词写的,用的Python。
这款软件可以方便以下类型的用户:
-
英语学习者:这个应用程序可以帮助英语学习者轻松地获取单词的翻译、发音和词源信息,有助于提高英语学习效率。
-
教师和教育工作者:教师可以使用这个应用程序来为学生提供单词的翻译和发音示例,以辅助教学。
-
翻译人员:翻译人员可以使用这个应用程序来快速查找单词的翻译和发音,以支持他们的翻译工作。
-
词汇研究者:对于对英语词汇和词源感兴趣的人来说,这个应用程序提供了方便的词源查看功能,有助于深入研究单词的起源和演变。
-
常用英语用户:即使不是英语学习者,一般用户也可以使用这个应用程序来查询英语单词的翻译和发音,以便在日常生活中更好地理解英语内容。
具有以下功能:
-
单词翻译:用户可以在文本框中输入要翻译的英文单词,然后点击“翻译”按钮或按下Ctrl+A快捷键来获取单词的翻译结果。
-
发音下载:用户可以选择英音或美音,并点击“下载发音”按钮来下载所输入单词的发音文件。发音文件将保存在默认路径或用户选择的路径中。
-
词源查看:用户可以点击“查看词源”按钮或按下Ctrl+Q快捷键来打开网页,以查看输入单词的词源信息。提供了多个在线词典链接,包括Etymonline、剑桥词典和有道词典。
-
清空输入:用户可以点击“清除”按钮或按下Ctrl+Z快捷键来清空输入框中的内容。
-
播放发音:用户可以点击“播放”按钮来播放所输入单词的发音。
-
帮助和赞助作者:在菜单栏中,用户可以找到“关于”选项,其中包括“帮助”和“赞助作者”两个子选项,用于显示帮助文本和作者联系信息。
-
选择默认保存路径:用户可以点击“选择默认保存路径”按钮来选择下载发音文件的默认保存路径。
可执行文件下载链接:英语学习小助手.exe - 蓝奏云
软件截图:
import sys
import requests
import tkinter as tk
from tkinter import filedialog # 导入文件对话框模块
import os
import winsound
from tkinter import IntVar
import re
import webbrowser
import io
import pygame
# 帮助内容
help_text = """\
这是一个发音下载器应用程序。
使用说明:
1. 输入要翻译的单词。
2. 选择发音类型(英音或美音)。
3. 点击“翻译”按钮获取单词翻译 Ctrl+A。
4. 点击“下载发音”按钮下载发音文件 Enter。
5. 点击“查看词源”按钮查看单词词源 Ctrl+Q。
6. 点击“清空”按钮清空输入框和翻译 Ctrl+Z。
"""
# 赞助作者内容
sponsor_text = """\
感谢您的支持!
您可以联系作者:
QQ: 1164334031 """
# 创建帮助窗口
def show_help_window():
help_window = tk.Toplevel(root)
help_window.title("帮助")
help_label = tk.Label(help_window, text=help_text)
help_label.pack(padx=10, pady=10)
# 创建赞助作者窗口
def show_sponsor_window():
sponsor_window = tk.Toplevel(root)
sponsor_window.title("赞助作者")
sponsor_label = tk.Label(sponsor_window, text=sponsor_text)
sponsor_label.pack(padx=10, pady=10)
# 全局变量,用于存储默认保存路径
default_save_path = ""
fileNameEx = ""
def open_etymology_link(event=None):
input_word = entry_word.get()
url = f"https://www.etymonline.com/cn/word/{input_word}"
webbrowser.open(url)
def open_etymology_link2(event=None):
input_word = entry_word.get()
url = f"https://dictionary.cambridge.org/zhs/%E8%AF%8D%E5%85%B8/%E8%8B%B1%E8%AF%AD-%E6%B1%89%E8%AF%AD-%E7%AE%80%E4%BD%93/{input_word}"
webbrowser.open(url)
def open_etymology_link3(event=None):
input_word = entry_word.get()
url = f"https://www.youdao.com/result?word={input_word}&lang=en"
webbrowser.open(url)
# 创建处理回车和Ctrl+Z按键事件的函数
def on_enter_key(event):
download_button_click()
def on_ctrl_z(event):
clear_input()
def choose_save_path():
global default_save_path
default_save_path = filedialog.askdirectory()
def get_translation(word):
url = f'http://www.youdao.com/result?word={word}&lang=en'
global fileNameEx
# 发送HTTP GET请求获取网页内容
response = requests.get(url)
# 检查请求是否成功
if response.status_code == 200:
translation = ""
try:
pattern = r'>/(.*?)/</span><div'
matches = re.findall(pattern, response.text)
translation = f"英[{matches[0].replace(' ', '')}] 美[{matches[1].replace(' ', '')}]\n"
fileNameEx = matches[accent_var.get() - 1]
if len(matches) != 0:
translation += '--------------------------------------\n'
except:
pass
try:
pattern = r'<p class="trans-content" data-v-6eef0f9c>(.*?)</p>'
matches = re.findall(pattern, response.text)
translation = matches[0] + '\n'
if len(matches) != 0:
translation += '--------------------------------------\n'
except:
pass
try:
pattern = r'<a class="point" data-v-61ce6cc7 data-v-d80c723c>(.*?)</a>.*?<div class="word-exp_tran grey" data-v-d80c723c>(.*?)</div>'
matches = re.findall(pattern, response.text)
# 格式化输出结果
for words in matches:
translation += words[0] + ':\n' + words[1] + '\n'
if len(matches) != 0:
translation += '--------------------------------------\n'
except:
pass
try:
# 使用正则表达式提取单词翻译
pattern = r'<span class="pos" data-v-d80c723c>(.*?)</span><span class="trans" data-v-d80c723c>(.*?)</span>'
matches = re.findall(pattern, response.text)
for match in matches:
pos = match[0]
meaning = match[1]
translation += (f"{pos}{meaning}\n")
# 格式化输出结果
if len(matches) != 0:
translation += '--------------------------------------\n'
except:
pass
try:
pattern = r'<div class="col2"><a class="point" data-v-61ce6cc7>(.*?)</a><p class="sen-phrase">(.*?)</p></div>'
matches = re.findall(pattern, response.text)
# 格式化输出结果
for match in matches:
translation += match[0] + ':\n' + match[1] + '\n'
if len(matches) != 0:
translation += '--------------------------------------\n'
except:
pass
try:
pattern = r'简明.*?<span class="trans" data-v-d80c723c>(.*?)</span></li>'
matches = re.findall(pattern, response.text)
# 格式化输出结果
translation += matches[0] + '\n'
if len(matches) != 0:
translation += '--------------------------------------\n'
except:
pass
if translation == '':
return '未找到单词'
return translation.replace("<", "<").replace(">", ">").replace('"', '').replace('<i>', '').replace(
'</i>', '')
else:
return '未找到单词'
def play_pronunciation():
url = f"https://dict.youdao.com/dictvoice?audio={entry_word.get()}&type={accent_var.get()}"
response = requests.get(url)
if response.status_code == 200:
# 创建一个内存文件对象,并将音频内容写入其中
audio_data = io.BytesIO(response.content)
# 初始化pygame
pygame.init()
# 加载音频文件
pygame.mixer.music.load(audio_data)
# 播放音频
pygame.mixer.music.play()
# 等待音频播放完毕
while pygame.mixer.music.get_busy():
pass
# 关闭pygame
pygame.quit()
else:
pass
def download_pronunciation(word, accent):
global default_save_path # 访问全局变量
global fileNameEx
accent_map = {1: 'UK', 2: 'US'}
url = f"https://dict.youdao.com/dictvoice?audio={word}&type={accent}"
response = requests.get(url)
if response.status_code == 200:
pronunciation_folder = "pronunciation"
if not os.path.exists(pronunciation_folder) and default_save_path == "":
os.makedirs(pronunciation_folder)
# 使用选择的目录或者默认的 "pronunciation" 文件夹
save_path = default_save_path if default_save_path else pronunciation_folder
if fileNameEx != '':
file_path = os.path.join(save_path, f"{word} [{fileNameEx.replace(' ', '')}] ({accent_map[accent]}).mp3")
else:
file_path = os.path.join(save_path, f"{word} ({accent_map[accent]}).mp3")
fileNameEx = ''
with open(file_path, "wb") as f:
f.write(response.content)
print(f"{accent_map[accent]} 发音已下载并保存为 {file_path}")
winsound.PlaySound("success.wav", winsound.SND_FILENAME) # 播放成功音效
else:
pass
def download_button_click():
input_word = entry_word.get()
selected_accent = accent_var.get()
if input_word == '---':
print("程序结束")
root.destroy()
else:
translate_word()
download_pronunciation(input_word, selected_accent)
def clear_input():
entry_word.delete(0, tk.END)
text_translation.config(state=tk.NORMAL) # 允许编辑文本框
text_translation.delete(1.0, tk.END) # 清除文本框内容
def center_window(window, width, height):
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x_coordinate = (screen_width - width) // 2
y_coordinate = (screen_height - height) // 2
window.geometry(f"{width}x{height}+{x_coordinate}+{y_coordinate}")
def table_translate(event=None):
translate_word()
def translate_word():
input_word = entry_word.get()
translation = get_translation(input_word)
text_translation.config(state=tk.NORMAL) # 允许编辑文本框
text_translation.delete(1.0, tk.END) # 清除文本框内容
text_translation.insert(tk.END, translation) # 将翻译结果插入文本框
text_translation.config(state=tk.DISABLED) # 禁用文本框编辑
# 创建一个Tkinter窗口
root = tk.Tk()
root.title("英语小助手") # 设置窗口标题
# 创建菜单栏
menu_bar = tk.Menu(root)
root.config(menu=menu_bar)
# 创建"关于"菜单
about_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="关于", menu=about_menu)
# 将"帮助"和"赞助作者"添加到"关于"菜单
about_menu.add_command(label="帮助", command=show_help_window)
about_menu.add_command(label="赞助作者", command=show_sponsor_window)
# # 创建标签,用于显示提示文本
# label_word = tk.Label(root, text="请输入:")
# label_word.pack(pady=5) # 将标签添加到窗口,并设置上边距
# 创建一个框架,用于包含输入单词的文本框、清除按钮和翻译按钮
frame_input = tk.Frame(root)
frame_input.pack() # 将框架添加到窗口
# 创建一个文本框,用于输入单词
entry_word = tk.Entry(frame_input,width=30)
entry_word.pack(side=tk.LEFT, padx=5,pady=10) # 将文本框添加到框架的左侧,并设置左边距
# 创建一个清除按钮,用于清除文本框内容
clear_button = tk.Button(frame_input, text="清除", command=clear_input)
clear_button.pack(side=tk.LEFT, padx=5) # 将清除按钮添加到框架的左侧,并设置左边距
# 创建一个翻译按钮,用于触发单词翻译
translate_button = tk.Button(frame_input, text="翻译", command=translate_word)
translate_button.pack(side=tk.LEFT, padx=5) # 将翻译按钮添加到框架的左侧,并设置左边距
translate_button = tk.Button(frame_input, text="播放", command=play_pronunciation)
translate_button.pack(side=tk.LEFT, padx=5) # 将翻译按钮添加到框架的左侧,并设置左边距
# 创建一个文本框,用于显示翻译结果
text_translation = tk.Text(root, height=20, width=50)
text_translation.pack(pady=5) # 将文本框添加到窗口,并设置上边距
# 禁用文本框编辑,以防止用户修改翻译结果
text_translation.config(state=tk.DISABLED)
# 创建标签,用于提示用户选择发音类型
# label_accent = tk.Label(root, text="请选择发音类型:")
# label_accent.pack(pady=5) # 将标签添加到窗口,并设置上边距
# 创建一个整数变量,用于存储用户选择的发音类型
accent_var = IntVar()
# 创建一个框架,包含英音和美音的单选按钮
frame_accent = tk.Frame(root)
frame_accent.pack()
# 创建英音单选按钮,并将其关联到整数变量,值为1
radio_button_uk = tk.Radiobutton(frame_accent, text="英音", variable=accent_var, value=1)
radio_button_us = tk.Radiobutton(frame_accent, text="美音", variable=accent_var, value=2)
radio_button_uk.pack(side=tk.LEFT, padx=10) # 将英音单选按钮添加到框架的左侧,并设置左边距
radio_button_us.pack(side=tk.LEFT) # 将美音单选按钮添加到框架的左侧
dictionary_accent = tk.Frame(root)
dictionary_accent.pack()
# 创建一个按钮,用于选择默认保存路径
choose_path_button = tk.Button(dictionary_accent, text="选择默认保存路径", command=choose_save_path)
choose_path_button.pack(side=tk.LEFT, padx=5, pady=5) # 将按钮添加到窗口,并设置左边距和上边距
# 创建一个按钮,用于下载发音
download_button = tk.Button(dictionary_accent, text="下载发音", command=download_button_click)
download_button.pack(side=tk.LEFT, padx=5, pady=5) # 将按钮添加到窗口,并设置左边距和上边距
# 创建一个按钮,用于打开词源链接
open_link_button = tk.Button(dictionary_accent, text="查看词源", command=open_etymology_link)
open_link_button.pack(side=tk.LEFT, padx=5, pady=5) # 将按钮添加到窗口,并设置左边距和上边距
# 创建一个按钮,用于打开词源链接
open_link_button = tk.Button(dictionary_accent, text="剑桥词典", command=open_etymology_link2)
open_link_button.pack(side=tk.LEFT, padx=5, pady=5) # 将按钮添加到窗口,并设置左边距和上边距
# 创建一个按钮,用于打开词源链接
open_link_button = tk.Button(dictionary_accent, text="有道词典", command=open_etymology_link3)
open_link_button.pack(side=tk.LEFT, padx=5, pady=5) # 将按钮添加到窗口,并设置左边距和上边距
# 调用函数以将窗口置于屏幕中央
window_width = 400
window_height = 390
center_window(root, window_width, window_height)
# 绑定函数到输入框的按键事件,允许用户通过回车键触发翻译
entry_word.bind("<Return>", on_enter_key)
# 绑定函数到输入框的按键事件,允许用户通过Ctrl+Z快捷键清除输入
entry_word.bind("<Control-z>", on_ctrl_z)
# 绑定函数到输入框的按键事件,允许用户通过Ctrl+Q快捷键打开词源链接
entry_word.bind("<Control-q>", open_etymology_link)
# 绑定 "Table 翻译" 快捷键,允许用户通过Ctrl+A快捷键进行翻译
root.bind("<Control-a>", table_translate)
# 启动Tkinter主循环,等待用户交互
root.mainloop()