4

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 = ''
7
  • 4
    My crystal ball is out for repairs. Could you post some code? (Paste it into your question, select it, and press Ctrl-K) Commented Mar 4, 2011 at 16:29
  • Could you perhaps supply some source code? It's easier to determine the problem then. Commented Mar 4, 2011 at 16:30
  • You are hitting enter at the raw_input prompt, right? Commented Mar 4, 2011 at 16:30
  • 1
    @Tim Ahh, crystal ball spare parts are so damn hard to come by these days. Mine hasn't been working right for ages, I'm gonna need to see some code as well. Commented Mar 4, 2011 at 16:35
  • Wow I suck at life. That's not where it gets stuck. Sorry about that. This is the first time I use a forum. Don't know how these things work. Well it messed with the formatting of the source code. But you can still figure out more or less what's going on. Commented Mar 4, 2011 at 16:44

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Bryan, could you please explain how I would do that?
OK. I used Toplevel. I get the annoying blank tk popup (I can get rid of that) but it still gives me the same problem. Second time around it gets stuck in the same place
@Chris Mena: without seeing your modified code it's impossible to say what the problem is.
@Chris Mena: I still see code that destroys the root, and I see you're trying to call mainloop on the toplevel. You also seem to create the root in a function after you use the message dialog. None of that is correct. The way Tk works is, you create the root object once at the start of your program, and you have a single mainloop running for the life of the program. When it exits your program should exit.
Would a tk filedialog situation possibly work a little better?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.