0

I started looking at some Tkinter tutorials. I always like to mess around with code before going on with the next chapter: for instance I tried to open more than one independent root.

from Tkinter import *

def main():
    root1, root2 = Tk(), Tk()
    root1.title("First window")
    root2.title("Second window")
    root1.mainloop()
    root2.mainloop()

main()

What I don't understand is why this code works (i.e. it shows two different windows at the same time). If the Tk "mainloop" method is a loop, why do I see the second window? Shouldn't I see it only after closing the first window (i.e. after breaking the first while loop)?

Please, explain me how it works

Pigna
  • 2,792
  • 5
  • 29
  • 51
  • 1
    It is irrelevant how it works (or doesn't work). This isn't correct usage of Tkinter. You should only ever create exactly once instance of `Tk` at a time. – Bryan Oakley Sep 09 '16 at 17:56
  • This question does not show any research effort. It is indeed just a basic fundamental concept. – Parviz Karimli Sep 09 '16 at 18:21
  • Either it is irrelevant or it is a basic fundamental concept ... :/ I don't get it. I'm checking out 4-5 tutorials and neither makes any reference to this – Pigna Sep 09 '16 at 18:29
  • You have created two instances of `Tk` at the same time; then given a `mainloop()` for each of them. A `Tk` window pops-up right after you create it. Again, you have defined two of them at the same time which means they both will pop-up at the same time when the program starts. It is simple as that. What else were you expecting? That is why I am telling it is a fundamental concept of `Tkinter`. If you have not understood it, I would recommend you to start from the the beginning -- the basics -- again (if you actually had done it before). – Parviz Karimli Sep 09 '16 at 18:46
  • 1
    "why do I see the second window? Shouldn't I see it only after closing the first window?" -Nope. In order to get that check this code out: http://pastebin.com/iLPsU7Mc – Parviz Karimli Sep 09 '16 at 18:47
  • In the pastebin link I provided above as you can see, first, I have created the first instance of `Tk`, then given it a title, then put it in a `mainloop`, and only after that I have created the second instance of `Tk`, and then the same process with this one too. But the problem is not this. The problem is as @BryanOakley has already stated [above](http://stackoverflow.com/questions/39417091/tkinter-why-do-both-windows-open#comment66159271_39417091): "This isn't correct usage of Tkinter. You should only ever create exactly once instance of `Tk` at a time." – Parviz Karimli Sep 09 '16 at 18:58
  • Possible duplicate of [Is each instance of Tk() class running independent of each other?](http://stackoverflow.com/questions/36141010/is-each-instance-of-tk-class-running-independent-of-each-other) – Parviz Karimli Sep 09 '16 at 19:03

1 Answers1

1

EDIT: Removed incorrect answer, added answer below

The short of why are confused is that the behavior of your code is undefined. Really, it isn't running as differently as you think. Try running this code:

from Tkinter import *

def main():
    root1, root2 = Tk(), Tk()
    root1.title("First window")
    root2.title("Second window")

    root1.mainloop()
    print 'root1.mainloop() has finished running.'
    root2.mainloop()
    print 'root2.mainloop() has finished running.'

main()

So the first mainloop doesn't actually continue on immediately. It hangs on root1.mainloop() (as you expected) until you close both of the windows, then it continues on. I am not sure exactly why running root1.mainloop() opens both windows as I don't have access to all of the source code but I think it would be a little bit like asking about the results of 0/0. It just isn't supposed to happen.

qfwfq
  • 2,416
  • 1
  • 17
  • 30
  • 1
    no, tkinter runs in a single thread -- whichver thread it is created in. A good, concise description is here: http://stackoverflow.com/a/38767665/7432 – Bryan Oakley Sep 09 '16 at 18:59
  • Sorry about that (to anyone that read my original answer). I am editing it now. – qfwfq Sep 10 '16 at 01:01