1

I have multiple .py tkinter GUIs that I want to combine. I did not initially plan on combining them into one GUI, so I have many variables in common as well as global commands which makes putting each window in a function in order to combine it under one .py file difficult. So I was trying to see if I can get buttons to open the applications separately to get the same thing done. I have a menu with some buttons and it works, however if I close one window and return to the menu, I cannot "reimport" a .py file so I cannot go into a new window, close it and go back. Here's my code:

def newWindow():
    root.destroy()
    import newFile

button1 = tk.Button(root, command=newWindow)
button1.pack()

To make the issue more clear: Say I start off on the menu. I click a button to close the menu and open a new window with a separate program on it. I close that program which automatically reopens the menu. However, now when I open the program from the menu again, the menu closes, but the program will not reopen as it has already been imported.

1 Answer 1

1

Alright @drsom, this should work, where you import py1 you should instead have from py1 import func1 then func1() on the next line (obviously this means you will have to create func1 and func2 like I've done below), this for some reason will allow you to carry on forever, I don't know why plain importing won't. In addition to this you must add (after the function) if __name__ == '__main__': and func2() on the next line to run that function the first time and start the cycle.

a small example:

py1:

def func1():
    import tkinter as tk

    root1 = tk.Tk()

    def kill1():
        root1.destroy()
        from py2 import func2
        func2()

    button1 = tk.Button(root1, bg = 'green', text = 'hit to kill py1 and start py2', command = kill1)
    button1.pack()

    root1.mainloop()

if __name__ == '__main__':
    func1()

py2:

def func2():
    import tkinter as tk

    root2 = tk.Tk()

    def kill2():
        root2.destroy()
        from py1 import func1
        func1()

    button2 = tk.Button(root2, bg = 'red', text = 'hit to kill py2 and start py1', command = kill2)
    button2.pack()

    root2.mainloop()

if __name__ == '__main__':
    func2()

This should work, but may cause some issues if the code is meant to carry on after the original function. I'm not sure what else you wanted, but just ask :), hope this helps you.

1
  • This works great and it addresses the problem of 'reimpoting'. It is actually only importing once and running the function multiple times, which works great. Just to mention, I found a more personally acceptable solution using "import runpy", but since you address the original issue I will consider this the confirmed answer.
    – somil
    Aug 7, 2014 at 15:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.