2011-03-04 97 views
4

我是Python的新手,我正在編寫程序只是爲了好玩。我的程序由三個.py文件組成(比如說a.py,b.py,c.py)。 a會根據用戶的選項調用b或c中的函數。完成第一輪後,它會詢問用戶是否想要繼續或簡單地退出程序。如果他們選擇繼續,它會再次詢問是否應該運行b或c。Python程序卡住

我遇到的問題是,第一次,一個將調用的功能,要麼完美的罰款,它運行順利,然後當我選擇繼續它再次調用任何功能完美的罰款,它會進入函數,但是這個函數在第一步就陷入了困境。

該程序沒有終止,沒有提供錯誤。它接受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 = '' 
+4

我的水晶球出去修理。你可以發佈一些代碼嗎? (將它粘貼到你的問題中,選擇它,然後按下Ctrl-K) – 2011-03-04 16:29:52

+0

你能提供一些源代碼嗎?那麼確定問題就容易多了。 – 2011-03-04 16:30:21

+0

您正在輸入raw_input提示符,對吧? – 2011-03-04 16:30:42

回答

2

您需要調整您的代碼只有一次創建根窗口,只調用mainloop一次。 Tkinter的設計不是爲了能夠在單個過程中多次創建和銷燬根。

如果您需要多個窗口,請使用Toplevel命令創建其他窗口。

+0

布賴恩,請你解釋我將如何做到這一點? – 2011-03-04 17:48:35

+0

好的。我用Toplevel。我得到惱人的空白tk彈出窗口(我可以擺脫這一點),但它仍然給我同樣的問題。第二次被卡住在同一個地方 – 2011-03-04 17:58:55

+0

@Chris Mena:沒有看到你修改過的代碼,不可能說出問題所在。 – 2011-03-04 18:09:54