Python应用—获得汉字的unicode编码

1.需求

在编程中有时需要获得汉字的unicode 16进制编码,比如正则表达式。该小程序可以输入汉字后返回其Unicode编码,十分方便。

2.代码实现

复制代码
import PySimpleGUI as sg

# 定义一个函数来获取并显示Unicode编码
def show_unicode(input_text):
    # 获取输入框中的内容
    input_text = input_text
    # 遍历字符串中的每个字符,并转换为十六进制的Unicode编码
    unicode_hex = '\n'.join([f"字符: {char}, Unicode编码(十六进制): {ord(char):X}" for char in input_text])
    # 显示每个字符的Unicode编码
    sg.popup("Unicode编码(十六进制)", unicode_hex)

layout = [
    [sg.Text('请输入中文字符:')],
    [sg.Input(key='input_text', size=(20, 1))],
    [sg.Button('显示Unicode编码'), sg.Button('退出')]
]

# 创建窗口
window = sg.Window('Unicode编码显示', layout)

# 事件循环
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == '退出':
        break
    if event == '显示Unicode编码':
        input_text = values['input_text']
        show_unicode(input_text)

# 关闭窗口
window.close()
相关推荐
胡萝卜3.05 小时前
掌握C++ map:高效键值对操作指南
开发语言·数据结构·c++·人工智能·map
电子_咸鱼5 小时前
【STL string 全解析:接口详解、测试实战与模拟实现】
开发语言·c++·vscode·python·算法·leetcode
哈茶真的c6 小时前
【书籍心得】左耳听风:传奇程序员练级攻略
java·c语言·python·go
沐知全栈开发6 小时前
ionic 选项卡栏操作详解
开发语言
曹牧6 小时前
C#中,#region和#endregion
开发语言·c#
顾安r6 小时前
11.22 脚本打包APP 排错指南
linux·服务器·开发语言·前端·flask
蒙小萌19937 小时前
Swift UIKit MVVM + RxSwift Development Rules
开发语言·prompt·swift·rxswift
io_T_T7 小时前
Paddle-CLS图像分类_环境安装
python·日常软硬件经验分享
Z***25807 小时前
Java爬虫框架
java·开发语言·爬虫
百***48077 小时前
Python使用PyMySQL操作MySQL完整指南
数据库·python·mysql