【问题标题】:Python program getting stuckPython程序卡住了
【发布时间】:2011-03-05 03:02:36
【问题描述】:

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

我遇到的问题是,第一次时,a 会以非常正常的方式调用函数,它运行顺利,然后当我选择继续时,它再次完美调用任一函数,它将进入函数,但随后该函数在第一步中被卡住了。

程序没有终止,没有报错。它接受 raw_input 变量,但不会继续。我想知道是否有某种方法可以强制它接受变量然后继续该过程(让它“解开”)。我已经尝试将 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 = ''

【问题讨论】:

  • 我的水晶球要维修了。你能发布一些代码吗? (将其粘贴到您的问题中,选择它,然后按 Ctrl-K)
  • 你能提供一些源代码吗?这样就更容易确定问题了。
  • 你在 raw_input 提示符下按回车,对吧?
  • @Tim Ahh,现在水晶球备件太难买到了。我的已经很久没有正常工作了,我也需要看看一些代码。
  • 哇,我的生活糟透了。这不是它卡住的地方。对于那个很抱歉。这是我第一次使用论坛。不知道这些东西是如何工作的。好吧,它弄乱了源代码的格式。但是您仍然可以或多或少地弄清楚发生了什么。

标签: python tkinter


【解决方案1】:

您需要重新构建代码,只创建一次根窗口,并且只调用一次mainloop。 Tkinter 并非旨在能够在单个进程中多次创建和销毁根。

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

【讨论】:

  • 布莱恩,你能解释一下我会怎么做吗?
  • 好的。我用过顶级。我得到了烦人的空白 tk 弹出窗口(我可以摆脱它),但它仍然给我同样的问题。第二次它卡在同一个地方
  • @Chris Mena:没有看到您修改过的代码,就不可能说出问题所在。
  • @Chris Mena:我仍然看到破坏根目录的代码,并且我看到您正试图在顶层调用 mainloop。使用消息对话框后,您似乎也在函数中创建了根。这些都不对。 Tk 的工作方式是,在程序开始时创建一次根对象,并且在程序的整个生命周期中运行一个主循环。当它退出时,你的程序应该退出。
  • tk 文件对话框的情况会不会好一点?
猜你喜欢
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-24
相关资源
最近更新 更多