Just starting Python and I have a question about how to run it just by double-clicking. With simple things like print "Hello World" the wait for user input clause can be added and it will run by double clicking and give the desired output, but when I try it with something that uses a window for example it will not. All I get is some compiled python file that does nothing. I don't really want to make an executable if possible since I'd think there'd be a way to do this without one.

Any help would be greatly appreciated since I want to begin on a little project and for it to make any sense I'd need it to start up by itself. I've been searching the forums but there are pages upon pages of people trying to make it into an executable or irrelevant information and I'm not quite sure what the proper keywords for this is. Sorry for the trivial question.

Dani AI

Generated

Nice catch, — naming your script Tkinter.pyw was the whole problem. When Python imports, it searches the script’s folder first; any file that matches an import name there wins over the standard library. That meant from Tkinter import * was trying to import your own file (or its cached .pyc) instead of the real Tk toolkit, so the app errored and vanished. Renaming the file avoids shadowing. See the search-path rules in The Python Tutorial: Modules. The .pyc you saw is just cached bytecode for faster loads; it is not something you double-click to run. That behavior is described in py_compile.

For double-clickable GUI apps on Windows, ’s .pyw tip is right: use the .pyw extension so Windows launches the windowed interpreter (pythonw.exe) and no console appears. If file associations are set up properly, double-clicking a .pyw should start your Tk app directly. Details are in Using Python on Windows.

If a window still flashes and disappears, run it once from a terminal to see the traceback, or add a tiny logger so errors don’t vanish:

import traceback

if __name__ == '__main__':
    try:
        # start your GUI here
        pass
    except Exception:
        with open('error.log', 'w') as f:
            traceback.print_exc(file=f)

Finally, for readers on modern Python 3: the module name is lower-case tkinter, not Tkinter. You can verify Tk is installed with python -m tkinter, which should pop up a demo window. See the tkinter docs. And ’s note holds: IDEs keep the window around, but for end users, .pyw plus a non-conflicting filename is the simplest path.

Recommended Answers

All 8 Replies

Actually, it probably does something and then closes the window immediately -- which means you can't see it!

Try this:

print "Hello, World"
raw_input("Press <Enter> to exit.")

If you double-click this, you should get output in a command prompt window.

Jeff

Some editors like DrPython, UliPad, PyScripter, IDLE have internal output window that will stay open without the use of console wait line like raw_input("Press <Enter> to exit.")

The raw_input part is what I meant by "wait for user input clause". Sorry for not being more clear, but this does not seem to work with some programs. Here's an example of a program I'm trying to do this with (got it as example code from one of the forums):

# import all the Tkinter methods
from Tkinter import * 
 
# create a window frame 
frame1 = Tk() 
# create a label 
label1 = Label(frame1, text="Hello, world!") 
# pack the label into the window frame
label1.pack() 
 
frame1.mainloop()  # run the event-loop/program

What would I have to do to code like this to make it double-clickable?

Two things:

(1) I can double-click on your program and get a nice little Hello, World window, along with the command-prompt window. Do you get something else?

(2) If you manually rename the file from "" to "myprog.pyw", the command-prompt window will not come up.

Jeff

Hmm maybe my install is bad then. Even by renaming it .pyw as I did before, all I get is a .pyc (Compiled Python File). Clicking on this does nothing but show a command prompt for an instant and it disappears. Are you also using Windows XP jrcagle?

EDIT: Oh and to note I get the .pyc file from both .py and .pyw

EDIT: And another thing to add... Code using wxPython works fine, but the examples with Tkinter do not. Didn't plan on using Tkinter anyway, but after clicking it a ton of times it looks like the command prompt says something isn't defined. Unless anyone has a reason for this I'll probably mark this as solved in a couple hours.

OK, wait. In your source code directory, you have

myprog.pyw
myprog.pyc

Yes?

What happens if you double-click "myprog.pyw"?

Literally nothing so far as a I can tell.

Oh wow... I suppose the reason for it not working was simply because I named it Tkinter. When I renamed it TkinterHW it worked. Sorry for the trouble, and thanks for the help all (especially jrcagle).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.