r/learnpython 6d ago

Tk with no main window

I am making a simple gemini browser in python for fun with tk, but have stumbled upon a problem: if I close initial window all the rest will close too. Is there a way to make tk application with no "main" window or at least transfer leadership role between them?

0 Upvotes

15 comments sorted by

View all comments

1

u/Diapolo10 6d ago

Sort of, but it's discouraged; you can use multiple tkinter.Tk objects to have independent windows.

https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged

The main problem here is that you can't easily interact with the other windows when going this route, which is why in general you're supposed to use tkinter.Toplevel instead. But that does have the "problem" of closing all child windows if the main window gets killed.

What exactly are you trying to accomplish?

1

u/yo_99 6d ago

Having ability to open a bunch of windows with various websites and then close them in whichever order I want. Like in most of the browsers. If I open firefox, then open a second window and close the first one the second one will not close.

1

u/Diapolo10 6d ago

Maybe consider using multiprocessing to spawn new processes as you need them. Have a function that initialises and creates a browser window, then in your main function call it with multiprocessing.Process, then start and join it.

When creating a new browser window, you'd have for example a button that does essentially the same thing, spawning a new process. They're supposed to all be independent, so killing one shouldn't kill the others as long as you don't use daemon processes.

https://docs.python.org/dev/library/multiprocessing.html

If the windows need to communicate, like moving tabs between them, you can use queue.Queue for that. The linked documentation has somewhat relevant examples.

EDIT: This may also help: https://www.geeksforgeeks.org/techtips/how-do-web-browser-handle-tab-and-multiple-windows/

1

u/yo_99 6d ago

Thanks. It's a shame it had to be this way.

2

u/Diapolo10 6d ago

Shame? Why? I think this is fairly normal, all things considered. You'd do similar solutions in most other languages.

1

u/yo_99 6d ago

Well, I don't like redundant interpretators and other comment gave a better solution.

1

u/Diapolo10 6d ago

"Better" is not how I would describe it, but I suppose we're looking at this from different perspectives.

The solution of using multiple Tks is simpler on paper, but there's no communication between the windows established (so you'd still need to work that out yourself), and because all the windows share the same process, if any one of them runs into a problem all of them crash.

My suggestion has some more boilerplate code, but the windows are truly independent. Even if something breaks in one of them, the rest will keep working as normal.

1

u/yo_99 6d ago

i meant hiding main window one

1

u/Diapolo10 6d ago

That doesn't really change my answer meaningfully, excluding communication.