【ANSI】汉字转机内码工具

工具介绍

工具名称:汉字转机内码工具(GbToANSI)

工具版本:1.01

功能介绍:输入汉字内容,即可将对应内容转为机内码

工具截图:

【ANSI】汉字转机内码工具_第1张图片

PS:使用过程中若出现Bug可告知作者进行修复

下载链接:GitHub下载链接  百度网盘下载链接(提取码:1314)

源代码:

from tkinter import *
from Tool.ico import gtaIco
from Tool import PackTool as pt
from ctypes import windll
import os
from sys import executable as EXECUTABLE
from subprocess import Popen, PIPE, STDOUT
from win32api import SetFileAttributes
from win32con import FILE_ATTRIBUTE_HIDDEN
import Tool.MyLogger as ml


class GbToANSI:
    __log = ml.IMyLogger()

    def _createShortcut(self):
        execpath = os.path.dirname(EXECUTABLE)
        batpath = execpath + "\\init.bat"
        if os.path.exists(batpath):
            self.__log.InLog("Create shortcut successful!")
            startcmd = "start " + "shortcut.bat"
            startpath = execpath + "\\shortcut.bat"
            cmd = "cd " + execpath + " && " + startcmd
            pop = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)
            pop.wait()
            if pop.returncode == 0:
                pop.terminate()
                pop.kill()
                SetFileAttributes(startpath, FILE_ATTRIBUTE_HIDDEN)

    def __init__(self):
        self._createShortcut()
        self.__cIPT = pt.IPackTool(False)
        # 告诉操作系统使用程序自身的dpi适配
        windll.shcore.SetProcessDpiAwareness(1)
        # 获取屏幕的缩放因子
        ScaleFactor = windll.shcore.GetScaleFactorForDevice(0)
        self.__root = Tk()
        self.__root.title("汉字机内码")
        self.__cIPT.IcoBase64Decoder('gta', gtaIco)
        self.__cIPT.IcoSetToTk(self.__root, 'gta')
        # 获取屏幕宽高
        self.__screenWidth = self.__root.winfo_screenwidth()
        self.__screenHeight = self.__root.winfo_screenheight()
        # 设置窗口大小
        self.__root.geometry(self._windowSize(int(self.__screenWidth * 0.35), int(self.__screenHeight * 0.625)))
        self.__root.wm_minsize(int(self.__screenWidth * 0.31), int(self.__screenHeight * 0.56))
        # 设置程序缩放
        self.__root.tk.call('tk', 'scaling', ScaleFactor / 75)

        self.__Lb_1 = Label(self.__root, text='汉字内容:', foreground='red')
        self.__Lb_1.place(relx=0.1, rely=0, relwidth=0.8, relheight=0.1)
        self.__Et_1 = Entry(self.__root)
        self.__Et_1.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.1)
        self.__Lb_2 = Label(self.__root, text='机内码如下:', foreground='red')
        self.__Lb_2.place(relx=0.1, rely=0.2, relwidth=0.8, relheight=0.1)
        self.__Et_3 = Entry(self.__root)
        self.__Et_3.place(relx=0.1, rely=0.3, relwidth=0.8, relheight=0.1)
        self.__Bt_1 = Button(self.__root, text='转化', command=lambda: self._run(str(self.__Et_1.get())))
        self.__Bt_1.place(relx=0.4, rely=0.45, relwidth=0.2, relheight=0.1)
        self.__root.mainloop()

    def _windowSize(self, p_width, p_height):
        size = '%dx%d+%d+%d' % (
            p_width, p_height, (self.__screenWidth - p_width) / 2, (self.__screenHeight - p_height) / 2)
        return size

    def _toUnicode(self, p_str):
        r_str = ""
        for v_char in p_str:
            r_str = r_str + hex(ord(v_char)).replace("0x", "") + " "
        return r_str

    def _toGb2312(self, p_bytes):
        r_str = ""
        count = 2
        for v_byte in p_bytes:
            r_str += hex(int(v_byte)).replace("0x", "")
            count -= 1
            if count == 0:
                r_str += " "
                count = 2
        return r_str

    def _run(self, p_context):
        try:
            Str1 = self._toGb2312(p_context.encode("gb2312")).strip()
            self.__Et_3.insert(0, Str1)
        except IOError:
            print("Error:转换失败!")
        else:
            print("Success:转换成功!")


class IGbToANSI:
    def __init__(self):
        self.__gta = GbToANSI()


mIGTA = IGbToANSI()

免责声明:由于该开源工具未经过正规和严格的测试,可能存在错误,因此造成的损失均由工具使用者自行承担。

如果这篇文章对你有帮助,请给作者点个赞吧!

你可能感兴趣的:(个人开发的小工具,python)