鱼C论坛

 找回密码
 立即注册
查看: 12960|回复: 46

[作品展示] python 制作一个翻译软件

[复制链接]
发表于 2021-2-15 15:50:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
废话不多说,直接上代码:
  1. from tkinter import *
  2. from tkinter import ttk
  3. import requests
  4. import json

  5. class Application(Tk):
  6.     def __init__(self):
  7.         super().__init__()

  8.         self.title('翻译')
  9.         self.geometry('540x380')
  10.         self.resizable(False, False)
  11.         self.init_widgets()

  12.     def init_widgets(self):
  13.         trans_type1List = ["自动检测", "中文", "英文"]
  14.         trans_type2List = ["中文", "英文"]

  15.         label1 = Label(self, text='翻译成——>', font=('微软雅黑', 14))
  16.         label1.place(x=168, y=8, width=120, height=33)

  17.         trans_button = Button(self, text='翻译', command=self.translate_text)
  18.         trans_button.place(x=450, y=8, width=70, height=33)

  19.         self.type_dict = {
  20.             "自动检测": "auto-detect",
  21.             "中文": "zh-Hans",
  22.             "英文": "en"
  23.         }

  24.         self.trans_type1 = ttk.Combobox(self, values=trans_type1List, font=('微软雅黑', 14))
  25.         self.trans_type1.current(0)
  26.         self.trans_type1.bind("<Key>", lambda e: "break")
  27.         self.trans_type1.place(x=16, y=8, width=130, height=33)

  28.         self.trans_type2 = ttk.Combobox(self, values=trans_type2List, font=('微软雅黑', 14))
  29.         self.trans_type2.current(1)
  30.         self.trans_type2.place(x=310, y=8, width=130, height=33)

  31.         self.origin_text = Text(self, font=('微软雅黑', 14))
  32.         self.origin_text.insert("1.0", "原文")
  33.         self.origin_text.place(x=0, y=56, width=540, height=150)

  34.         self.trans_text = Text(self, font=('微软雅黑', 14))
  35.         self.trans_text.insert("1.0", "翻译")
  36.         self.trans_text.configure(state=DISABLED)
  37.         self.trans_text.place(x=0, y=210, width=540, height=180)

  38.     def translate_text(self):
  39.         text = self.origin_text.get("1.0", "end")
  40.         type1 = self.type_dict[self.trans_type1.get()]
  41.         type2 = self.type_dict[self.trans_type2.get()]

  42.         self.trans_text.configure(state=NORMAL)
  43.         self.trans_text.delete("1.0", "end")
  44.         self.trans_text.insert("1.0", self.translate(text, type1, type2))
  45.         self.trans_text.configure(state=DISABLED)

  46.     def translate(self, text, type1, type2):
  47.         url = "https://cn.bing.com/ttranslatev3?isVertical=1&&IG=B82C0E46ED384A0FA9E24E5DBFE84EBF&IID=translator.5024.1"
  48.         form_data = {
  49.             "fromLang": f"{type1}",
  50.             "text": f"{text}",
  51.             "to": f"{type2}"
  52.         }
  53.         headers = {
  54.             "user-agent": 'Mozilla/5.0 (Windows NT 10; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94412.3 Safari/537.36'
  55.         }
  56.         res = requests.post(url, data=form_data, headers=headers)
  57.         content = json.loads(res.text)

  58.         return content[0]["translations"][0]["text"]


  59. if __name__ == '__main__':
  60.     app = Application()
  61.     app.mainloop()
复制代码

效果:
翻译.gif

如果想看教程的话,可以到这个链接:
游客,如果您要查看本帖隐藏内容请回复


如果上面的链接失效了的话或者感觉我的公众号还不错的话,希望各位鱼油们可以关注一下我的公众号。

                               
登录/注册后可看大图

评分

参与人数 4荣誉 +9 鱼币 +11 贡献 +3 收起 理由
yayc_zcyd + 1 + 1 鱼C有你更精彩^_^
糖甜弯了嘴 + 3 + 3 + 3 感谢楼主无私奉献!
qq1151985918 + 2 无条件支持楼主!
weiter + 5 + 5

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-15 16:43:05 | 显示全部楼层

回帖奖励 +1 鱼币

大佬牛皮!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-15 16:54:11 From FishC Mobile | 显示全部楼层

回帖奖励 +1 鱼币

weiter 发表于 2021-2-15 16:43
大佬牛皮!

我更喜欢百度翻译,你有没有兴趣参加NOIP
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-15 17:58:29 | 显示全部楼层

回帖奖励 +1 鱼币

厉害
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-15 18:05:45 | 显示全部楼层

回帖奖励 +1 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-15 19:33:34 | 显示全部楼层

回帖奖励 +1 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-16 08:49:40 | 显示全部楼层

回帖奖励 +1 鱼币

很想知道调用了什么接口
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-16 09:39:04 | 显示全部楼层

回帖奖励 +1 鱼币

厉害
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-16 09:48:54 | 显示全部楼层

回帖奖励 +1 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-16 10:12:20 | 显示全部楼层
厉害呀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-16 12:41:47 | 显示全部楼层
厉害
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-16 15:38:39 | 显示全部楼层
看看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-16 15:57:18 | 显示全部楼层
牛啊,i了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-16 16:18:44 | 显示全部楼层
厉害
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-16 18:04:29 | 显示全部楼层
www厉害
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-18 21:15:52 | 显示全部楼层
zhiweng07 发表于 2021-2-16 08:49
很想知道调用了什么接口

微软的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-19 12:23:17 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-19 19:34:19 | 显示全部楼层
感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-20 10:31:31 | 显示全部楼层
建议增加按回车键就可以翻译的功能
不增加输入完内容还得用鼠标点“翻译”按钮
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-20 16:29:27 | 显示全部楼层
tkinter
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 01:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表