Previously we designed the graphical interface and completed the translation function through a crawler. In this section we will combine the previous content to complete the production of the translator.
importwximporturllib.request#Import module importurllib.parseimportjsonclassMyFrame(wx.Frame):def__init__(self):wx.Frame.__init__(self,None,-1,translator,size=(600,2 00))panel=wx.Panel(self)#Create a canvas, then create a ribbon and place it on the canvas #Create a title and place it in the panel self.title=wx.StaticText(panel,label='Simple Translator ')#Create a static text and place it in the panel self.translate=wx.StaticT ext(panel,label='Translation content:')#Create an input text box and place it in the panel self.tran_slate=wx.TextCtrl(panel,style=wx.TE_LEFT)#Create a translation button and place it in the panel self .button_ts=wx.Button(panel,label= 'Translation')#Bind button event, click to trigger translation function self.button_ts.Bind(wx.EVT_BUTTON,self.OnclickEventname)#Create a close button and place it in panel self.button_shutdown=wx.Button(panel,label= 'Close')#Bind button component, click to trigger the closing function self.button_shutdown.Bind(wx.EVT_BUTTON,self.OnclickEvent)container_one=wx.BoxSizer(wx.HORIZONTAL)#Put the static text and input text box in this BoxSizer container_one. Add(self.translate,proportion=0,flag=wx.ALL,border=7)container_one.Add(self.tran_slate,proportion=1,flag=wx.ALL,border=7)#Create another horizontal arrangement BoxSizercontainer _two=wx.BoxSizer(wx.HORIZONTAL)#Put two buttons into this BoxSizer container_two.Add(self.button_ts,proportion=0,flag=wx.ALIGN_CENTER,border=4)container_two.Add(self. button_shutdown,proportion=0,flag=wx.ALIGN_CENTER,border=4)#Create a vertically arranged BoxSizersizers=wx.BoxSizer(wx.VERTICAL)#Put all the above content into this BoxSizer sizers.Add (self.t itle,proportion=0,flag=wx.BOTTOM|wx.TOP|wx.ALIGN_CENTER,border=10)sizers.Add(container_one,proportion=0,flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border= 40)si zers.Add(container_two,proportion=0,flag=wx.ALIGN_CENTER|wx.TOP,border=10)panel.SetSizer(sizers)defOnclickEventname(self,e):globalpanelurnm=self.tran _slate.GetValue()url=http://fanyi.youdao.com/translate?smartresult=dict&smartresult=ruledata={}data['i']=urnmdata['from']='AUTO'data['to'] ='AUTO'data ['smartresult']='dict'data['client']='fanyideskweb'data['salt']='15823411455528'data['sign']='d03024a90896a5eb31a74a9344657b0e'data['doctype']='json' data['version']='2.1'data['keyfrom']='fanyi.web'data['action']='FY_BY_REALTlME'data=urllib.parse.urlencode(data).encode('utf-8' )r=urllib.request.Req uest(url,data)r.add_header('User-Agent','Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/79.0.3945.130Safari/537.36')re sponse=urllib.request.urlopen(r)html=response.read().decode('utf-8')trs=json.loads(html)result=trs['translateResult'][0][0][' tgt']print(translation result:,result) wx.MessageBox('Translation result: %s'%result,'Translator') defOnclickEvent(self,e):self.Destroy()if__name__==__main__:app=wx.App()frame=MyFrame()frame. Show()app.MainLoop()
Since the code in this section is derived from the previous two sections, we will not repeat the analysis of the same code parts. Let's mainly look at the differences.
#Create a translation button and place it in the panel self.button_ts=wx.Button(panel,label='Translation')#Bind the button event, click to trigger the translation function self.button_ts.Bind(wx.EVT_BUTTON,self.OnclickEventname) #Create one A close button, placed in the panel self.button_shutdown=wx.Button(panel,label='close')#Bind button event, click to trigger the close function self.button_shutdown.Bind(wx.EVT_BUTTON,self.OnclickEvent)
We have bound events to the translation and close buttons respectively. When clicked, the corresponding functions will be triggered. Let's analyze the functions. The function of the close button is relatively simple, that is, click the button to close the window.
defOnclickEvent(self,e):self.Destroy()
When we click the translation button, the translation function will be triggered. The content of the translation function can be learned in detail in the previous section. Here we mainly pass a parameter.
urnm=self.tran_slate.GetValue()
Pass the information entered on the GUI interface to data.
data['i']=urnm
This corresponds to the content we entered in the previous section. Finally, look at this line of code.
wx.MessageBox('Translation result: %s'%result,'Translator')
We return the translation results through a window, thus realizing the translation function.
Finally we come to test our translator.
The actual combat content in this section is relatively basic, mainly combining GUI programming and crawler content. If you are interested, you can also combine the database to complete the actual combat content. The next actual combat will combine the database to further complete the actual combat content.