I would like to have an executable (Windows, MacOS, Linux) of my program that will have its own terminal window where users will be able to type in. I don't want to have the user install modules or anything except this executable. You could say that the UI of my program would be a terminal window. Is it possible and with what?
I basically want the GUI of my app to be a terminal window.
To create a command-line interface for your program you'll likely want to use something like argparse, which is in the standard Python library. There are many other feature-rich command-line interface modules (e.g., click) you could try to use instead. You can design command-line interactions for your users including input and output, menus, etc.
Once you've built your application, a good choice to make it portable is the PyInstaller module. If you set the --onefile option when invoking it, it will generate a single .exe file which can be easily shared with users. If you also set the --console option when invoking PyInstaller, then your Python .exe application will interface with stdin/stdout. On Windows, this would be a cmd window.
If a user invokes your .exe via the GUI (e.g., a double-click on its icon), a Windows CMD window will be opened. If you want that window to stay open, however, you will need to create your application with some kind of "infinite"/"captive" design that never finishes executing until the user explicitly quits.
If the user instead runs the .exe from a Windows CMD terminal that is already open, then that window will always stay open.
I suspect you're looking for curses, or UniCurses if you're stuck with Microsoft Windows.
Many terminal emulators will allow you to start them up with a command line option that specifies a command to run in that emulator instead of your default shell.
Related
I have made a GUI for my application. All scripts are in Python (2.7) and the GUI is created with Tkinter. I work on Linux, but I needed this app to be executable on Windows. So I've used py2exe, to create an executable. After a while it is working almost perfectly, but I have the following problem:
Somewhere in the application, I need to call external programs, namely ImageMagick and LaTeX. I use the commands convert, pdflatex, simply by importing os module and running os.system(build), where build = 'convert page.pdf page.gif'etc. When those commands are called from the *.exe application the console pops up (meaning a console window opens up for a split of a second and closes again). Is there a way, to prevent this behaviour?
It does not interrupt the application, but it is ugly and not a desired behaviour.
[Note] I chose not to add any samples, since there are lots of files and other content, which, I think, is not related to the problem. I could however try to post minimal (not)working example. But maybe it is not needed.
Thanks!
import subprocess
subprocess.Popen("application.exe", shell = True)
I have a python program using PySide. When run normally, it opens up a PySide GUI, but when run with some flags in the command line, it spits some things out in the console window.
I'd like to retain this dual functionality, but it seems with py2exe you have to choose whether to have a console window or not when compiling, with no option for choosing during program execution.
Is what I want to do possible with py2exe, or even with some other python "compiler?"
This is not a py2exe limitation, but a Windows limitation. On Windows, applications are compiled either as Console Applications or GUI Applications. The difference is that Console Applications always open a console window, whilst GUI Applications never do.
As far as I can tell, it's not possible to have an application with dual functionality. As a workaround, I suggest that you simply compile two executables: one for console use and one for GUI use.
I have a program called ftpgrab.py. At the command prompt to run it I type:
c:\path\to\python\dir\python.exe ftpgrab.py
Is there a way on Windows 8 to create an icon which I can double-click to run this?
You can either create a batch file that will launch the program, or use something like pyinstaller to transform your script into an executable that can be run directly.
create a file named foo.bat;
copy your command to that file and save it;
double click foo.bat...
Assuming that you used one of the standard installers for python on windows, .py is already registered and it should just work. Copy it to your desktop and double-click. A console running the program should appear and run as normal. Its still a console app - the the customer wants a gui app, that's a different story.
btw, you shouldn't even have to type c:\path\to\python\dir\python.exe ftpgrab.py, just a plain ftpgrab.py or ftpgrab should do.
I use the Windows version of Python. I have a Python script using Pyside (nothing complicated, a kind of "hello world").
When I click on my script file or if I launch it from a command line, it executes perfectly and I have a GUI appearing.
However, I would like to avoid having a GUI if the script is launched from a textual terminal (cmd.exe, cygwin, ...). A kind of script which automatically knows if it should have a GUI output or a textual output.
Is there an easy and simple way to do that? I want to be able to do that with the Windows version of Python (not the one coming with Cygwin packages).
An obvious way would be to add a kind of "--no-gui" parameter when I launch the script from a textual terminal, but I wonder if Python (or some Python libraries) already provide tools for that.
Moreover I have an SSH server (Cygwin-base) on this computer, I can execute the script at distance but no GUI appear (of course) and I have no error message. It is a case where it is very interesting to know if the script failed because of the lack of Windows graphical support or if the script should adapt its output for a textual terminal.
I know that you can run file as .py file or as .pyw file. The second option is used for GUI applications and it does not open the console window. To distinguish these to two cases you can check isatty method of sys.stdout.
import sys
if sys.stdout.isatty():
# .py file is running, with console window
pass
else:
# .pyw file is running, no console
pass
EDIT:
I tried to run that with putty+ssh on linux box - it returns True.
I tried to use msys bash shell on windows box - it returns True (.py file)
I tried to use cygwin bash shell with cygwin python - it returns True (.py file)
Unfortunately, I have no possibility to try putty + windows cygwin ssh server.
I have a Python program (GUI application). I can run this program from the command prompt on Windows (command line on Linux). But it can be too complicated for users. Is there an easy way to initiate a start of the program with a click (double click) on a pictogram (a small image on the desktop)?
Linux:
I am not sure, which linux distro and desktop you use but for gnome I create such files on desktop e.. create a myapp.desktop and put in on desktop
[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=MyApp
Type=Application
Exec=python /home/anushri/display.anurag/xxx.py
TryExec=
Icon=/usr/share/pixmaps/gnome-qeye.png
X-GNOME-DocPath=
Terminal=false
Name[en_IN]=MyApp
GenericName[en_IN]=MyApp
Comment[en_IN]=MyApp
GenericName=MyApp
Comment=MyApp
Windows:
Right-click an open area on the desktop, point to New, and then click Shortcut, type the command line to start you program, Type a name for the shortcut
I'm not sure if I understood the question well, but if you just need a way to simulate a command line input with a simply clickable icon, just create a simple .bat file (assuming windows) on the desktop, as a new text file containing something like
C:\[Pythonpath]\python C:\[MyPythonAppPath]\myapp.py
See http://en.wikipedia.org/wiki/Batch_file for more info.
Use py2exe to make an exe and just to make it more 'user friendly' use Inno set up (www.jrsoftware.org/isinfo.php ) along with IStools to build up an installer which would integrate the GUI with sound, widgets, other elements etc and users who do not have python etc installed in their systems can also play your GUI perfectly fine !
By the way what GUI are you using ? pygame, tk, wx, PyQt ...etc ?
What I've done in the past is use py2exe to create an executable from my python script. That embeds the interpreter and the source inside an EXE, that way it works just like a native executable and you don't have to have the users install python or anything complicated.