我的python pyw文件没有打开,尽管没有错误

2024-09-30 01:26:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我发明了一种用于转换信息的密码。我想制作一个python程序来完成艰难的步骤(基本上是对消息进行编码和解码),并将代码保存到一个.pyw文件中。但它似乎没有打开,我已经将文件重命名为.py,当我运行它时,控制台窗口打开,没有错误,然后关闭。如果我通过IDLE运行程序,程序工作正常,没有错误,这是某种错误吗?我应该重新安装python或其他东西吗

from tkinter import Text,Button,Tk,Label,Scrollbar,Frame

alphabets={'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,'i':9,'j':10,'k':11,'l':12,'m':13,'n':14,'o':15,'p':16,'q':17,'r':18,'s':19,'t':20,'u':21,'v':22,'w':23,'x':24,'y':25,'z':26,' ':' '}
inv={k:v for v,k in alphabets.items()}

#scripting the program

def insert(msg,char,index):
    return msg[:index]+char+msg[index:]

def encode():
    msg=_input.get('1.0','end').strip().lower()
    if msg=='':
        return ''
    if msg[-1]=='\n':
        msg=msg[:-1]
    nonconvertable={}
    for x in msg:
        if x not in alphabets:
            print(x)
            nonconvertable.update({x:msg.index(x)})
            msg=msg.replace(x,'')
    char=[]
    msg=msg.split()
    for item in msg:
        for x in item:
            number=len(item)
            num=alphabets[x]-number
            if num<=0:
                num+=26
            char.append(num)
        char.append(' ')
    msg=''
    for x in char:
        msg+=inv[x]
    for x in nonconvertable:
        msg=insert(msg,x,nonconvertable[x])
    return msg[:-1]

def decode():
    msg=_input.get('1.0','end').strip()
    if msg=='':
        return 
    nonconvertable={}
    for x in msg:
        if x not in alphabets:
            nonconvertable.update({x:msg.index(x)})
            msg=msg.replace(x,'')
    char=[]
    msg=msg.lower().split()
    for item in msg:
        for x in item:
            number=len(item)
            num=alphabets[x]+number
            if num>26:
                num-=26
            char.append(num)
        char.append(' ')
    msg=''
    for x in char:
        msg+=inv[x]
    for x in nonconvertable:
        msg=insert(msg,x,nonconvertable[x])
    return msg[:-1]

#script for interaction of the user(through the GUI) with the script

def encodeGUI():
    msg=encode()
    output.delete('1.0','end')
    output.insert('1.0',msg)

def decodeGUI():
    msg=decode()
    output.delete('1.0','end')
    output.insert('1.0',msg)

#building the GUI

root=Tk()
root.state('zoomed')
root.title('converter')
root.configure(background='#747f91')

note=Label(root,text="NOTE: Don't use a dot(.) in your message",bg='#747f91')
note.grid(row=1,column=5)

inputlabel=Label(root,text='Input',bg='#747f91')
outputlabel=Label(root,text='Output',bg='#747f91')
inputlabel.grid(row=1,column=1)
outputlabel.grid(row=3,column=1)

_input=Text(root,height=15,borderwidth=1)
_input.grid(row=2,column=1)
frame1=Frame(root,width=_input.winfo_width(),height=_input.winfo_height())
frame2=Frame(root,width=_input.winfo_width(),height=_input.winfo_height())
inputscrollbar=Scrollbar(frame1,command=_input.yview)
frame1.grid(row=2,column=3)
frame2.grid(row=4,column=3)
inputscrollbar.pack(fill='y')
output=Text(root,height=15,borderwidth=1)
output.grid(row=4,column=1)
outputscrollbar=Scrollbar(frame2,command=output.yview)
outputscrollbar.pack(fill='y')

encodebutton=Button(root,text='encode',command=encodeGUI,bg='#6179c9')
decodebutton=Button(root,text='decode',command=decodeGUI,bg='#6179c9')
encodebutton.grid(row=4,column=4)
decodebutton.grid(row=4,column=6)

Tags: inforinputoutputifcolumnmsgroot

热门问题