Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3648490
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2021-08-04 17:18:13

# 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="复制结果")

        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 = ""

        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()

阅读(1051) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~