【发布时间】: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,现在水晶球备件太难买到了。我的已经很久没有正常工作了,我也需要看看一些代码。
-
哇,我的生活糟透了。这不是它卡住的地方。对于那个很抱歉。这是我第一次使用论坛。不知道这些东西是如何工作的。好吧,它弄乱了源代码的格式。但是您仍然可以或多或少地弄清楚发生了什么。