2021-09-09

英文转中文十六进制文本翻译器

这个主要是用在软件汉化时辅助翻译,英文字节和中文字节长度不一样使用~

from tkinter import *
import tkinter as tk
from urllib import request
from urllib import parse
import json
import hashlib
import random
import struct,re,time,pyperclip
window.title('英文转中文16进制文本翻译器') 
window.geometry('530x600')
def translate_Word(en_str):
    URL = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
	Form_Data = {}
    Form_Data['from'] = 'en'             
    Form_Data['to'] = 'zh'
    Form_Data['q'] = en_str                      #要翻译数据
    Form_Data['transtype'] = 'hash'
    Form_Data['appid'] = ''     #这里输入您申请的APP ID
    Form_Data['salt'] = str(random.randint(32768, 65536))   #随机数
    Key="" #这里输入平台分配的密钥
    m=Form_Data['appid']+en_str+Form_Data['salt']+Key
    m_MD5 = hashlib.md5(m.encode('utf8'))
    Form_Data['sign'] = m_MD5.hexdigest()

    data = parse.urlencode(Form_Data).encode('utf-8')    #使用urlencode方法转换标准格式
    response = request.urlopen(URL,data)              #传递Request对象和转换完格式的数据
    html = response.read().decode('utf-8')              #读取信息并解码
    translate_results = json.loads(html)                #使用JSON
    print(translate_results)                         #打印出JSON数据
    translate_results = translate_results['trans_result']
    s=[]
    for i in translate_results:
        translate_results = i['dst']
        s.append(translate_results)
    return s
# 是文本数据始终停留在最后一行
def end_():
    hex_data.see(tk.END)
    hex_data.update()

# 复制结果到粘贴板
def copyText(key):
    value = pyperclip.copy(key)
    return value

# 清空所有数据
def dele():
    yiwen.delete(0.0,'end')
    yuanwen.delete(0.0,'end')
    hex_data.delete(0.0,'end')
    jieguo.set("")
    yuanwen_hex_zijie.set("")
    yuanwen_zijie.set("")
    yiwen_zijie.set("")
    yiwen_hex_zijie.set("")
    xiangcha_zijie.set("")
    jieguo_zijie.set("")
    jieguo_hex_zijie.set("")
    yuanwen.focus_force()

# 中文字符串转16进制编辑后输出
def utf_to_utf(data_txt):
    yiwen_data=data_txt
    yiwen_by=yiwen_data.encode('gbk')
    yi_lenn=len(yiwen_data.encode('gbk'))-2
    a=[]
    for i in yiwen_by:
        a.append(i)
    aas=a[:yi_lenn]
    d=struct.pack('<'+str(yi_lenn)+'B',*aas)
    gbk_to_utf8=d.decode("gbk").encode("utf-8")
    utf8_decode = gbk_to_utf8.decode("utf-8")
    return utf8_decode

# 格式化HEX数据
def hex_kong(text_d):
    b=str.upper(text_d)
    text_list = re.findall(".{2}",b)
    text = " ".join(text_list)
    text_len=len(text)
    new_text=text[:text_len]
    return new_text
xiangjian_len=''

# 翻译
def fanyi():
    global xiangjian_len
    yiwen.delete(0.0,'end')
    aass=0
    while True:
        try:
            yuanwen_data=yuanwen.get("0.0","end")
            ss=translate_Word(yuanwen_data)
            aass=0
            break
        except KeyError:
            aass+=1
            if aass>10:
                tt=tk.messagebox.showinfo(title='提示', message='翻译失败请重新更换要翻译的文本或重启软件!!')   # return 'ok'
                aass=0
                break
    aas=0
    try:
        for i in ss:
            aas+=1
            yiwen.insert('end',i)
            # # if aas==2:
            yiwen.insert('end','\n')
    except UnboundLocalError:
        print('翻译失败.')


# 打印数据
def Print():
    hex_data.delete(0.0,'end')
    yuanwen_data=yuanwen.get("0.0","end")
    yuanwen_len=len(yuanwen_data.encode('gbk'))-1
    yuanwen_by=yuanwen_data.encode('gbk')
    yuanwen_hexstring=yuanwen_by.hex()
    yuanwen_hex_len=len(yuanwen_hexstring.encode('gbk'))-2
    yuanwen_hex=yuanwen_hexstring[:yuanwen_hex_len]
    yuanwen_hex_data=hex_kong(yuanwen_hex)
    yiwen_data=yiwen.get("0.0","end")
    yiwen_len=len(yiwen_data.encode('gbk'))-2
    yiwen_by=yiwen_data.encode('gbk')
    yiwen_hexstring=yiwen_by.hex()
    yiwen_hex_len=len(yiwen_hexstring.encode('gbk'))-2
    yiwen_hex=yiwen_hexstring[:yiwen_hex_len]
    yiwen_hex_data=hex_kong(yiwen_hex)
    yuanwen_zijie.set(yuanwen_len)
    yuanwen_hex_zijie.set(yuanwen_hex_len)
    yiwen_zijie.set(yiwen_len)
    yiwen_hex_zijie.set(yiwen_hex_len)
    xiangjian_len=yuanwen_len-yiwen_len
    jieguo.set(utf_to_utf(yiwen_data)+" "*xiangjian_len)
    jieguo_data=jieguo.get()
    jieguo_len=len(jieguo_data.encode('gbk'))
    jieguo_by=jieguo_data.encode('gbk')
    jieguo_hexstring=jieguo_by.hex()
    jieguo_1=str.upper(jieguo_hexstring)
    jieguo_text = re.findall(".{2}",jieguo_1)
    jieguo_hex = " ".join(jieguo_text)
    jieguo_hex_len=len(jieguo_hexstring.encode('gbk'))
    jieguo_zijie.set(jieguo_len)
    jieguo_hex_zijie.set(jieguo_hex_len)
    copyText(jieguo_data)
    xiangcha_zijie.set(xiangjian_len)
    if xiangjian_len<0:
        tt=tk.messagebox.showinfo(title='提示', message='输出字符超出原始字符,请修改!!')   # return 'ok'
    hex_data.insert('end'," "*15+">"*6+" "+"原文16进制数据"+" "+"<"*6+"\n"+"="*60)
    hex_data.insert('end',yuanwen_hex_data)
    hex_data.insert('end','\n\n\n')
    end_()
    hex_data.insert('end'," "*15+">"*6+" "+"译文16进制数据"+" "+"<"*6+"\n"+"="*60)
    hex_data.insert('end',yiwen_hex_data)
    hex_data.insert('end','\n\n\n')
    end_()
    hex_data.insert('end'," "*15+">"*6+" "+"结果16进制数据"+" "+"<"*6+"\n"+"="*60)
    hex_data.insert('end',jieguo_hex)
    end_()
# 原文输入框
tk.Label(window, text='输入需要翻译的文字').place(x=230, y=1, anchor='nw')
yuanwen=tk.Text(window,height=10,width=60)
yuanwen.place(x=100, y=20, anchor='nw')

# 译文显示框
tk.Label(window, text='翻译结果').place(x=250, y=160, anchor='nw')
yiwen=tk.Text(window,height=10,width=60)
yiwen.place(x=100, y=180, anchor='nw')

# 十六进制显示框
tk.Label(window, text='十六进制数据显示区').place(x=230, y=320, anchor='nw')
hex_data=tk.Text(window,height=16,width=60)
hex_data.place(x=100, y=340, anchor='nw')

# 结果显示框
tk.Label(window, text='输出结果:').place(x=2, y=560, anchor='nw')

jieguo=tk.StringVar()
tk.Entry(window,textvariable =jieguo ,show=None,width=66).place(x=60,y=560,anchor='nw')

# 翻译
tk.Button(window,text='翻译',command=fanyi,width=10).place(x=2,y=36,anchor='nw')

# 清空
tk.Button(window,text='清空',command=dele,width=10).place(x=2,y=86,anchor='nw')

# 打印结果
tk.Button(window,text='打印',command=Print,width=10).place(x=2,y=146,anchor='nw')

# tk.Button(window,text='test',command=tks,width=10).place(x=2,y=246,anchor='nw')

# 原文字节
tk.Label(window, text='原文字节:').place(x=1, y=350, anchor='nw')
yuanwen_zijie=tk.StringVar()
tk.Entry(window,textvariable =yuanwen_zijie ,show=None,width=4).place(x=60,y=350,anchor='nw')
# 原文HEX
tk.Label(window, text='原文HEX:').place(x=1, y=380, anchor='nw')
yuanwen_hex_zijie=tk.StringVar()
tk.Entry(window,textvariable =yuanwen_hex_zijie ,show=None,width=4).place(x=60,y=380,anchor='nw')

# 译文字节
tk.Label(window, text='译文字节:').place(x=1, y=410, anchor='nw')
yiwen_zijie=tk.StringVar()
tk.Entry(window,textvariable =yiwen_zijie ,show=None,width=4).place(x=60,y=410,anchor='nw')
# 译文HEX
tk.Label(window, text='译文HEX:').place(x=1, y=440, anchor='nw')
yiwen_hex_zijie=tk.StringVar()
tk.Entry(window,textvariable =yiwen_hex_zijie ,show=None,width=4).place(x=60,y=440,anchor='nw')

# 相差
tk.Label(window, text='相差字节::').place(x=1, y=470, anchor='nw')
xiangcha_zijie=tk.StringVar()
tk.Entry(window,textvariable =xiangcha_zijie ,show=None,width=4).place(x=60,y=470,anchor='nw')

# 结果字节
tk.Label(window, text='结果字节:').place(x=1, y=500, anchor='nw')
jieguo_zijie=tk.StringVar()
tk.Entry(window,textvariable =jieguo_zijie ,show=None,width=4).place(x=60,y=500,anchor='nw')
# 结果HEX
tk.Label(window, text='结果HEX:').place(x=1, y=530, anchor='nw')
jieguo_hex_zijie=tk.StringVar()
tk.Entry(window,textvariable =jieguo_hex_zijie ,show=None,width=4).place(x=60,y=530,anchor='nw')
window.mainloop() 

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用python中的pymsql完成如下:表结构与数据创建 1. 建立 `users` 表和 `orders` 表。 `users` 表有用户ID、用户名、年龄字段,(id,name,age) `orders` 表有订单ID、订单日期、订单金额,用户id字段。(id,order_date,amount,user_id) 2 两表的id作为主键,`orders` 表用户id为users的外键 3 插入数据 `users` (1, '张三', 18), (2, '李四', 20), (3, '王五', 22), (4, '赵六', 25), (5, '钱七', 28); `orders` (1, '2021-09-01', 500, 1), (2, '2021-09-02', 1000, 2), (3, '2021-09-03', 600, 3), (4, '2021-09-04', 800, 4), (5, '2021-09-05', 1500, 5), (6, '2021-09-06', 1200, 3), (7, '2021-09-07', 2000, 1), (8, '2021-09-08', 300, 2), (9, '2021-09-09', 700, 5), (10, '2021-09-10', 900, 4); 查询语句 1. 查询订单总金额 2. 查询所有用户的平均年龄,并将结果四舍五入保留两位小数。 3. 查询订单总数最多的用户的姓名和订单总数。 4. 查询所有不重复的年龄。 5. 查询订单日期在2021年9月1日至9月4日之间的订单总金额。 6. 查询年龄不大于25岁的用户的订单数量,并按照降序排序。 7. 查询订单总金额排名前3的用户的姓名和订单总金额。 8. 查询订单总金额最大的用户的姓名和订单总金额。 9. 查询订单总金额最小的用户的姓名和订单总金额。 10. 查询所有名字中含有“李”的用户,按照名字升序排序。 11. 查询所有年龄大于20岁的用户,按照年龄降序排序,并只显示前5条记录。 12. 查询每个用户的订单数量和订单总金额,并按照总金额降序排序。
最新发布
06-03

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值