How to run Python Tkinter Windows app without ugliness?

I wrote a simple graphical program in python using Tkinter. Call this program "gui.py". My users run "gui.py" on Windows computers using a USB key using Portable Python ; installing anything on the host machine is undesirable.

I want my users to run "gui.py" by double-clicking the icon in the root of the USB key. My users do not care about what python is and they do not want to use the command line if they do not need it. I do not want them to care about which drive letter the USB key is assigned to. I would 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 donโ€™t could figure out how to make a relative path in a Windows shortcut, and using an absolute path such as "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 does not seem to care about which drive letter the USB key is assigned to, but it leaves the DOS window open during my program. Functional but ugly.

Next, I tried the .bat script as follows:

 @echo off set basepath=%~dp0 start "" "%basepath%App\pythonw.exe" "%basepath%\App\gui.py" 

(see here for an explanation of ridiculous quoting)

Now the DOS window flashes briefly on the screen before my graphical interface opens. Less ugly! Still ugly.

How do real men deal with this problem? What is the least ugly way to run the Python Tkinter GUI on a Windows machine from a USB drive?

EDIT: All of the answers below were very good (py2exe, pyinstaller, small.exe, .wsf script.) .Wsf solution was the easiest, so I am using it now. I will probably return to one of three other solutions if I want a more beautiful icon and a standard .exe extension. Thank you all!

+4
source share
7 answers

This Windows Scripting Host script host (.wsf file extension) can be used instead of a 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 program in C and bind the 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 create App.exe with the icon, save the C code in app.c, create the Windows icon file app.ico and save the following line in app.rc:

 appicon ICON "app.ico" 

Using Visual Studio 2008 Express, run the following 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 configure the commands accordingly.

Note that the icon will only be used for the App.exe startup program, not for your Python program.

+4
source

Use pyinstaller to consolidate your distribution (the advantage over py2exe is that it knows different third-party libraries and, as a rule, is more relevant).

You can then create .exe for your users to click to start the application. If you just copy the pyinstaller assembly results to your USB drive, you should be fine.

+4
source

Make it one executable using py2exe .

+3
source

You can do this in a hacked way by writing that you have your own small C application that calls system('start "" "%basepath%App\pythonw.exe" "%basepath%\App\gui.py"') . Then you compile it without a console and use it as a shortcut.

+2
source

Short answer:

This question was asked a few years ago, but recently I found a solution for a program I was working on that might be useful to others. Using this method, you can create a standalone exe launcher that can be located anywhere and access the file in the same folder or subdirectory, while having a nice icon of your choice and not go to the DOS screen. In other words, a real cute portable relative path label :)

The solution should be easy to follow and make even for non-programmers and looks like this:

  • open notebook
  • 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" when checking the "invisible application" option and selecting the icon file that you want on the "versioninformations" tab. You can name the output exe file for everything you want. A link to the program converter can be found at http://www.freewaregenius.com/how-to-create-shortcuts-with-a-relative-path-for-use-on-usb-drives/ Converter program download contains 32- bit and 64-bit using the 32-bit version to make the shortcut usable by both older and newer PCs.

(note that these solutions are 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 from the code, it uses in step 2, which allows you to run the launcher anywhere on the computer, and not just in the top folder of the USB drive, and is new to emphasize that you need to check the invisible parameter. crucial.)

Details (optional):

Initial question: "What is the least ugly way to run the Python Tkinter GUI on a Windows PC from a USB drive?"

What you need can be divided into four things: 1. The exe launcher. 2. It works on any computer and in any directory (that is, it supports relative paths). 3. It has an icon. 4. This does not open the ugly DOS window.

Several possible solutions were proposed, but so far they have not met all the criteria. The original poster was sent for the ".wsf" option, which allowed relative paths and not an ugly DOS window, but did not allow a custom icon or a recognizable exe file.

Part of the problem with previously proposed solutions includes:

  • You do not have programming skills or C / VB software.
  • you need an icon in your launcher. Using a shortcut file that runs "cmd" and uses it to open your GUI file will allow you to install the icon file, BUT the link to the icon file is absolute and will not work on any other computer except the one on which the shortcut file was created.
  • You do not want the ugly DOS window to start. The cmd shortcut mentioned at the previous point creates a DOS window that flashes before opening your GUI.
  • Creating the main py2exe executable file as a program launch is almost an ideal solution because it meets all the criteria, but the backdraw with it is that py2exe ececutable will require the ugly "tlc" folder to be placed in the same top -directory. Therefore, it is better to hide the main py2exe launcher in a beautifully named subfolder. In addition, there are many cases where you would like the launch program and the program itself to be separate exe files, for example, if you use only your main py2exe program to work as a runner python that can run open editable python scripts that you you can edit on the go without creating a new py2exe file for every change to one of your scripts.
+2
source

You can also use fork Portable Python sources on GitHub and create a shortcut in the same way as other Portable Python shortcuts.

This gives you a good way to run the application, an icon, you can set your own registry variables / env if you need, etc. etc.

As an example, you can take, for example. IDLE from Portable Python .

+1
source

I made a batch script package (PyRunEXE) that compiles simple assembly language code to make EXE run for you:

https://github.com/SzieberthAdam/pyrunexe

0
source

Source: https://habr.com/ru/post/1335002/


All Articles