Building a Simple Python Translator with a Tkinter GUI and Youdao Web API
This article demonstrates how to create a basic translation application in Python using Tkinter for the graphical interface and the Youdao web API for translating text, providing complete source code and step‑by‑step explanations.
The article presents a tutorial for building a simple translation tool in Python, featuring a graphical user interface created with Tkinter and leveraging the Youdao translation web service to perform the actual language conversion.
An example screenshot of the resulting GUI is shown, illustrating the input field, translate button, and output display.
The required imports are listed:
<code>import tkinter
import random
import requests
import request
import urllib
from urllib import request, parse
import time, json, random, hashlib</code>The core function pachong() constructs a POST request to the Youdao API, generates the required timestamp and signature, encodes the parameters, sends the request, parses the JSON response, and returns the translated text.
<code>def pachong():
try:
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
data = {}
u = 'fanyideskweb'
d = content
f = str(int(time.time() * 1000) + random.randint(1, 10))
c = 'rY0D^0\'nM0}g5Mm1z%1G4'
sign = hashlib.md5((u + d + f + c).encode('utf-8')).hexdigest()
data['i'] = content
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = f
data['sign'] = sign
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_CL1CKBUTTON'
data['typoResult'] = 'true'
data = parse.urlencode(data).encode('utf-8')
req = request.Request(url, data=data)
response = request.urlopen(req)
res = response.read().decode('utf-8')
res = json.loads(res)
res = res["translateResult"]
return res[0][0]['tgt']
except:
print("cuowu")
</code>The GUI event handler eBtn(event) retrieves the user input, calls pachong() , and displays the translation result. The main block creates the Tkinter window, adds labels, entry widgets, a button bound to the click event, and starts the main loop.
<code>def eBtn(event):
global content
content = entry_w.get()
entry_r.config(entry_r, text=content)
entry_r.delete(0, 80)
entry_r.insert(0, str(pachong()))
if __name__ == "__main__":
win = tkinter.Tk()
label_val_q = tkinter.Label(win, width="80")
label_val_q.pack(side="top")
label_val_q.config(text="请输入要翻译的文本")
entry_w = tkinter.Entry(win, width="80")
entry_w.pack(side="top")
btn = tkinter.Button(win, text="翻译")
btn.pack(side="top")
btn.bind('<Button-1>', eBtn)
label_val_q = tkinter.Label(win, width="80")
label_val_q.pack(side="top")
label_val_q.config(text="翻译为:")
entry_r = tkinter.Entry(win, width="80")
entry_r.pack(side="top")
entry_r.bind('<Return>', eBtn)
win.mainloop()
</code>The author encourages readers to like the post if they find it helpful and includes a promotional QR code offering free Python course materials, which is ancillary to the tutorial.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.