Akik Look at that coder

Public account: Look at that code farmer

1. Project Introduction

In my life and work, I often translate some words on the Internet.

In that case, why not use Python to make your own translation gadget.

Mini translation tool demo copy

Function:

  • translation

  • Save translation information in TXT file

  • Empty page

2. Configure the project

  • Python3.x
  • Requests the library
  • Tkinter library

The Requests library is mainly used to get post information for youdao Dictionary translation pages

The Tkinter library is primarily used to create a GUI interface for small programs, although Pyqt5 is an option here

3. Project process

1. Import related modules

import requests
from requests.exceptions import RequestException
import tkinter as tk
Copy the code

2. Define the translation function

Str1 = text1.get() # assign the value of the textbox to the interface parameter data={'doctype':'json'.'type':'Auto'.'i'Str1} # call youdao Dictionary url ="http://fanyi.youdao.com/translate"

    try:
        r=requests.get(url,params=data)
        if r.status_code == 200:
            result = r.json()
            translate_result =result['translateResult'] [0] [0] ["tgt"]
            text2.delete(1.0."end")
            text2.insert('end',translate_result)
    except RequestException:
        text2.insert('end'."Error occurred")
Copy the code

3. Define save functions

def write():
    f1 = open('E:/demo/tran/save.txt'.'a+')
    f1.write(text1.get()+', '+text2.get(0.0,tk.END))
Copy the code

4. Define the emptying function

def delete():
    text1.delete(0."end")
    text2.delete(1.0."end")
Copy the code

5. Design of GUI Windows

window=tk.Tk()
window.title("Look at that code non translator.")
Copy the code

1. Create the size and color of the text box

text1 = tk.Entry(window,width=80,bg='whitesmoke')
text2 = tk.Text(window,height=18,bg='lightgrey')

text1.grid(row=0,sticky="W",padx=1)
text2.grid(row=1)
Copy the code

2. Create the size and color of the text box

t_button = tk.Button(window,text='translation',relief=tk.RAISED,width=8,height=1,font='宋体',bg='red',fg='white',command=translate)
button1=tk.Button(window,text='save',relief=tk.RAISED,width=8,height=1,font='宋体',command=write)
button2=tk.Button(window,text='empty',relief=tk.RAISED,width=8,height=1,font='宋体',command=delete)
Copy the code

3. Create a background image

image_file = tk.PhotoImage(file='1.jpg')
label = tk.Label(window,image=image_file)
Copy the code

4. Complete the layout and set the location of each control

t_button.grid(row=0,column=1,padx=2)
button1.grid(row=0,column=2,padx=2)
button2.grid(row=0,column=3,padx=2)
label.grid(row=1,column=1,columnspan=3)

tk.mainloop()
Copy the code

5. Run the program

4. Project summary and thinking

This project uses two common Python libraries, Requests and Tkinter library, to realize the functions of translation, saving, clearing, and GUI display. On the one hand, this demo shows the process of tkinter creating GUI interface. On the other hand, it shows the requests library process for obtaining Web page POST information, which can help deepen the basic learning of Python.

If you find this helpful:

1. Click “like” to support it, so that more people can see this article

2, pay attention to the public number: look at that code farmers, we study together and progress together.

This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign