Create a desktop icon for a Tkinter script - python

I have written a python script with a Tkinter GUI. I would like to create a desktop icon that will execute this script so that the end-user (not myself) will be able to double-click the icon and have the GUI load, rather than 'run' the script from the terminal or python shell and then have to F5 from there.
Is there a way to do this? I have googled many arrangements of my question but most answers seem to be normal python scripts, not ones which are Tkinter based.
I am using a Raspberry Pi with Wheezy and Python 2.7
Thanks in advance.

I create executables(icons that I click to start the programs I write) using 'py2exe'. I use windows 7 and I am not sure if it would work for you with Raspberry Pi, but a google search may clear that up for you. I will provide the link below. Basically you end up with a folder with the executable(icon) and also some files, without which the executable won't work. It's unfortunate that you get this extra 'baggage', but it's still handy and the best solution I have come across. I don't think there is a much better way, but I am not 100% on that. Interestingly, I found that you could delete most of these baggage files and the executable would still work, it was trial and error, but it didn't take long. If I want to send the folder to someone, I zip it first.
py2exe is here
If you need a 64 bit binary you can get it here, along with, actually, pretty much every other version. get py2exe here also

Besides creating executable file other option is create simple .bat file:
Open notepad
Enter "C:\ProgramData\Anaconda3\python.exe" "C:\Users\Your ID\script.py"
First part is path to python.exe, second to your python script
save file as .bat file, ex. "open_program.bat"
Now simply double click on saved .bat file icon should open your script.

I like using cx-freeze to convert python scripts to exe.
There is very easy to follow documentation to do this. In short, you create a setup.py script that specifies what libraries and packages you want to include or exclude in your application.
You can then run the build in the console and it will create a build folder, in which will be an Application File. You can create a Desktop short cut using send to and selecting desktop.
Documentation link

You can save the script as a .pyw file so the user can click on the file and the GUi would open

Related

How to change default program/ file associations to open files using python script?

I need to make a way to install my code, and set default programs depending on file type.
I've been trying to make my python program easy for people who know nothing about python or programming to use.
As an alternative to an exe I want to make a script to do everything, including putting a shortcut to the python code on the desktop and a start menu folder.
The thing is I need to make sure that .py files automatically hey opened by python, and not the software, psychopy, which the user will install.
Is there a way to use python to change the default program for a given file type?
I've already tried:
Py2exe: I get errors (see post history)
Pyinstaller: I get different errors.
The errors with py2exe seem almost irresolvable.
I've used pyinstaller before and it worked. It won't easily work with this code because it doesn't work with an updated library/package I need. I have library b which depends on library a being up to date. So I can't revert back to a setup that I know how to make work.

create an application in excel using python for a user without python

I am doing an application in Excel and I'd like to use python language. I've seen a pretty cool library called xlwings, but to run it a user need to have python installed.
Is there any possibility to prepare this kind of application that will be launch from a PC without Python?
Any suggestion are welcome!
A small workaround could be to package your application with cx_freeze or pyinstaller. Then it can run on a machine without installing python. The downside is of course that the program tend to be a bit bulky in size.
no. you must need install a python and for interpreting the python function etc.
It is possible using xlloop. This is a customized client-server approach, where the client is an excel .xll which must be installed on client's machine.
The server can be written in many languages, including python, and of course it must be launched on a server that has python installed. Currently the .xll is available only for 32 bits.
This might not be exactly what you're looking for (i.e.- "without python"), but you could ship it as a virtualenv .zip folder with all the necessary packages and respective python installation included in the folder... and also include a run.command bash executable inside the folder that the user can click on to run your program. You'll have to tell the user (inside a README.txt or something) how to enable the executable... For a particular application mine looked like...
Please complete the following steps after installation of MyApplication.zip:
Step 1: Move the MyApplication folder to your Desktop.
Step 2: Open your Terminal in Go then Applications then Utilities.
Step 3: In the Terminal, paste cd ~/Desktop/MyApplication
then press Enter.
Step 4: Then, paste chmod +x run.command
then press Enter.
From now on, clicking the “run” file in MyApplication will run the application.
If clicking the “run” file still doesn’t work, then do…
Click Ok on the popup then Open System Preferences then Security & Privacy
then Click on Open Anyway button then Click Open button on the popup.
NOTE: Please do not move or delete the MyApplication folder, or any of its files.
Then my run.command executable looked like the following:
cd ~/Desktop/MyApplication
PATH=$PATH:$HOME/Desktop/MyApplication/lib/python2.7/site-packages:$HOME/Desktop/MyApplication/lib/python2.7/site-packages/aeosa:$HOME/Desktop/MyApplication/lib/python2.7/lib-dynload:$HOME/Desktop/MyApplication/lib/python2.7/lib-old:$HOME/Desktop/MyApplication/lib/python2.7/lib-tk:$HOME/Desktop/MyApplication/Extras/lib/python:$HOME/Desktop/MyApplication/lib/python2.7/plat-mac/lib-scriptpackages:$HOME/Desktop/MyApplication/lib/python2.7/plat-mac:$HOME/Desktop/MyApplication/lib/python2.7/plat-darwin:$HOME/Desktop/MyApplication/lib/python2.7:$HOME/Desktop/MyApplication/lib/python27.zip
source bin/activate
python main.py
Three things are important here:
1) In run.command, I first change directory into the virtualenv I created for this project. I'm not trying to be dynamic here, since this is all just a quick hack of a solution, so as part of the README.txt, I simply tell the user to put the folder they downloaded onto their desktop. Otherwise, the cd command wouldn't work.
2) I set the client's PATH variable, separating entries with colons. You can see a list of the paths you'd need using a quick check on your computer...
import sys
for path in sys.path:
print path
3) I then call source bin/activate to activate the virtualenv.
4) I then execute my main.py file, which is in the top level of my virtualenv, along with run.command.
5) I only wrote this bash file for Mac OS X, so I don't know how it would translate to windows PCs. I think the only thing that'd change is that the path literals would need to use backslashes instead of forward slashes, but again, I haven't done this for windows so I'm uncertain.
Read more about setting up a virtualenv here: https://virtualenv.readthedocs.org/en/latest/index.html
Finally, note that you will not be able to send your final, working .zip folder over most email services, since they tend to prevent people from sending executables back and forth, so you'll have to use a service like Dropbox or something to send it to people.

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.

Distributing python application tutorials

Hi as a python newbie I have written a small python application that can convert an excel worksheet into a sqlite database table. Its not a terribly complex application but it does make use of external modules/package like xlrd (http://pypi.python.org/pypi/xlrd) which I had to download and install when writing my app.
Now that is all done I would like to distribute it amongst my friends, all windows users, while they have python on their machines, they may or may not have the xlrd modules.
I would like to package my app, make sure it includes everything that it needs to run, and share the final .zip file with my friends so they can use the application. Is there a good tutorial that covers how to package a python application, with all the necessary external modules/packages, so that another windows user, can easily run my application.
I keep hearing about disutils, can anyone point me to some good tutorials, or any other python packaging tutorials that show how to get everything into a simple easy to distribute file.
Many thanks
Note: I also want to include the sqlite database file with my application, so the end user doesn't have to worry about anything
First download Pyinstaller and save it somewhere. Then, if you're running Python 2.6 or 2.7 go and install pywin32.
Go create a directory for your output file.
Open Command prompt and enter the fallowing:
python path/to/pyinstaller/pyinstall.py file/to/be/converted.py --onefile
If you want to add an icon, add the --icon argument.
If you want it to hide the CMD window add the --windowed argument.
So if you wanted it to use an icon and hide the CMD window it would look like this:
python path/to/pyinstaller/pyinstall.py file/to/be/converted.py --onefile --icon="path/to/icon.ico" --windowed
The --onefile argument makes the saves everything into one file, without it, the output would be would be a lot of files.
The output is saved in the "dist" folder in the directory it was using.
After you convert the python script in to an .exe, put it in a .zip file along with any other files you need (in your case the squite database file) and you can give it to anyone without them needing to have Python at all.
If you want a program to do this all for you, you can download a program called PTEC.

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

Categories