Fundamentals 4 min read

Building a Python GUI Translator with Tkinter and Web Requests

This article demonstrates how to create a simple desktop translator application in Python using Tkinter for the graphical interface and web‑scraping techniques to call Youdao's translation service, providing complete source code and step‑by‑step explanations.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Building a Python GUI Translator with Tkinter and Web Requests

The article introduces a Python project that implements a personal translator with a graphical user interface built on Tkinter. It explains that the program sends translation requests to Youdao's online API by constructing the required parameters, generating a security sign, and handling the JSON response.

Key components include importing necessary modules (tkinter, random, requests, urllib, time, json, hashlib), setting up the main window, and defining two functions: pachong() for performing the HTTP request and parsing the result, and eBtn(event) for handling button clicks, retrieving user input, and displaying the translated text.

The provided source code is presented in a pre block to preserve formatting:

<code>import tkinter
import random
import requests
import request
import urllib
from urllib import request,parse
import time,json,random,hashlib
win = tkinter.Tk()

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

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

if __name__ == "__main__":
    label_val_q = tkinter.Label(win,width = "80")
    label_val_q.pack(side = "top")
    label_val_q.config(label_val_q,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(label_val_q,text = "翻译为:")
    entry_r = tkinter.Entry(win,width = "80")
    entry_r.pack(side = "top")
    entry_r.bind('<Return>',eBtn)
    win.mainloop()
</code>

Running the script opens a window where users can input text, click the "翻译" button, and see the translated result displayed in a second entry field. The article concludes with a disclaimer that the content is collected from the internet and credits the original author.

GUItranslationWeb ScrapingTkinter
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.