小白教程

 找回密码
 立即注册
查看: 6141|回复: 1

[未解决] python 制作一个翻译软

[复制链接]

2

主题

6

帖子

14

积分

新手上路

Rank: 1

积分
14
发表于 2021-5-18 05:47:04 | 显示全部楼层 |阅读模式
python 制作一个翻译软
  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()
复制代码


本帖寻求最佳方案

回复被采纳后将获得奖励 C币 5 ,目前已有 1 个回复 我要奖励
回复

使用道具 举报

0

主题

3

帖子

6

积分

新手上路

Rank: 1

积分
6
发表于 2021-5-18 05:47:38 | 显示全部楼层

很想知道调用了什么接口
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-20 14:43 , Processed in 0.047186 second(s), 22 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

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