SoFunction
Updated on 2024-12-20

python implementation of a translation tool for gui with tkinter

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from tkinter import *
import hashlib
import time
import json
import requests
import random
LOG_LINE_NUM = 0

class MY_GUI():
  def __init__(self,init_window_name):
    self.init_window_name = init_window_name
     = {

      'User-Agent': 'Own User-Agent',

      'Referer': '/',

      'Cookie': 'Own Cookies'

    }

     = {

      'i': None,

      'from': 'AUTO',

      'to': 'AUTO',

      'smartresult': 'dict',

      'client': 'fanyideskweb',

      'salt': None,

      'sign': None,

      'ts': None,

      'bv': None,

      'doctype': 'json',

      'version': '2.1',

      'keyfrom': '',

      'action': 'FY_BY_REALTlME'

    }

     = '/translate_o?smartresult=dict&smartresult=rule'


  #Setup Window
  def set_init_window(self):
    self.init_window_name.title("Translation_tool_v1.0")      #Window name
    #self.init_window_name.geometry('320x160+10+10') #290 160 is the size of the window, +10 +10 defines the default display position when the window pops up
    self.init_window_name.geometry('1068x681+10+10')
    #self.init_window_name["bg"] = "pink" #window background color, for other background colors see: /chl0000/article/details/7657887
    #self.init_window_name.attributes("-alpha",0.9) #voiding, the smaller the value the higher the degree of voiding
    #hashtag
    self.init_data_label = Label(self.init_window_name, text="Data to be processed")
    self.init_data_label.grid(row=0, column=0)
    self.result_data_label = Label(self.init_window_name, text="Output results")
    self.result_data_label.grid(row=0, column=12)
    self.log_label = Label(self.init_window_name, text="Log.")
    self.log_label.grid(row=12, column=0)
    # Textbox
    self.init_data_Text = Text(self.init_window_name, width=67, height=35) # Raw data entry boxes
    self.init_data_Text.grid(row=1, column=0, rowspan=10, columnspan=10)
    self.result_data_Text = Text(self.init_window_name, width=70, height=49) #Processing results show
    self.result_data_Text.grid(row=1, column=12, rowspan=15, columnspan=10)
    self.log_data_Text = Text(self.init_window_name, width=66, height=9) # Log box
    self.log_data_Text.grid(row=13, column=0, columnspan=10)
    #buttons
    self.str_trans_to_md5_button = Button(self.init_window_name, text="Conversion.", bg="lightblue", width=10,command=self.str_trans) # Call internal method plus() as direct call
    self.str_trans_to_md5_button.grid(row=1, column=11)


  # Function Functions
  def str_trans(self):
    word = self.init_data_Text.get(1.0,END).strip().replace("\n","")
    #print("src =",word)
    if word:
      try:
        ts = str(int(() * 10000))

        salt = str(int(() * 10000) + () * 10 + 10)

        sign = 'fanyideskweb' + word + salt + ']BjuETDhU)zqSxf-=B#7m'

        sign = hashlib.md5(('utf-8')).hexdigest()

        bv = '5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'

        bv = hashlib.md5(('utf-8')).hexdigest()

        ['i'] = word

        ['salt'] = salt

        ['sign'] = sign

        ['ts'] = ts

        ['bv'] = bv

        re = (, headers=, data=)
        jieguo = ()['translateResult'][0][0].get('tgt')
        #print(jieguo)
        # Output to interface
        self.result_data_Text.delete(1.0,END)
        self.result_data_Text.insert(1.0,jieguo)
        self.write_log_to_Text("INFO:Translation success")
      except:
        self.result_data_Text.delete(1.0,END)
        self.result_data_Text.insert(1.0,"Translation failed.")
    else:
      self.write_log_to_Text("ERROR:str_trans failed")


  # Get the current time
  def get_current_time(self):
    current_time = ('%Y-%m-%d %H:%M:%S',(()))
    return current_time


  # Log dynamic printing
  def write_log_to_Text(self,logmsg):
    global LOG_LINE_NUM
    current_time = self.get_current_time()
    logmsg_in = str(current_time) +" " + str(logmsg) + "\n"   # Line feeds
    if LOG_LINE_NUM <= 7:
      self.log_data_Text.insert(END, logmsg_in)
      LOG_LINE_NUM = LOG_LINE_NUM + 1
    else:
      self.log_data_Text.delete(1.0,2.0)
      self.log_data_Text.insert(END, logmsg_in)


def gui_start():
  init_window = Tk()       # Instantiate a parent window
  ZMJ_PORTAL = MY_GUI(init_window)
  # Set root window default properties
  ZMJ_PORTAL.set_init_window()

  init_window.mainloop()     # The parent window enters the event loop, which can be interpreted as keeping the window running, otherwise the interface is not displayed


gui_start()

Running effects:

You can use pyinstaller to package it as an exe and use it at any time.

It saves you from having to open a web page to search for translations and download translation software.

Above is the detailed content of python with tkinter to realize a gui translation tool, more information about python translation tool please pay attention to my other related articles!