python制作一款翻译小工具,附含代码!

coding:utf-8

import wx
import requests
import json
import pyperclip
class Myframe(wx.Frame):

def __init__(self):
    # 调用父类的初始化
    wx.Frame.__init__(self,parent=None,title="翻译小工具",size=(400,350))
    # 设置窗口居中
    self.Center()
    # 创建画布布局控件
    panel = wx.Panel(parent=self)
    hbox1 = wx.BoxSizer(wx.HORIZONTAL)
    # 创建文本
    button1 = wx.Button(panel, label="翻译")
    button2 = wx.Button(panel,label=[Skrill下载](https://www.gendan5.com/wallet/Skrill.html)"复制结果")
    list1 = ["中文-英文",'中文-日语',"中文-德语","中文-韩语"]
    ch1 = wx.ComboBox(panel, -1, value=list1[0], choices=list1, style=wx.CB_SORT)
    ch1.Bind(wx.EVT_COMBOBOX, self.choess)
    button1.Bind(wx.EVT_BUTTON,self.onclick)
    button2.Bind(wx.EVT_BUTTON,self.copy)
    hbox1.Add(ch1,0,flag=wx.LEFT|wx.RIGHT|wx.FIXED_MINSIZE,border=5)
    hbox1.Add(button1,0,flag=wx.LEFT|wx.FIXED_MINSIZE,border=100)
    hbox1.Add(button2,1,flag=wx.LEFT|wx.FIXED_MINSIZE,border=5)
    vbox= wx.BoxSizer(wx.VERTICAL)
    self.text_ctrl = wx.TextCtrl(panel, style=wx.TE_LEFT)
    self.text_ctrl2 = wx.TextCtrl(panel, style=wx.TE_LEFT)
    vbox.Add(self.text_ctrl,3,flag=wx.ALL|wx.EXPAND,border=5)
    vbox.Add(hbox1,0,flag=wx.ALL|wx.EXPAND,border=5)
    vbox.Add(self.text_ctrl2,3,flag=wx.ALL|wx.EXPAND,border=5)
    panel.SetSizer(vbox)
    self.url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
    self.headers={
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.50'
    }
    self.dict = {
        "中文":"zh-CHS",
        '英文':'en',
        '日语':"ja",
        "韩语":'ko',
        '德语':'de'
    }
def choess(self,event):
    string = event.GetString()
def onclick(self,event):
    word = self.text_ctrl.GetValue()
    if word=="":
        str = "请输入要翻译的单词或句子"
        wx.MessageBox(str)
        return
    data = {
        'i': word,
        'from': 'dede',
        'to': 'ja',
        'doctype': 'json',
    }
    text = requests.post(url=self.url, headers=self.headers, data=data).text
    dict_data = json.loads(text)
    self.text_ctrl2.SetValue(dict_data['translateResult'][-1][-1]['tgt'])
def copy(self,event):
    str = self.text_ctrl2.GetValue()
    pyperclip.copy(str)

if name == '__main__':

app = wx.App()
frame  = Myframe()
frame.Show()
app.MainLoop()

你可能感兴趣的:(python)