0
votes

I am having a problem between the python shell in my IDE and the Tkinter window. What I am trying to do is have all of my user input in the shell, and then I would like to output the corresponding information in a Tkinter window.

However, when my window is made and pops up, I close it to continue my program in the shell, then I continue with input. However, when I try to reinitialize my window. It says that the window has been destroyed. I understand what this means so I tried having a Toplevel window where I output my info which can be closed, and hide my root window, but the shell will not continue until I close/destroy the root window as well.

Is there a way I can continue in the shell without destroying my root window? I am fairly new to this language so any help would be very much appreciated.

This is my general idea:

from Tkinter import *

#get all my info from the shell

root = Tk()
root.withdraw()  #hide the root window

main = Toplevel()
     #this is the window that I want to be able to close and open later

#get more info from the shell after main is closed

#now I want to open the updated main window

Thanks in advance! (And I am working on Windows if that matters)

2

2 Answers

1
votes

I'm not sure if the way you are trying to do this is the most efficient way, but i would propose these changes so far:

from Tkinter import *

#get all my info from the shell

window = Tk()
window.iconify()  #hide the root window

#get more info from the shell after main is closed

window.deiconify()
window.mainloop() # to handle events

i renamed your root-Window to make it more clear for you whats happening and removed the superflous (imho) additional Toplevel-window!

Also keep in mind, that you won't accomplish anything without the mainloop and the necessary event-handlers!

1
votes

Simply put, this is not how Tkinter is designed to work. Tkinter was designed to have a single root window that is created once, and with a single eventloop that is running. Using it any other way is bound to lead to undesired behavior.

If you really need code to work this way, gather your input from your shell in one process, then use a separate process to display the tkinter window. You can either communicate from one the other using a socket, or you could pass the data from the parent to the child via arguments or environment variables or temporary files.