Python learning diary-Baidu translation GUI and python learning diary gui

Source: Internet
Author: User

Python learning diary-Baidu translation GUI and python learning diary gui

When chatting with my girlfriend, my girlfriend complained that he had to open the Baidu webpage to find the translation every time. This gave me the idea of doing a small translation field, and Baidu translation searched for its APIs, in my opinion, register an individual test account on the open platform of Baidu translation. The Code is as follows:

1 # coding = UTF-8 2 from Tkinter import * 3 import tkMessageBox 4 import urllib2 5 import hashlib 6 import json 7 8 9 trans_id = '************ * ***** '# provide Baidu translation APP ID10 trans_password =' ************* '# provide key 11 phone_num = '***** * ****** '# The requirement is salt, in fact, the phone number is 12 13 14 def count (word): 15 c = 016 for I in word: 17 c + = 118 return c19 20 21 def md5hex (word ): 22 if isinstance (word, unicode): 23 word = word. encode ("ut F-8 ") 24 elif not isinstance (word, str): 25 word = str (word) 26 m = hashlib. md5 () 27 m. update (word) 28 return m. hexdigest () 29 30 31 def trans (word, fr = 'en', to = 'zh '): 32 word_num = count (word) 33 sign = md5hex (trans_id + word + phone_num + trans_password) 34 api = 'HTTP: // api.fanyi.baidu.com/api/trans/vip/translate? Q = '+ word +' & from = '+ 'en' +' & to = '+ 'zh' + '& appid = 20161120000032369 & salt =' + phone_num + '& sign = '+ sign35 trans_data = urllib2.urlopen (api ). read () 36 trans_data = json. loads (trans_data) 37 trans_data = trans_data ['Trans _ result'] [0] ['dst '] 38 return trans_data39 40 41 class Application (Frame ): 42 def _ init _ (self, master = None): 43 Frame. _ init _ (self, master, bd = 30) 44 self. pack () 45 self. createWidgets () 46 47 def createWidgets (self): 48 self. nameInput = Entry (self) 49 self. nameInput. pack () 50 self. alertButton = Button (self, text = 'translation', command = self. hello) 51 self. alertButton. pack () 52 53 def hello (self): 54 name = self. nameInput. get () 55 result = trans (name) 56 tkMessageBox. showinfo ('translation result', 'result: % s' % Result) 57 58 app = Application () 59 # Set window title: 60 app. master. title ('translate') 61 # main message loop: 62 app. mainloop ()

The test result is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.