溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用Python制作ASCII碼轉(zhuǎn)換器

發(fā)布時間:2022-02-08 09:22:34 來源:億速云 閱讀:188 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“如何使用Python制作ASCII碼轉(zhuǎn)換器”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“如何使用Python制作ASCII碼轉(zhuǎn)換器”這篇文章吧。

實現(xiàn)效果

如何使用Python制作ASCII碼轉(zhuǎn)換器

使用 chr 和 ord 進(jìn)行互轉(zhuǎn),

prtint(chr(98))    

結(jié)果:b

print(ord(b))

結(jié)果:98

實現(xiàn)步驟

導(dǎo)入模塊

import tkinter
from tkinter import *
from tkinter.ttk import *

創(chuàng)建畫布并更改背景顏色添加紋理圖片,如果圖片不存在則執(zhí)行exit()進(jìn)行退出程序

canvas = tkinter.Canvas(root, bg="#ebebeb", height=400, width=700, borderwidth=-3)  # 創(chuàng)建畫布
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)

加標(biāo)簽

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

ASCII_ord 是用來字符轉(zhuǎn)ASCII碼的,ASCII_chr是用來ASCII碼轉(zhuǎn)字符的,核心部位

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='字符轉(zhuǎn)ASCII碼', command=ASCII_ord).place(x=55, y=240)
Button(root, text='ASCII碼轉(zhuǎn)字符', command=ASCII_chr).place(x=336, y=240)

執(zhí)行程序

root.mainloop()

程序運行:

如何使用Python制作ASCII碼轉(zhuǎn)換器

完整代碼

import tkinter
from tkinter import *
from tkinter.ttk import *
 
 
root = Tk()
root.title('賤工坊-ASCII碼轉(zhuǎn)換')  # 程序的標(biāo)題名稱
root.geometry("480x320+512+288")  # 窗口的大小及頁面的顯示位置
root.resizable(False, False)  # 固定頁面不可放大縮小
root.iconbitmap("picture.ico")  # 程序的圖標(biāo)
 
canvas = tkinter.Canvas(root, bg="#ebebeb", height=400, width=700, borderwidth=-3)  # 創(chuàng)建畫布
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('錯誤字符或多輸入字符!?。?#39;)
 
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='字符轉(zhuǎn)ASCII碼', command=ASCII_ord).place(x=55, y=240)
Button(root, text='ASCII碼轉(zhuǎn)字符', command=ASCII_chr).place(x=336, y=240)
root.mainloop()

打包一下,我們在當(dāng)前python根目錄運行cmd

如何使用Python制作ASCII碼轉(zhuǎn)換器

運行指令

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

-i  添加圖標(biāo)

--noconsole   運行程序時不出現(xiàn)命令框

-F   打包為單個文件

如何使用Python制作ASCII碼轉(zhuǎn)換器

可以看到已經(jīng)打包好了

如何使用Python制作ASCII碼轉(zhuǎn)換器

以上是“如何使用Python制作ASCII碼轉(zhuǎn)換器”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI