千家信息网

如何使用Python制作ASCII码转换器

发表于:2025-02-07 作者:千家信息网编辑
千家信息网最后更新 2025年02月07日,这篇文章主要为大家展示了"如何使用Python制作ASCII码转换器",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何使用Python制作ASCII码转换
千家信息网最后更新 2025年02月07日如何使用Python制作ASCII码转换器

这篇文章主要为大家展示了"如何使用Python制作ASCII码转换器",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何使用Python制作ASCII码转换器"这篇文章吧。

实现效果

使用 chr 和 ord 进行互转,

prtint(chr(98))

结果:b

print(ord(b))

结果:98

实现步骤

导入模块

import tkinterfrom tkinter import *from tkinter.ttk import *

创建画布并更改背景颜色添加纹理图片,如果图片不存在则执行exit()进行退出程序

canvas = tkinter.Canvas(root, bg="#ebebeb", height=400, width=700, borderwidth=-3)  # 创建画布canvas.pack(side='top')  # 放置画布(为上端)try:    image_file = tkinter.PhotoImage(file="./Along.png")  # 加载图片文件    canvas.create_image(0, 0, anchor='nw', image=image_file)  # 将图片置于画布上except:    exit()    pass

添加输入框和信息框

#输入信息var_Input_information = tkinter.StringVar()tkinter.Entry(root, width=20, borderwidth=1, bg='#ebebeb', textvariable=var_Input_information).place(x=29, y=160) #输入信息var_pick_up_information = tkinter.StringVar()tkinter.Entry(root, width=20, borderwidth=1, bg='#ebebeb', textvariable=var_pick_up_information).place(x=306, y=160)  #获取信息var_Input_information_2 = tkinter.StringVar()tkinter.Entry(root, width=20, borderwidth=1, bg='#ebebeb', textvariable=var_Input_information_2).place(x=29, y=210) #获取信息var_pick_up_information_2 = tkinter.StringVar()tkinter.Entry(root, width=20, borderwidth=1, bg='#ebebeb', textvariable=var_pick_up_information_2).place(x=306, y=210)

加标签

tkinter.Label(canvas, bg="#ebebeb", text='↓↓↓↓').place(x=364, y=184)tkinter.Label(canvas, bg="#ebebeb", text='↓↓↓↓').place(x=84, y=184)

ASCII_ord 是用来字符转ASCII码的,ASCII_chr是用来ASCII码转字符的,核心部位

def ASCII_ord():    try:        ord_ = ord(var_Input_information.get())        var_Input_information_2.set(ord_)    except:        var_Input_information_2.set('错误字符或多输入字符!!!') def ASCII_chr():    try:        chr_ = chr(int(var_pick_up_information.get()))        var_pick_up_information_2.set(chr_)    except:        var_pick_up_information_2.set('错误字符或多输入字符!!!')

加俩按钮

Button(root, text='字符转ASCII码', command=ASCII_ord).place(x=55, y=240)Button(root, text='ASCII码转字符', command=ASCII_chr).place(x=336, y=240)

执行程序

root.mainloop()

程序运行:

完整代码

import tkinterfrom tkinter import *from tkinter.ttk import *  root = Tk()root.title('贱工坊-ASCII码转换')  # 程序的标题名称root.geometry("480x320+512+288")  # 窗口的大小及页面的显示位置root.resizable(False, False)  # 固定页面不可放大缩小root.iconbitmap("picture.ico")  # 程序的图标 canvas = tkinter.Canvas(root, bg="#ebebeb", height=400, width=700, borderwidth=-3)  # 创建画布canvas.pack(side='top')  # 放置画布(为上端)try:    image_file = tkinter.PhotoImage(file="./Along.png")  # 加载图片文件    canvas.create_image(0, 0, anchor='nw', image=image_file)  # 将图片置于画布上except:    exit()    pass #输入信息var_Input_information = tkinter.StringVar()tkinter.Entry(root, width=20, borderwidth=1, bg='#ebebeb', textvariable=var_Input_information).place(x=29, y=160) #输入信息var_pick_up_information = tkinter.StringVar()tkinter.Entry(root, width=20, borderwidth=1, bg='#ebebeb', textvariable=var_pick_up_information).place(x=306, y=160)  #获取信息var_Input_information_2 = tkinter.StringVar()tkinter.Entry(root, width=20, borderwidth=1, bg='#ebebeb', textvariable=var_Input_information_2).place(x=29, y=210) #获取信息var_pick_up_information_2 = tkinter.StringVar()tkinter.Entry(root, width=20, borderwidth=1, bg='#ebebeb', textvariable=var_pick_up_information_2).place(x=306, y=210) tkinter.Label(canvas, bg="#ebebeb", text='↓↓↓↓').place(x=364, y=184)tkinter.Label(canvas, bg="#ebebeb", text='↓↓↓↓').place(x=84, y=184)  def ASCII_ord():    try:        ord_ = ord(var_Input_information.get())        var_Input_information_2.set(ord_)    except:        var_Input_information_2.set('错误字符或多输入字符!!!') def ASCII_chr():    try:        chr_ = chr(int(var_pick_up_information.get()))        var_pick_up_information_2.set(chr_)    except:        var_pick_up_information_2.set('错误字符或多输入字符!!!')Button(root, text='字符转ASCII码', command=ASCII_ord).place(x=55, y=240)Button(root, text='ASCII码转字符', command=ASCII_chr).place(x=336, y=240)root.mainloop()

打包一下,我们在当前python根目录运行cmd

运行指令

pyinstaller -i picture.ico ASCII.py --noconsole

-i 添加图标

--noconsole 运行程序时不出现命令框

-F 打包为单个文件

可以看到已经打包好了

以上是"如何使用Python制作ASCII码转换器"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

0