翻译器

import os                                   # 创建文件夹,删除文件
import time                                 # 引入时间模块,用作延迟
import base64                               # 将二进制转换为图片显示(软件logo)
import minimu                               # 播放音频
import requests                             # 爬取有道翻译,获取翻译结果
from tkinter import *                       # 制作GUI界面
from aip import AipSpeech                   # 调用百度API,语音合成
from mutagen.mp3 import MP3                 # 获取mp3时长
from icon import img1, img2                 # 导入图片转换后的二进制(软件logo)
from PIL import Image, ImageTk              # 可以让TK使用png格式的图片(TK默认只支持GIF格式)
from tkinter import messagebox              # TK弹窗
from json.decoder import JSONDecodeError    # 捕获json.decoder错误

# 获取翻译后的结果
def fanYi(event):
    global result, var, path
    try:
        entry2.delete(0, 'end')
        var = entry1.get()
        data = {
            'i': var,
            'from': 'AUTO',
            'to': 'AUTO',
            'smartresult': 'dict',
            'client': 'fanyideskweb',
            'salt': '15466010467912',
            'sign': '98565fc8e4ea2ab0bb49b082b474ed27',
            'ts': '1546601046791',
            'bv': '0ef078d6b7f3e6f8047a6cbd85c069cc',
            'doctype': 'json',
            'version': '2.1',
            'keyfrom': 'fanyi.web',
            'action': 'FY_BY_REALTIME',
            'typoResult': 'false',
        }
        res = requests.post(
            'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule', data)
        result = res.json()['translateResult'][0][0]['tgt']
        name = os.environ['USERNAME']
        path = r"C:/Users/" + name + "/AppData/Local/Temp/"
    except JSONDecodeError:
        messagebox.showwarning(title="warning", message="翻译内容为空,请输入后重试")
    else:
        entry2.insert('end', result)

# 生成翻译前的音频
def say1():
    try:
        # 调用百度语音识别API
        """ 你的 APPID AK SK """
        APP_ID = '15704344'
        API_KEY = 'roM0RL4WS8ZzzF2Eu40HdUFD'
        SECRET_KEY = '1hRU2MPFGHRDGbRcRzIGveevORvY2Kbs'
        client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
        effect_data = client.synthesis(var, 'zh', 1, {
            'vol': 5,
            'spd': 4,
            'pit': 8,
            'per': 4
        })
        # 写入文件
        if not os.path.exists(path):
            os.mkdir(path)
        with open(path + "data.mp3", 'wb+') as ff:
            ff.write(effect_data)
        # 获取mp3时长
        audio = MP3(path + "data.mp3")
        mp3_lenth = audio.info.length
        # 播放语音1
        song = minimu.load(path + "data.mp3")
        song.play()
        time.sleep(mp3_lenth)
        song.stop()
    except NameError:
        messagebox.showwarning(title="warning", message="请输入内容并点击翻译后重试")
    except TypeError:
        messagebox.showwarning(title="warning", message="请输入内容并点击翻译后重试")

# 生成翻译后的音频
def say2():
    try:
        # 调用百度语音识别API
        """ 你的 APPID AK SK """
        APP_ID = '15704344'
        API_KEY = 'roM0RL4WS8ZzzF2Eu40HdUFD'
        SECRET_KEY = '1hRU2MPFGHRDGbRcRzIGveevORvY2Kbs'
        client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
        effect_data = client.synthesis(result, 'zh', 1, {
            'vol': 5,
            'spd': 4,
            'pit': 8,
            'per': 4
        })
        # 写入文件
        if not os.path.exists(path):
            os.mkdir(path)
        with open(path+"data.mp3", 'wb+') as ff:
            ff.write(effect_data)
        # 获取mp3时长
        audio = MP3(path+"data.mp3")
        mp3_lenth = audio.info.length
        # 播放语音2
        song = minimu.load(path+"data.mp3")
        song.play()
        time.sleep(mp3_lenth)
        song.stop()
    except NameError:
        messagebox.showwarning(title="warning", message="请输入内容并点击翻译后重试")

# 清空输入框
def clean():
    entry1.delete(0, 'end')
    entry2.delete(0, 'end')

# 写入图片数据
with open("img1.ico", 'wb+') as f:
    f.write(base64.b64decode(img1))
with open(r'img2.png', 'wb+') as f:
    f.write(base64.b64decode(img2))

# 基本设置
root = Tk()
root.title('翻译器')
root.iconbitmap("img1.ico")
os.remove("img1.ico")
root.geometry('455x150+560+250')      # 位置坐标随意

# 导入png图片
img_open = Image.open(r'img2.png')
img_png = ImageTk.PhotoImage(img_open)

# 设置标签
label1 = Label(root, text='输入内容:', fg='red', font=('GB2312', 18))
label1.grid(row=0, column=0)
label2 = Label(root, text='结果:', fg='red', font=('GB2312', 18))
label2.grid(row=1, column=0)

# 设置文本
entry1 = Entry(root, fg='red', font=('GB2312', 18))
entry1.grid(row=0, column=1, pady=10)
entry2 = Entry(root, fg='red', font=('GB2312', 18))
entry2.grid(row=1, column=1, pady=10)

# 设置按钮
root.bind('<Return>', fanYi)        # 绑定回车键
button1 = Button(root, text='翻译', fg='blue', font=('GB2312', 18))
button1.bind('<Button-1>', fanYi)   # 绑定按钮
button1.grid(row=2, column=0)
button2 = Button(root, text='清空', fg='blue', font=('GB2312', 18), command=clean)
button2.grid(row=2, column=1)
button3 = Button(root, text='退出', fg='blue', font=('GB2312', 18), command=root.quit)
button3.grid(row=2, column=2)
button4 = Button(root, text='发音1', image=img_png, command=say1)
button4.grid(row=0, column=2)
button5 = Button(root, text='发音2', image=img_png, command=say2)
button5.grid(row=1, column=2)
os.remove("img2.png")

root.mainloop()       # 让图形化软件出现

Pyinstaller 打包 Tkinter 程序时引入图标运行报错的解决方法

1.图片转换base64码:image.py

import base64

# 图片转换
def tra(name, path, style='a+'):
    with open(path, 'rb') as f:
        b64str = base64.b64encode(f.read())
    write_data = name + " = " + "%s" % b64str
    with open('icon.py', style) as f:
        f.write("\n"+write_data)

tra(name='img1', path=r'E:\Python\Code\packing\windows.ico', style='w+')
tra(name='img2', path=r'E:\Python\Code\小喇叭.png')

2.这时会生成一个icon.py的文件,就是两串base64编码,也就是那两张图片转换来的

# 需要用到的的所有文件
翻译器.py
image.py
icon.py
icon.ico
icon.png

3.1 在翻译器.py里面导入以下模块

import os
import base64
from icon import img1, img2

3.2 并加入以下代码

# 写入图片数据
with open("img1.ico", 'wb+') as f:
    f.write(base64.b64decode(img1))
with open("img2.png", 'wb+') as ff:
    ff.write(base64.b64decode(img2))

3.3 然后再再需要的地方添加图片,最后把临时创建的img图片文件删除即可!

master.iconbitmap("<span style="color: #ff0000;">img1.ico</span>")
os.remove("img1.ico")
# 设置画布
canvas = Canvas(master, width=80, height=70)
canvas.grid(row=0, column=2)
image_file = PhotoImage(file='<span style="color: #ff0000;">img2.png</span>')
os.remove("img2.png")
image = canvas.create_image(0, 0, anchor='nw', image=image_file)


2.再次执行打包

pyinstaller -F -w -i icon.ico 翻译器.py

 

更新记录:

1.0.0:完善基本功能

1.0.1:加入一键清空功能

1.0.2:优化界面布局

1.0.3:加入语音播报功能

1.0.4:输入内容后按下回车键可以直接把结果显示在结果处(不用再去点击翻译按钮)

1.0.5:优化界面布局

  • 最后偷偷在下面放个不起眼的小蓝链:翻译器.exe :aixin: :mianqiang:
Last modification:August 26th, 2020 at 08:48 pm
如果觉得我的文章对你有用,请随意赞赏