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()
相关推荐
m0_7349497917 小时前
MySQL如何配置定时清理过期备份文件_find命令与保留周期策略
jvm·数据库·python
t***54417 小时前
Clang 编译器在 Orwell Dev-C++ 中的局限性
开发语言·c++
m0_5145205718 小时前
MySQL索引优化后性能没提升_通过EXPLAIN查看索引命中率
jvm·数据库·python
H Journey18 小时前
Python 国内pip install 安装缓慢
python·pip·install 加速
oy_mail18 小时前
QoS质量配置
开发语言·智能路由器·php
oyzz12018 小时前
PHP操作redis
开发语言·redis·php
nashane18 小时前
HarmonyOS 6学习:网络能力变化监听与智能提示——告别流量偷跑,打造贴心网络感知应用
开发语言·php·harmony app
Polar__Star19 小时前
如何在 AWS Lambda 中正确使用临时凭证生成 S3 预签名 URL
jvm·数据库·python
凌波粒19 小时前
Java 8 “新”特性详解:Lambda、函数式接口、Stream、Optional 与方法引用
java·开发语言·idea
m0_7436239219 小时前
React 自定义 Hook 的命名规范与调用规则详解
jvm·数据库·python