首页 > 编程技术 > python

基于Python制作ASCII码转换器

发布时间:2022-2-7 13:10 作者:小木_.

实现效果

使用 chr 和 ord 进行互转,

prtint(chr(98))    

结果:b

print(ord(b))

结果:98

实现步骤

导入模块

import tkinter
from 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 tkinter
from 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码转换器的文章就介绍到这了,更多相关Python ASCII码转换器内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

原文出处:https://blog.csdn.net/weixin_46625757/article/details/122801

标签:[!--infotagslink--]

您可能感兴趣的文章: