I am using Tkinter in python 2.7, and I noticed that sometimes the root window doesn't open until I start the mainloop method.
I addition, in those cases, when close the window, the program is ending (the root.mainloop is the last line in the code), and in the other cases it's not happening.
Why is it happening?
The short answer is that it's happening because that is how it was designed to happen.
Nothing happens in tkinter except through the event loop. Even something as simple as the window appearing is the event loop responding to an event. For example, when you create the window (and assuming you don't withdraw it), a "redraw yourself" event in effect gets added to the event queue. When you start the event loop, that's one of the first events it processes and the window appears.
When you call mainloop(), that causes the event loop to start processing events. It is designed to start processing events and not return until the root window has been destroyed. That is why the call to mainloop is typically the last line of code in the file, since there's usually nothing else to do once the user has closed the window.
The other way to process events is to call update or update_idletasks. update will start the event loop, and return once all pending events have been processed (ie: it doesn't wait for the window to be destroyed). update_idletasks is similar, but only updates "idle" events: things scheduled with after, events related to redrawing windows, and a few others. It doesn't process user-generated events like mouse and button clicks.
Why doesn't Tkinter.Tk() open the window right away?