Freezer - A GUI for Python's cx_freeze

Post your working scripts, libraries and tools
Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

Freezer - A GUI for Python's cx_freeze

23 Apr 2018, 21:40

Hey guys,

So for any of you Python buffs out there, this little script can be a nice shortcut. For those of you who don't know, Python is an interpreted language like AHK, but sharing them on Windows can be a PITA. So they invented a program called "cx_freeze" that compiles scripts to an EXE. But it's a command-line app that can also be a PITA :lol:. I wanted a way to just click an icon, choose a file, and be done with it (kinda like ahk2exe :lol:). Yes, sometimes having tons of options is a good thing (especially if you're building something commercial), but most of the time it really isn't necessary. So here goes nuttin':

Code: Select all

; Bring up a "file open" dialog, saving the path to the output variable "file"
FileSelectFile, file,, %A_ScriptDir%, Freeze Python Script, Python Scripts (*.py)

; If no file was selected, we're done
if (file == "")
	ExitApp

; Replaces \ with \\, as the Windows command prompt requires that
file := StrReplace(file, "\", "\\", 0)

; Set up the directory where the compiled EXE should go
; I just wanted it to be a "build" folder in the same path as the Python script
parts := StrSplit(file, "\\")
path := ""
Loop % parts.MaxIndex()
{
	if (A_Index == parts.MaxIndex())
		path .= "build"
	else
		path .= parts[A_Index] . "\\"
}

; If an icon was placed in the same directory as the script, add it to the settings
; So first, we need the script's path (some more string-replacing stuff)
icon := ""
path2 := StrReplace(path, "\\", "\", 0)
path2 := StrReplace(path2, "\build", "", 0)

; Then if an icon is found, save its path
Loop, Files, %path2%/*.ico
{
	icon := path2 . "\" . A_LoopFileName
	icon := StrReplace(icon, "\", "\\", 0)
	break
}

; Then run cxfreeze and we're good
if (icon != "")
	RunWait, %comspec% /c python C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts\\cxfreeze "%file%" --target-dir "%path%" --icon "%icon%", , Hide
else
	RunWait, %comspec% /c python C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts\\cxfreeze "%file%" --target-dir "%path%", , Hide
TrayTip, Freezer, Python script "frozen" successfully; Bring up a "file open" dialog, saving the path to the output variable "file"
FileSelectFile, file,, %A_ScriptDir%, Freeze Python Script, Python Scripts (*.py)

; If a file was selected, "freeze" it as an EXE
if (file == "")
	ExitApp

; Replaces \ with \\, as the Windows command prompt requires that
file := StrReplace(file, "\", "\\", 0)

; Set up the directory where the compiled EXE should go
; I just wanted it to be a "build" folder in the same path as the Python script
parts := StrSplit(file, "\\")
path := ""
Loop % parts.MaxIndex()
{
	if (A_Index == parts.MaxIndex())
		path .= "build"
	else
		path .= parts[A_Index] . "\\"
}

; If an icon was placed in the same directory as the script, add it to the settings
; So first, we need the script's path (some more string-replacing stuff)
icon := ""
path2 := StrReplace(path, "\\", "\", 0)
path2 := StrReplace(path2, "\build", "", 0)

; Then if an icon is found, save its path
Loop, Files, %path2%/*.ico
{
	icon := path2 . "\" . A_LoopFileName
	icon := StrReplace(icon, "\", "\\", 0)
	break
}

; Then run cxfreeze and we're good
if (icon != "")
	RunWait, %comspec% /c python C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts\\cxfreeze "%file%" --target-dir "%path%" --icon "%icon%", , Hide
else
	RunWait, %comspec% /c python C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts\\cxfreeze "%file%" --target-dir "%path%", , Hide
TrayTip, Freezer, Python script "frozen" successfully
Enjoy :)
tyio

Re: Freezer - A GUI for Python's cx_freeze

24 Apr 2018, 00:47

just curious if you think there are things you can do with python that u can't with ahk?

I remember trying to compile py scripts and half the time one of the import modules messed things up and the other times I ended up with a simple program that was 59mb
Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

Re: Freezer - A GUI for Python's cx_freeze

24 Apr 2018, 06:20

The short version is "libraries".

AHK is awesome, and on Windows I'd say as good as or maybe even better than Python for most things (cuz as you pointed out Python seems to have way too many compatibility problems). But people aren't writing AHK bindings to popular open-source libraries, or implementing new libraries in AHK. So if I wanted to use something like PyGame (SDL), OpenCV (a computer vision lib), etc. I would have to basically build the AHK version of the library first (involving a ton of DllCall, VarSetCapacity, Num/Str/Put/Get and a bunch of other low-level crap I don't feel like taking on alone). Then there are libraries like Keras (machine learning) that are built in Python; there's no DLL or COM API to call. So if I want to use any of these, my best bet is to use Python instead of AHK. But then there are little scripts like this where AHK is better, so I guess it's the same as always - the tools you choose depend on the project.
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: Freezer - A GUI for Python's cx_freeze

24 Apr 2018, 09:33

When talking about Windows, and especially automation, you cannot compete much with AHK for ease of use. But when you talk about *nix programming, etc, and neat, structured, strict code, Python is the nicest looking modern language.
try it and see
...

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 44 guests