NimishP
NimishP

Reputation: 53

How can I make .py files run using Python Shell by default?

I made a Python GUI using Tkinter, but when I run it directly (double click the file) it opens the black python window but automatically closes by itself in less than half a second. I found a way to make it open the IDLE editor but it just opens the editor and doesn't run it.

I want it to run the way it runs when you open the IDLE editor and press Run Module. This runs it using Python Shell.

Is there a way I can make it automatically run using Python Shell?

Upvotes: 2

Views: 1326

Answers (2)

NimishP
NimishP

Reputation: 53

Okay, one of the comments on the original question is correct.

As Terry Jan Reedy (user:722804) said,

It is possible that your mygui.py file is missing 'root.mainloop()' or the equivalent to start the GUI. IDLE lets you omit that during development so that one can interact with tkinter to retrieve values and make changes to widgets.

Adding gui.mainloop() to the end of my program worked.

Upvotes: 1

ti7
ti7

Reputation: 18796

Based on Mark Tolonen's comment you should do two things

  • rename your file to a .pyw from .py to prefer console-less runs

  • set your system to open .pyw files with pythonw if that's not configured already

    • Linux: configure xdg-open
    • Windows: right click and choose an application from the context menu (you may need to find where Python is installed to select the pythonw.exe executable)

Upvotes: 1

Related Questions