Why does this code only runs properly in only the python shell

Question:

I have this code

from tkinter import *
root = Tk()

It works fine on the shell but not in the IDE

New Contributor 😉
Also explain the reason

Thank you 🙂

Edit

IDE – PyCharm

Asked By: cooldude

||

Answers:

You will always need to call a mainloop() at the end of the script

Why does that code works perfectly fine in the Python Shell?

Generally Speaking, The Python Shell interprets only one line at a time and once you call the mainloop() you won’t be able to do anything with your tkinter window so in python shell it would be very inconvenient if you really think about it.

Your Updated Code

from tkinter import *
root = Tk()
root.mainloop()
Answered By: Belal Ahmed

I would also like to point out that TkObject.mainloop() also needs to be at the end of the script

Answered By: cooldude
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.