Python程序卡住了

4
我刚开始学习Python,正在写一个有趣的程序。我的程序包含三个.py文件(假设为a.py、b.py、c.py)。a将根据用户的选择调用b或c中的函数。第一轮完成后,它会询问用户是要继续还是退出程序。如果他们选择继续,它会再次询问是否运行b或c。
我遇到的问题是,在第一次执行时,a会完美地调用b或c中的函数,它运行得很顺利,然后当我选择继续时,它再次完美地调用两个函数中的任意一个,它会进入函数,但是函数会在第一步卡住。
程序没有终止,也没有报错。它接受了输入变量,但不会继续执行。我想知道是否有某种方法来强制它接受变量,然后继续处理(使其'解锁')。我已经尝试在下一行上放置'pass',但没有效果。
以下是从请求继续开始执行的步骤:
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 = ''

4
我的水晶球正在维修中。你可以发一些代码吗?(将代码粘贴到你的问题中,选择它,按Ctrl-K) - Tim Pietzcker
你能提供一些源代码吗?这样更容易确定问题所在。 - Morten Kristensen
1
@Tim 啊,现在水晶球备件真是难以找到。我的已经有很长时间没正常工作了,我需要看一些代码。 - salezica
没事了,就是在那里卡住了。我只是跳过了把我带到那里的代码片段。 - Chris Mena
好的。这些是到它卡住的步骤。有什么想法吗?对于所有的混乱,我很抱歉。 - Chris Mena
显示剩余2条评论
1个回答

2
您需要重构您的代码,只创建一次根窗口,并且只调用mainloop一次。Tkinter不设计为在单个进程中多次创建和销毁根窗口。
如果您需要多个窗口,请使用Toplevel命令创建其他窗口。

Bryan,你能否解释一下如何做到这点? - Chris Mena
好的。我使用了Toplevel。我得到了令人讨厌的空白tk弹出窗口(我可以摆脱它),但它仍然给我同样的问题。第二次运行时,它会在同一个地方卡住。 - Chris Mena
@Chris Mena:没有看到你修改的代码,很难说出问题所在。 - Bryan Oakley
@Chris Mena:我仍然看到有破坏根的代码,而且我看到你试图在toplevel上调用mainloop。你似乎还在使用消息对话框之后在函数中创建根。这些都是不正确的。Tk的工作方式是,在程序开始时只创建一次根对象,并且在整个程序生命周期内运行单个主循环。当它退出时,你的程序应该退出。 - Bryan Oakley
可能一个 tk 文件对话框的情况会更好一些? - Chris Mena
@Chris Mena:我不理解这个问题,但我非常确定答案是“不”。问题在于你只是错误地使用了Tkinter。你需要创建一个单一的根,只调用一次mainloop,然后使用Toplevel创建任意数量的窗口。它们都将共享同一个事件循环。 - Bryan Oakley

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接