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.

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 "myprog.py" 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.