0

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?

  • Possible duplicate of [Turtle graphics - How do I control when the window closes?](https://stackoverflow.com/questions/6234798/turtle-graphics-how-do-i-control-when-the-window-closes) – Ankit Agrawal Jul 21 '19 at 14:29
  • in `turtle` similar to `tkinter` you should run `mainloop()` in last line. – furas Jul 21 '19 at 14:31
  • Awesome, this solved my problem thanks @furas –  Jul 21 '19 at 14:34

1 Answers1

0

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.

furas
  • 134,197
  • 12
  • 106
  • 148