[Python] 纯文本查看 复制代码
# -*- coding:utf-8 -*-
import wx,re
import requests
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='翻译', size=(600, 500),name='frame',style=541072448)
self.启动窗口 = wx.Panel(self,-1)
self.Centre()
self.标签1 = wx.StaticText(self.启动窗口,size=(149, 39),pos=(-12, 4),label='输入:',name='staticText',style=2321)
标签1_字体 = wx.Font(20,74,90,700,False,'Microsoft YaHei UI',28)
self.标签1.SetFont(标签1_字体)
self.编辑框2 = wx.TextCtrl(self.启动窗口,size=(349, 67),pos=(18, 51),value='',name='text',style=wx.TE_MULTILINE)
self.按钮1 = wx.Button(self.启动窗口,-1,size=(70, 60),pos=(377, 53),label='翻译')
self.Bind(wx.EVT_BUTTON, self.fanyi, self.按钮1)
按钮1_字体 = wx.Font(20,74,90,700,False,'Microsoft YaHei UI',28)
self.按钮1.SetFont(按钮1_字体)
self.多选框1 = wx.CheckBox(self.启动窗口,size=(70, 18),pos=(483, 56),name='check',label='百度翻译',style=12288)
self.多选框1.Set3StateValue(1)
self.多选框2 = wx.CheckBox(self.启动窗口,size=(63, 20),pos=(483, 77),name='check',label='有道翻译',style=12288)
self.多选框2.Set3StateValue(1)
self.多选框3 = wx.CheckBox(self.启动窗口,size=(66, 14),pos=(483, 100),name='check',label='中英翻译',style=12288)
self.多选框3.Set3StateValue(1)
self.编辑框3 = wx.TextCtrl(self.启动窗口,size=(532, 258),pos=(16, 184),value='',name='text',style=wx.TE_MULTILINE)
self.标签2 = wx.StaticText(self.启动窗口,size=(106, 38),pos=(12, 140),label='结果:',name='staticText',style=2321)
标签2_字体 = wx.Font(20,74,90,700,False,'Microsoft YaHei UI',28)
self.标签2.SetFont(标签2_字体)
def fanyi(self,event):
self.编辑框3.SetValue("")
if self.编辑框2.GetValue() == '':
pass
else:
if self.多选框1.GetValue() == True:
jie = self.baidu()
self.编辑框3.AppendText('百度翻译:\n' + jie + '\n---------------------------------------\n')
else:
pass
if self.多选框2.GetValue() == True:
jie1 = self.youdao()
self.编辑框3.AppendText('有道翻译:\n' + jie1 + '\n---------------------------------------\n')
else:
pass
if self.多选框3.GetValue() == True:
jie2 = self.zhongying()
self.编辑框3.AppendText('中英翻译:\n' + jie2 + '\n---------------------------------------\n')
else:
pass
def zhongying(self):
zh = 'zh'
en = 'en'
for c in self.编辑框2.GetValue():
if ('\u4e00' <= c <= '\u9fa5'): # 判断是否为中文
zh = 'en'
en = 'zh'
try:
url = "https://fy.httpcn.com/bdaify/?s1=%s&t1=%s&q=%s" % (en, zh, self.编辑框2.GetValue())
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
}
fanhui = requests.get(url, headers=headers, timeout=30)
jieguo = fanhui.json()
jie2 = jieguo['result']['trans_result'][0]['dst']
return self.编辑框2.GetValue() + '\n' + jie2
except:
return '无数据'
def youdao(self):
try:
url = "https://m.youdao.com/translate"
data = {
'inputtext': self.编辑框2.GetValue(),
'type': 'AUTO'
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0'
}
r = requests.post(url, params=data, headers=headers, timeout=30)
r.encoding = 'utf-8'
rr = r.text
b = re.findall("<li>(.*?)</li>", rr)[2]
return self.编辑框2.GetValue() + '\n' + b
except:
return '无数据'
def baidu(self):
try:
url = "https://fanyi.baidu.com/sug"
data = {
'kw': self.编辑框2.GetValue()
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0'
}
r = requests.post(url, params=data, headers=headers, timeout=30)
r.encoding = 'unicode_escape'
rr = r.json()
jie = f"{rr['data'][0]['k']}\n{rr['data'][0]['v']}"
return jie
except:
return '无数据'
class myApp(wx.App):
def OnInit(self):
self.frame = Frame()
self.frame.Show(True)
return True
if __name__ == '__main__':
app = myApp()
app.MainLoop()