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()
相关推荐
databook4 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室5 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三6 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户25191624271110 小时前
Python之语言特点
python
刘立军10 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机13 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机14 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i16 小时前
django中的FBV 和 CBV
python·django
c8i16 小时前
python中的闭包和装饰器
python
这里有鱼汤19 小时前
小白必看:QMT里的miniQMT入门教程
后端·python