Python程序卡住了

我是
Python新手,我正在编写一个程序只是为了好玩.我的程序包含三个.py文件(假设是a.py,b.py,c.py). a将调用b或c中的函数,具体取决于用户的选项.完成第一轮后,它会询问用户是想继续还是只是退出程序.如果他们选择继续,它会再次询问是否应该运行b或c.

我遇到的问题是,第一次,一个将调用函数完全正常,它运行平稳,然后当我选择继续它再次调用任何函数完全正常,它将进入函数,但然后该功能在第一步中陷入困境.

该程序没有终止,没有给出错误.它接受raw_input变量,但不会继续.我想知道是否有某种方法迫使它接受变量,然后继续这个过程(让它“解开”).我已经尝试过传递下一行了.那没用.

以下是从请求继续开始所需的步骤:

Continue = tkMessageBox.askyesno('Cypher Program', 'I have completed the task'
                          + '\nWould you like to do anything else?')

## This is in a.py;
if Continue == True:
    cyp()
def cyp():
    global root
    root = Tk()

    root.title("Cypher Program")
    root['padx'] = 40
    root['pady'] = 20

    textFrame = Frame(root)


    Label(root, text = 'What would you like to do?').pack(side = TOP)
    widget1 = Button(root, text = 'Encrypt a file', command = encrypt)
    widget1.pack(side = LEFT)

    widget2 = Button(root, text = 'Decrypt a file', command = decrypt)
    widget2.pack(side = RIGHT)

    widget3 = Button(root, text = 'Quit', command = quitr)
    widget3.pack(side = BOTTOM)
    root.mainloop()

def encrypt():
    root.destroy()
    encrypt3.crypt()

##Then from there it goes to b.py;
def crypt():
    entry('Enter a file to encrypt:', selectFile)

def entry(msg1, cmd):
    global top
    top = Toplevel()  ##changed it to Toplevel

    top.title("File Encrypion")
    top['padx'] = 40
    top['pady'] = 20

    textFrame = Frame(top)

    entryLabel = Label(textFrame)
    entryLabel['text'] = msg1
    entryLabel.pack(side = LEFT)

    global entryWidget
    entryWidget = Entry(textFrame)
    entryWidget['width'] = 50
    entryWidget.pack(side = LEFT)

    textFrame.pack()

    button = Button(top, text = "Submit", command = cmd)
    button.pack()
    button.bind('<Return>', cmd)

    top.mainloop()

def selectFile():
    if entryWidget.get().strip() == "":
        tkMessageBox.showerror("File Encryption", "Enter a file!!")
    else:
        global enc
        enc = entryWidget.get().strip() + '.txt'
        top.destroy()   ##gets stuck here

##This is the rest of crypt(). It never returns to the try statement
try:
    view = open(enc)
except:
    import sys
    sys.exit(badfile())
    text = ''

最佳答案 您需要重新构建代码以仅创建一次根窗口,并且只调用一次mainloop. Tkinter的设计不能在单个进程中多次创建和销毁root.

如果需要多个窗口,请使用Toplevel命令创建其他窗口.

点赞