时间:2022-11-13 10:15:00 | 栏目:Python代码 | 点击:次
使用 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 打包为单个文件
可以看到已经打包好了