Put Python/Tkinter into an application icon - python

I wrote a program in Python, then created a GUI using Tkinter. When I use programs on my computer (like Microsoft Word), I don't need to access the GUI from the command line I just click the application icon.
How do I put my program (the program itself is in the same .py file as the GUI) into an application icon that will start my program?

What you are referring to in Windows is a shortcut link.
The equivalent in *nix environments would be the ln -s command.
Links in computer systems are special files that point to a different file, often with parameters for easy access. See the Wikipedia Article on them for more information. Windows shortcuts are files with the extension .lnk, whereas soft links in Linux have the mode +l.
If I wanted to link to a file on Windows using Python, I would right click and create a shortcut. If your python installation is old (below 2.7.3), there is a little bug in the installation where the regkeys HKEY_CLASSES_ROOT\py_auto_file and HKEY_CLASSES_ROOT\.py are misconfigured and don't pass on command line arguments, so a fail-safe when passing arguments would be to edit the shortcut link and stick in something like the following:
"C:\Python27\python.exe" "C:\dev\path\to\file.py --args"
Windows can execute a program from a shortcut link or link files and directories directly (called symbolic links), whereas linux would link files or directories and then call that interpreter with a shebang (#!) or a direct command (./) in order to execute it. Windows just pretty much links the program to execute with the arguments after it.
You can also just double-click the file in explorer, but this doesn't supply arguments (if you need them).

Related

Run a Python program at start-up on Windows

I am developing a key-logger on Python (only for curiosity sake). And the script will be an executable. The process will not need a UI or user interaction.
Is there any way, even in another executable to make the key-logger start at start-up?
I don't use Windows, but you can try making a batch script that runs your python file and make that script Run a program automatically when Windows starts:
Click the Start button Picture of the Start button , click All
Programs, right-click the Startup folder, and then click Open.
Open the location that contains the item you want to create a shortcut
to.
Right-click the item, and then click Create Shortcut. The new
shortcut appears in the same location as the original item.
Drag the shortcut into the Startup folder.
As I said, I don't use Windows, so it might be totally wrong.
You can refer here for making the BAT file, which basically says:
#echo off
python c:\somescript.py %*
pause
I think that the above answers are too complex. What I did was just drag and drop or copy and paste my file in the startup folder by clicking the quick access toolbar, typing "startup", and the job is done.
I am using Windows 10 operating system, so it might be different in your case.
I hope this is useful.
Edit: The key to this solution is to have the .py extension files open by default by the python console (not a text editor), otherwise it will just open the file instead of executing it. In order to select the default program a type of file is opened with, right click on the .py file -> Open with -> Choose default program. See this example:
Use VBScript:
1-> create anyname.vbs with this data:
Set wvbs=CreateObject("Wscript.Shell")
wvbs.run "full location of your File",0
2-> copy anyname.vbs file in C:\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup this folder
Now when windows start it will run your file in hidden mode
Open run with "Start + R", then open "shell:startup". it opens you a folder(the folder that was mentioned before at start menu), and every file that is on this folder, well run at startup.
The folder path is: "C:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" (you can copy it on windows explorer, or copy this path and put your account name on USERNAME)
this is the trick i used in my script:
from os import getcwd
from shutil import copy
copy(getcwd()+'/FILE_NAME.exe','C:/Users/USERNAME/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup')
They are some ways for file name as well, but im not familiar with it. this code copies it self to startup folder and starts each time windows boots
Create a shortcut in the shell:startup folder with an absolute path to your pythonw.exe executable. The w version of Python is needed so that it starts up without a shell in the background.
Detailed instructions
Open the startup folder
Type the Windows and R keys at the same time. In the Run dialog, type shell:startup and it enter. This takes you to the Startup folder. Shorcuts in this folder are launched when the computer starts. (For me, the startup folder is at C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.)
Create new shortcut
Right-click in this folder and say "New shortcut." Browse to "This PC," "Windows (C:)," "Program Files,", "Python39", and select pythonw.exe. Your Python install is likely in a different location. More recently, Python is found somewhere within %appdata%. (You can paste %appdata% into the run dialog to open this folder and look for Python. It opens in Roaming by default, but be sure to look in Local, too.)
As mentioned at the start of this answer, it is important that you select pythonw.exe rather than python.exe as pythonw.exe will run without opening a command prompt.
On the next "Create Shortcut" screen, titled, "What would you like to name the shortcut," you can name it whatever you want. For the OP, I recommend "Self penetration test."
Click "Finish."
Edit the shortcut to launch your script
In the Startup folder, right click on the app you just created and select "Properties."
In the Properties dialog's Shortcut tab, edit the "Target" to look something like "C:\Program Files\Python310\pythonw.exe" pentest_keylogger.py and the "Start in" to be the folder in which pentest_keylogger.py is found. (As above, you will need to use your Python distribution's path.)
Test your script
Hit OK. Then double-click on the icon to see if it works silently as you desire.
Finally, reboot your machine and use resmon (from the run dialog) or Ctl-Alt-Delete and the task manager to see if your app is running in the background.
Acknowledgements
Thanks to Eryk Sun and Behfar baghery for the core ideas presented here.

How to turn a UNIX executable into a desktop icon shortcut?

I have a python program that is executable and I can run it on my terminal by typing: ./python_file_name
This is fine for my use, but I want the program to be accessible to non-computer people. I would like to be able to create a desktop shortcut that runs the UNIX executable file. Currently when I double click the executable file it just opens the python program in a text editor. I want to be able to have the end-user double click the file so that it executes in the background.
Just for completeness, the script generates a time-sensitive-data-scraping csv file in the user's downloads folder. Therefore, there is no need to create an application with a GUI or anything fancy like that. Also, I am using Ubuntu and the end-user will be on Mac OS X.
I think you're looking for py2app. It will allow you to send your user a nice little application bundle.

Python open a JAR shortcut

I have a JAR file saved on my desktop, is there a way that I ca get python to pen this up, as if I was just clicking on the link from my desk top, I am very new to python and know this is a dumb question, but would help me out alot.
Why do you want to execute it via Python?
In my Windows environment such .jar file is executed by Java:
C:\jre7\bin\javaw.exe -jar "%1" %*
Of course your OS should be able to change such association to open .jar with other program. On my Windows I can use local menu to open .jar file with Java, Eclipse, Firefox or I can chose other program.
I'll assume you're using Windows (it seems that way from how you've presented the question).
To simulate double-clicking a file on Windows, use os.startfile (available only on Windows):
os.startfile("path/to/myjarfile.jar")
This will do whatever your system is configured to do with files with a .jar extension.
For a more complete answer on starting files on Windows see this answer.

How should I launch a Portable Python Tkinter application on Windows without ugliness?

I've written a simple GUI program in python using Tkinter. Let's call this program 'gui.py'. My users run 'gui.py' on Windows machines from a USB key using Portable Python; installing anything on the host machine is undesirable.
I'd like my users to run 'gui.py' by double-clicking an icon at the root of the USB key. My users don't care what python is, and they don't want to use a command prompt if they don't have to. I don't want them to have to care what drive letter the USB key is assigned. I'd like this to work on XP, Vista, and 7.
My first ugly solution was to create a shortcut in the root directory of the USB key, and set the "Target" property of the shortcut to something like "(root)\App\pythonw.exe (root)\App\gui.py", but I couldn't figure out how to do a relative path in a windows shortcut, and using an absolute path like "E:" seems fragile.
My next solution was to create a .bat script in the root directory of the USB key, something like this:
#echo off
set basepath=%~dp0
"%basepath%App\pythonw.exe" "%basepath%\App\gui.py"
This doesn't seem to care what drive letter the USB key is assigned, but it does leave a DOS window open while my program runs. Functional, but ugly.
Next I tried a .bat script like this:
#echo off
set basepath=%~dp0
start "" "%basepath%App\pythonw.exe" "%basepath%\App\gui.py"
(See here for an explanation of the funny quoting)
Now, the DOS window briefly flashes on screen before my GUI opens. Less ugly! Still ugly.
How do real men deal with this problem? What's the least ugly way to start a python Tkinter GUI on a Windows machine from a USB stick?
EDIT:
All the answers below were very good (py2exe, pyinstaller, small .exe, .wsf script.) The .wsf solution was the simplest, so I'm using it for now. I'll probably end up switching to one of the other three solutions if I want a prettier icon and the standard .exe extension. Thanks, everyone!
This Windows Scripting Host script (file extension .wsf) can be used instead of the batch file:
<job>
<script language="VBScript">
set WshShell = WScript.CreateObject("WScript.Shell")
CMDFile = "App\\pythonw.exe App\\gui.py"
WshShell.Run CMDFile, 1
</script>
</job>
Update: Alternatively compile this C program and link an icon resource:
#include <windows.h>
#include <process.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
return _spawnl(_P_NOWAIT, "App/pythonw.exe", " App/gui.py", lpCmdLine, NULL);
}
Update 2: To build an App.exe with icon, save the C code to app.c, create an Windows icon file app.ico, and save the following line to app.rc:
appicon ICON "app.ico"
Using Visual Studio 2008 Express, run these commands:
"C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"
rc.exe app.rc
cl.exe app.c /FeApp.exe /link app.res
Alternatively use "Visual Studio 2010 Express" or "Microsoft Windows SDK v7.0 for Windows 7 and .NET Framework 3.5 Service Pack 1" and adjust the commands accordingly.
Note that the icon will only be used for the App.exe starter program, not your Python program.
Use pyinstaller to zip up your distribution (the advantage over py2exe is that it knows different third-party libraries and is generally more up-to-date).
You can then create a .exe for your users to click upon to start your application. If you just copy the results of the pyinstaller build onto your USB drive you should be fine.
Make it to a single executable using py2exe.
You could do this in a hacky manner by writing you're own little C application that calls system('start "" "%basepath%App\pythonw.exe" "%basepath%\App\gui.py"'). Next you compile it without console and use it as a "shortcut".
The Short Answer:
This question was asked a few years ago, but I recently found a solution for a program I was working on that may still be useful for others. With this method, you will be able to create a standalone exe program launcher that can be placed anywhere and refer to a file in its same folder or subdirectory, while having a pretty icon of your choice and no DOS screen popping up. In other words, a true good-looking relative-path transportable shortcut file :)
The solution should be easy to follow and do even for non-programmers and goes as follows:
open notepad
write: %windir%\system32\cmd.exe /c start "" "%CD%\optional
subfolder\mainpy2exeGUI.exe"
save as "whatever.bat"
convert the bat-file to an exe file using a program called "BAT to EXE converter"
while checking the "invisible application" option, and selection the
icon file you want under the "versioninformations" tab. You can name
the output exe file to whatever you want. Link to the converter
program can be found at
http://www.freewaregenius.com/how-to-create-shortcuts-with-a-relative-path-for-use-on-usb-drives/
The converter program download contains a 32 and 64-bit version, use the 32-bit version to make the shortcut usable by both older and newer PCs.
(note, this solutions is almost the same as suggested at http://www.freewaregenius.com/how-to-create-shortcuts-with-a-relative-path-for-use-on-usb-drives/. However the current solution is different in terms of the code it uses in step2 which allows the launcher progam to be placed anywhere on a computer and not just on the top directory of a USB-stick, and is new to emphasize that the invisible option should be checked. Those differences are crucial.)
More Details (optional):
The original question was: "What's the least ugly way to start a python Tkinter GUI on a Windows machine from a USB stick?"
What was needed can be broken down to four things:
1. An exe program launcher.
2. That works on any computer and in any directory (i.e. it supports relative paths).
3. That has an icon.
4. That does not open an "ugly" DOS window.
There were several possible solutions suggested but none so far that satisfies all criteria. The original poster went for the ".wsf" option which allowed for relative paths and no ugly DOS window, but did not allow a custom icon or the recognizable exe file.
Part of the problem with the previously suggested solutions include:
you do not have C/VB programming skills or software.
you want an icon to your launcher program. Using a shortcut file that executes "cmd" and uses it to open your GUI file will allow you to set an icon file, BUT the icon file reference is absolute and will fail on any other computer than the one you created the shortcut file on.
you do not want the "ugly" DOS window flash. The cmd shortcut solution mentioned in the previous point creates a DOS window that flashes before opening your GUI.
Making the py2exe main executable file as the program launcher would almost be a perfect solution because it satisfies all criteria, but a backdraw with it is that the py2exe ececutable would require an ugly "tlc" folder to be placed in the same top-directory. It is therefore better to hide the main py2exe launcher in a nicely named subfolder. Also, there are many cases where one would like to keep the program launcher and the program itself as separate exe files, for instance if you are only using your main py2exe program to function as a python-runner that can launch open-ended editable python scripts that you can edit on the go without having to create a new py2exe file for each time you make a change to one of your scripts.
You can also fork Portable Python sources on GitHub and create shortcut in the same way other Portable Python shortcuts are created.
This gives you nice way to start app, icon, you can set custom registry/env variables if you need to, etc etc.
As an example you can take e.g. IDLE shortcut from Portable Python sources.
I've made a batch script (PyRunEXE) which compiles a simple Assembly Language code to make an EXE launcher for you:
https://github.com/SzieberthAdam/pyrunexe

Default save path for Python IDLE?

Does anyone know where or how to set the default path/directory on saving python scripts prior to running?
On a Mac it wants to save them in the top level ~/Documents directory. I would like to specify a real location. Any ideas?
On OS X, if you launch IDLE.app (by double-clicking or using open(1), for example), the default directory is hardwired to ~/Documents. If you want to change the default permanently, you'll need to edit the file idlemain.py within the IDLE.app application bundle; depending on which Python(s) you have installed, it will likely be in one of:
/Applications/MacPython 2.x/IDLE.app/Contents/Resources
/Applications/MacPython 2.x/IDLE.app/Contents/Resources
/Applications/MacPorts/Python 2.x/IDLE.app/Contents/Resources
/Applications/Python 2.x/IDLE.app/Contents/Resources
/Applications/Python 3.x/IDLE.app/Contents/Resources
Edit the line:
os.chdir(os.path.expanduser('~/Documents'))
On the other hand, if you start IDLE from the command line, for example, with:
$ cd /some/directory
$ /usr/local/bin/idle
IDLE will use that current directory as the default.
I actually just discovered the easiest answer, if you use the shortcut link labeled "IDLE (Python GUI)". This is in Windows Vista, so I don't know if it'll work in other OS's.
1) Right-click "Properties".
2) Select "Shortcut" tab.
3) In "Start In", write file path (e.g. "C:\Users...").
Let me know if this works!
In Windows 10+, click the Windows Start button, then type idle, and then right-click on the IDLE desktop app and open the file location. This should bring you to the Start Menu shortcuts for Python, and you'll find a shortcut to IDLE there. Right-click on the IDLE shortcut and select properties. Set the "Start in" directory to be where you want default save path to be.
It seems like you can get idle into the directory you want if you run any module from that directory.
I had previously tried opening idlemain.py through the path browser. I was able to open and edit the file, but it seemed like I wasn't able to save my modifications.
I'm just glad to hear other people are having this problem. I just thought I was being stupid.
If you open a module, that sets the default working directory.
Start IDLE.
File -> Open to open your file. And set the current working directory.
In my case, the default directory is set to the directory from which I launched IDLE. For instance, if I launched IDLE from a directory called 'tmp' in my home directory, the default save path is set to ~/tmp. So start your IDLE like this:
~/tmp $ idle
[...]
On Windows (Vista at least, which is what I'm looking at here), shortcut icons on the desktop have a "Start in" field where you can set the directory used as the current working directory when the program starts. Changing that works for me. Anything like that on the Mac? (Starting in the desired directory from the command line works, too.)
For OS X:
Open a new finder window,then head over to applications.
Locate your Python application. (For my mac,it's Python 3.5)
Double click on it.
Right click on the IDLE icon,show package contents.
Then go into the contents folder,then resources.
Now,this is the important part:
(Note: You must be the administrator or have the administrator's password for the below to work)
Right click on the idlemain.py,Get Info.
Scroll all the way down. Make sure under the Sharing & Permissions tab,your "name"(Me) is on it with the privilege as Read & Write.
If not click on the lock symbol and unlock it.
Then add/edit yourself to have the Read & Write privilege.
Lastly,as per Ned Deily's instructions,edit the line:
os.chdir(os.path.expanduser('~/Documents'))
with your desired path and then save the changes.
Upon restarting the Python IDLE,you should find that your default Save as path to be the path you've indicated.
I am using windows 7 and by going to Start-> IDLE(Python 3.6 32-bit)
The click on properties and then in the shortcut tab go to
Start in and entering the desired path worked for me kindly note if IDLE is open and running while you do this you'll have to shut it down and restart it for this to work
If you locate the idlelib directory in your Python install, it will have a few files with the .def extension. config-main.def has instructions on where to put the custom config files. However, looking through these I did not find any configurable paths (your install may vary). Looks like you might need to crack open the editor code to alter it.
If you are using linux, you can create simple .sh file as presented below::
#!/bin/sh
cd /fullPath/PythonScripts/
idle
make the file executable by right click-> properties-> permissions-> check the execute as program checkbox-> done
Run the file :)

Categories