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()
相关推荐
小陈工2 小时前
Python安全编程实践:常见漏洞与防护措施
运维·开发语言·人工智能·python·安全·django·开源
是娇娇公主~8 小时前
C++ 中 std::deque 的原理?它内部是如何实现的?
开发语言·c++·stl
2401_874732538 小时前
为你的Python脚本添加图形界面(GUI)
jvm·数据库·python
SuperEugene8 小时前
Axios 接口请求规范实战:请求参数 / 响应处理 / 异常兜底,避坑中后台 API 调用混乱|API 与异步请求规范篇
开发语言·前端·javascript·vue.js·前端框架·axios
FreakStudio8 小时前
0 元学嵌入式 GUI!保姆级 LVGL+MicroPython 教程开更,从理论到实战全搞定
python·单片机·嵌入式·面向对象·电子diy
xuxie999 小时前
N11 ARM-irq
java·开发语言
wefly201710 小时前
从使用到原理,深度解析m3u8live.cn—— 基于 HLS.js 的 M3U8 在线播放器实现
java·开发语言·前端·javascript·ecmascript·php·m3u8
luanma15098010 小时前
PHP vs C++:编程语言终极对决
开发语言·c++·php
寂静or沉默10 小时前
2026最新Java岗位从P5-P7的成长面试进阶资源分享!
java·开发语言·面试
剑穗挂着新流苏31210 小时前
117_PyTorch 实战:利用训练好的模型进行单张图片验证
人工智能·python·深度学习