Tkinter window closes automatically after Python program has run in PyCharm

Bert Carremans

I am programming a small Python game in PyCharm. I am doing this on a Macbook with Python version 3.4. The game opens a Tkinter window and adds some stuff to it. However, when running the game, it shows up very briefly and closes immediately.

I found some tips here on Stackoverflow to add input('Press to close the window') at the end of the game. Indeed, this ensures that the window is not closed immediately, but it is not practical for the game. In the game, the user needs to use his arrow keys to play. So adding the input(...) is not useful in this case. How can I prevent the window to close automatically? Thanks!

Below the code:

from tkinter import *

# Scherm maken
HEIGHT = 500
WIDTH = 800
window = Tk()
window.title('Bellenschieter')
c = Canvas(window,width=WIDTH, height=HEIGHT, bg='darkblue')
c.pack()



# Duikboot maken
ship_id = c.create_polygon(5,5,5,25,30,15,fill='red')
ship_id2 = c.create_oval(0,0,30,30,outline='red')
SHIP_R = 15
MID_X = WIDTH/2
MID_Y = HEIGHT/2
c.move(ship_id, MID_X, MID_Y)
c.move(ship_id2, MID_X, MID_Y)

# Duikboot besturen
SHIP_SPD = 10
def move_ship(event):
    if event.keysym == 'Up':
        c.move(ship_id, 0, -SHIP_SPD)
        c.move(ship_id2, 0, -SHIP_SPD)
    elif event.keysym == 'Down':
        c.move(ship_id, 0, SHIP_SPD)
        c.move(ship_id2, 0, SHIP_SPD)
    elif event.keysym == 'Left':
        c.move(ship_id, -SHIP_SPD, 0)
        c.move(ship_id2, -SHIP_SPD, 0)
    elif event.keysym == 'Right':
        c.move(ship_id, SHIP_SPD, 0)
        c.move(ship_id2, SHIP_SPD, 0)
c.bind_all('<Key>', move_ship)

window.update()

input('Press <Enter> to end the program')
falsetru

Start a event loop after setting up widgets, event handlers.

# input('Press <Enter> to end the program')  # (X)
window.mainloop()  # OR mainloop()

Remove the call to the input.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Tkinter window closes automatically after Python program has run in PyCharm

From Dev

After the program is run, the window generated by the program is blank and has no components

From Dev

go to previous window automatically after printing dialog closes

From Dev

Console window closes immediately when I run the program

From Dev

Window is closing immediately after I run program in PyQt 4 (anaconda) [PyCharm 4.5]

From Dev

exit python program without close Tkinter window

From Dev

how to a get text from python Tkinter entry after window has closed?

From Dev

How to run a python program(containing tkinter) on startup

From Dev

PyQt window closes after launch

From Dev

PyQt window closes after opening

From Dev

How to run a Python program without a window?

From Dev

Rebooting program after input() automatically on python

From Dev

Clear PyCharm Run Window

From Dev

C Win32: Window Automatically closes

From Dev

Python Tkinter Crashing, every run but program continues? Final polishing of Tkinter

From Dev

Inno Setup: How to automatically run program after /verysilent install?

From Dev

automatically close pylint window after python code window is close in VIM

From Dev

automatically close pylint window after python code window is close in VIM

From Dev

PyCharm Cannot Run Program C:\\Anaconda\\python.exe

From Dev

Message box closes automatically after a brief delay

From Dev

SaveFileDialog closes automatically directly after calling showDialog()

From Dev

Python Program Breaks After 1 Run

From Dev

How to run Specific function automatically or infinite times in Python Tkinter?

From Dev

Bootstrap window closes after alert statement

From Dev

Firebase popup window closes after calling login

From Dev

Main Window closes after execvp() call

From Dev

Firebase popup window closes after calling login

From Dev

Program closes right after the last input

From Dev

PyCharm run configurations not on main window

Related Related

  1. 1

    Tkinter window closes automatically after Python program has run in PyCharm

  2. 2

    After the program is run, the window generated by the program is blank and has no components

  3. 3

    go to previous window automatically after printing dialog closes

  4. 4

    Console window closes immediately when I run the program

  5. 5

    Window is closing immediately after I run program in PyQt 4 (anaconda) [PyCharm 4.5]

  6. 6

    exit python program without close Tkinter window

  7. 7

    how to a get text from python Tkinter entry after window has closed?

  8. 8

    How to run a python program(containing tkinter) on startup

  9. 9

    PyQt window closes after launch

  10. 10

    PyQt window closes after opening

  11. 11

    How to run a Python program without a window?

  12. 12

    Rebooting program after input() automatically on python

  13. 13

    Clear PyCharm Run Window

  14. 14

    C Win32: Window Automatically closes

  15. 15

    Python Tkinter Crashing, every run but program continues? Final polishing of Tkinter

  16. 16

    Inno Setup: How to automatically run program after /verysilent install?

  17. 17

    automatically close pylint window after python code window is close in VIM

  18. 18

    automatically close pylint window after python code window is close in VIM

  19. 19

    PyCharm Cannot Run Program C:\\Anaconda\\python.exe

  20. 20

    Message box closes automatically after a brief delay

  21. 21

    SaveFileDialog closes automatically directly after calling showDialog()

  22. 22

    Python Program Breaks After 1 Run

  23. 23

    How to run Specific function automatically or infinite times in Python Tkinter?

  24. 24

    Bootstrap window closes after alert statement

  25. 25

    Firebase popup window closes after calling login

  26. 26

    Main Window closes after execvp() call

  27. 27

    Firebase popup window closes after calling login

  28. 28

    Program closes right after the last input

  29. 29

    PyCharm run configurations not on main window

HotTag

Archive