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?
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)
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.
Related
I want to open a python file in cmder terminal quickly. Currently, the fastest way i know how is to navigate to the directory of the python file in cmder terminal and then run it by calling "python file.py". This is slow and cumbersome. Is there a way for me to have a file or exe, that, when i run it (or drag the program onto it), automatically makes the program run in cmder straight away.
Windows 10
Clarification: I'm using cmder terminal specifically because it supports text coloring. Windows terminal and powershell do not support this.
On windows you can go to the directory with the file in the explorer and then simply hold shift as you right click at the same time. This will open the menu and there you will have the option to use the command shell/powershell and then you don't have to navigate to the directory inside the shell anymore and can just execute the python file.
I hope that helps.
Answer: The escape codes just weren't properly configured for the windows terminals. You can get around this by using colorama's colorama.init(). It should work after that.
I created a little application with Python3 and gtk glade, I'm using the terminal to see if I'm doing something wrong. However I want to launch the application without the terminal in the back at the end of the developement. So my question is, is there a way to do that?
You can change the file extension from .py to .pyw to make the Python script run without the terminal window. More on this here.
I want to hide the console window of a python program, so I change the file extensions to "pyw", but when I open it, the python IDLE show up even though I choose open it with "pythonw.exe"
If I use "pythonw test.py" in cmd, it works.
So I want to know what's wrong with this and how to solve this, thank you.
Change the program that opens python files.
Assuming you're using Windows, right click any python file (in your case any .pyw file, not .py), properties, change Opens with to pythonw instead of IDLE
For me, I had multiple version of Python installed that was causing issues. Once I had only had one version, I applied that pythonw.exe was the default for .pyw files and it worked.
I'd like to write cross platform Python scripts that are GUI frontends for command line programs. The problem is I know a few Mac users who think that using the terminal will have the same effect as throwing their computer off the top of a skyscraper. In Linux and Windows it's easy enough to setup a Python script so the user can double click an icon and the script will start without opening any extra windows. Is there an easy way to do this with OS-X? Would the user have to install a different Python than the one that comes with OS-X? I haven't been able to find a definitive answer.
You might want to look at Platypus. It's a freeware app for generating apps which wrap scripts.
Another way to do something like that is using Automator or even AppleScript Editor. Either can produce an application which just runs a script.
Update:
For Automator: Launch Automator, select the Application template, type "script" in the search field, double-click Run Shell Script, switch the shell pop-up menu to /usr/bin/python, type/paste your Python script into the text field. Or, leave the pop-menu on /bin/bash and just write an invocation of an external script in the text field. Save as an application.
You can also view help from its Help menu.
For AppleScript, launch AppleScript Editor, type the following as the script:
do shell script "/usr/bin/true"
Replace /usr/bin/true with the path to whatever script you like. Save as an application.
Again, there's help in the Help menu.
py2app does this with aplomb. You make your Python script, use whatever dependencies you need (wx, Tkinter, etc.) and py2app makes you a standalone app bundle that will run in any modern OS X environment. It bundles Python too, so you can use any Python you want (not just the system default).
The downside is that the generated apps might be large, up to 50MB if you have a lot of dependencies (though that is somewhat of an extreme).
There are two ways to do this:
Click on a script.
Press command-i to open the "get info" window.
Expand the "Open With" section (if it isn't already).
Choose "Python Launcher" from the drop-down menu.
Click "Change All" if you would like ALL Python scripts to launch when double clicked.
Possibly open Python Launcher and uncheck "Run in a Terminal window"
This will work for this machine only, so it is less portable than the following. Why? Because the default for opening a document type varies depending on what is installed (XCode and/or IDLE will both take over opening a .py file).
Method Two:
Validate the Interpreter Directive, that's the first line of the file. I suggest using /usr/bin/env python3. This will run the first python3 interpreter that is on the users path.
Make the script executable chmod a+x <script_name> from the Terminal.
Change the extension from .py to .command (this will be opened by the Terminal).
Use zip or tar for distribution so that the permissions do not get mangled.
This method will open a Terminal window, but when the Python window is closed the terminal window will also close.
If your script has dependencies outside of the standard library, then you should provide a second .command file to install those. This may make things more complicated, but using pip3 install --user <list of dependencies> should minimize complications.
I am running python 2.7.1. I can't figure out how to launch the IDLE IDE. I am told it comes already installed with python, but I can't find it using spotlight.
In the stock Mac OS X python installation, idle is found in /usr/bin, which is not (easily) accessible from Finder and not indexed by Spotlight. The quickest option is to open the Terminal utility and type 'idle' at the prompt. For a more Mac-like way of opening it, you'll have to create a small app or shortcut to launch /usr/bin/idle for you (an exercise left to the reader).
When you open up a new terminal window, just type in
idle
Then you will see a little rocket icon show up as IDLE loads
Then the Python shell opens up for you to edit
I think the shell command is
python -m idlelib.idle
but i am not a mac user so i can't test.
One way to run IDLE from spotlight or an icon in the Applications folder is to build a quick Automation for it. As mentioned by other commentators, this probably isn't necessary for Python 3, as it creates a shortcut automatically, and some hand-installed versions have tools to do this automatically. But if you want to roll your own:
You'll need to know the terminal command to open your version of IDLE. On my Mac right now (early 2016), running python 2.7.10, it is "idle2.7"
Using spotlight, or in the Utilities folder, open "Automator"
Choose an "Application" type document.
Make sure "Actions" is selected in the gray bar, upper left.
In the actions column, find "Run Shell Script" and double-click it, or drag it to the workflow area on the right.
Enter the terminal command in the parameters box that appears.
Save your automation (I called mine "IDLE" and put it in the Applications folder, to make it easy).
It's now available (as soon as spotlight indexes it) via all the normal methods. The only side-effect will be that while it's running, your menu bar will have a spinning gear over in the tray area next to the clock. This indicates an automation workflow is running. Once you close IDLE, it will go away.
first to launch the terminal CMD+space
second to input idle3
the idle will be activated automatically.
After you launch idle from the command line (make sure idle shell window has focus), click File, click "New File". A new window opens; this is your editor.
Type your program in the editor. Click "File", click "Save As...". Save your file somewhere with any name you choose, and a ".py" extension to the file name.
Click "Run", click "Run Module" (or, F5). Assuming no errors, the results will appear in the Shell window. Edit your file & repeat as necessary.
The answer of Matthewm1970 works like a charm!
And if you add an & to your shell command, the automation script will end immediately. There is no spinning gear. Like so:
/usr/local/bin/idle3.5&
Note the ampersand.
Cheers.
-melle
open Terminal
type "idle" and press enter
right click on "Python" icon > Options > Keep in Dock
so for python 3.4.3 in applications a folder named "python 3.4" click that and click IDLE.
for python 2.7.9 go here https://www.python.org/downloads/ and get 2.7.9 and a folder named "python 2.7" click that and click IDLE.
As to the earlier questions about starting IDLE: you can certainly start it from the command line. Also, if you installed Python using Homebrew, you can run 'brew linkapps' (from the command line); that will place an app for IDLE (among other things) in Launchpad (Applications folder).