Chris Mena
Chris Mena

Reputation: 35

Python program getting stuck

I am new to Python and I am writing a program just for fun. My program consists of three .py files (let's say a.py, b.py, c.py). a will either call on the functions within either b or c, depending on the user's option. After it completes its first round it asks if the user would like to continue or simply exit the program. If they chose continue it asks again whether it should run b or c.

The problem I ran into is that the first time around, a will call the functions in either perfectly fine, it runs smoothly, and then when I select to continue it again calls either function perfectly fine, it will go into the function, but then the function gets stuck in its first step.

The program is not terminating, not giving an error. It accepts the raw_input variable but it will not continue. I was wondering if there was some way to force it to accept the variable and then continue the process (getting it 'unstuck'). I already tried putting pass on the next line. That didn't work.

Here are the steps it takes starting from the request to continue:

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 = ''

Upvotes: 4

Views: 2009

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385950

You need to restructure your code to only create the root window once, and only call mainloop once. Tkinter is not designed to be able to create and destroy the root multiple times in a single process.

If you need multiple windows, create additional windows with the Toplevel command.

Upvotes: 2

Related Questions