954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Running a program by double-click (windows)

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.

Kabooom
Newbie Poster
14 posts since May 2008
Reputation Points: 10
Solved Threads: 3
 

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

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

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 to exit.")

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

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?

Kabooom
Newbie Poster
14 posts since May 2008
Reputation Points: 10
Solved Threads: 3
 

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

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

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.

Kabooom
Newbie Poster
14 posts since May 2008
Reputation Points: 10
Solved Threads: 3
 

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

myprog.pyw
myprog.pyc

Yes?

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

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

Literally nothing so far as a I can tell.

Kabooom
Newbie Poster
14 posts since May 2008
Reputation Points: 10
Solved Threads: 3
 

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).

Kabooom
Newbie Poster
14 posts since May 2008
Reputation Points: 10
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You