PyCharm automatically closes pop-up windows, can I stop this?

I am rather new to using PyCharm by JetBrains, but I am just messing around with tkinter and turtle-graphics and whenever I run some code for example:

t = turtle.Pen()
t.forward(50)

PyCharm will successfully create a turtle window and draw the line, however, the window opened just closes instantly. The same goes if I use tkinter.

Is there anyway to stop PyCharm doing this, and keep the window open?

Answer

In turtle which is built on top of tkinter you should run mainloop() in last line like in tkinter

Turtle:

import turtle

t = turtle.Pen()
t.forward(50)

turtle.mainloop()

Tkinter:

import tkinter as tk

root = tk.Tk()

b = tk.Button(root, text='Close', command=root.destroy)
b.pack()

root.mainloop()

It gets key/mouse events from system and send to widgets, check pressed buttons, etc. – so it keeps window open till you press closing button.

Leave a Reply

Your email address will not be published. Required fields are marked *