cx_Freeze help: is there a way to NOT make console open? - python

I am trying to convert a python game (made with pygame) into a exe file for windows, and I did using cx_Freeze. No problems there.
The thing is that when I launch myGame.exe, it opens the normal Pygame window and a console window(which I do not want).
Is there a way to remove the console window? I read most of the documentation, but I saw nothing really (except base, but I don't get what that is).
BTW, here is my setup file:
import cx_Freeze
exe = [cx_Freeze.Executable("myGame.py")]
cx_Freeze.setup(
name = "GameName",
version = "1.0",
options = {"build_exe": {"packages": ["pygame", "random", "ConfigParser", "sys"], "include_files": [
"images", "settings.ini", "arialbd.ttf"]}},
executables = exe
)
Here's a screen shot of what happens when I launch the exe:

So what was wrong, was that the setup.py file was missing a parameter.
What you need to add is base = "Win32GUI" to declare that you do not need a console window upon launch of the application.
Here's the code:
import cx_Freeze
exe = [cx_Freeze.Executable("myGame.py", base = "Win32GUI")] # <-- HERE
cx_Freeze.setup(
name = "GameName",
version = "1.0",
options = {"build_exe": {"packages": ["pygame", "random", "ConfigParser", "sys"],
"include_files": ["images", "settings.ini", "arialbd.ttf"]}},
executables = exe
)

The parameter can be passed also by the shell if you are making a quick executable
like this:
cxfreeze my_program.py --base-name=WIN32GUI

Related

invalid syntax at cx_Freeze executables

I made a tkinter application in windows and now I want to make an executable version of it. multiframe.py contains all the the code of the tkinter application.
But when I try to build it I always get syntax error but I don't get it why. Here is a cmd snapshot.
This is how my setup.py looks like:
import cx_Freeze
base = None
if sys.platform == 'Win32':
base = "Win32GUI"
executables = [cx_Freeze.Executable("frame.py"), base=base, icon='ds.ico']
cx_Freeze.setup(
name="cuQ",
options = {"build_exe": {"packages":["tkinter"], include_files=["ds.ico"]},
version= "0.01",
description = "dasdasd",
executables = executables
)
base and icon are options of cx_Freeze.Executable in your code they're not being passed to it. They need to be in () just like "frame.py" is so use:
executables = cx_Freeze.Executable("frame.py", base=base, icon='ds.ico')
In cx_Freeze.setup(options = ... you're first adding a dictionary key "packages" as part of the value for the dictionary key "build_exe" but then suddenly you're trying to add a list include_files instead of a key while still inside the dictionary that is the part of the value alongside "packages" to "build_exe" key. It's hard to describe. Anyway.
Your whole code should look like:
import cx_Freeze, sys
base = None
if sys.platform == 'Win32':
base = "Win32GUI"
executables = cx_Freeze.Executable("frame.py", base=base, icon='ds.ico')
cx_Freeze.setup(
name="cYou",
options = {"build_exe": {"packages":["tkinter"], "include_files":["ds.ico"]}},
version= "0.01",
description = "dasdasd",
executables = executables
)
Below is what I use for tkinter. I just put my tkinter script something.py next to this script. Then I just respond to it something. Some modifications may need to be done in order to include icon files and such:
from cx_Freeze import setup, Executable
import sys, os
fileName = input("What's the name of the py file to be converted to .exe?\n")
sys.argv.append('build')
os.environ['TCL_LIBRARY'] = r'C:\Users\username\AppData\Local\Programs\Python\Python36\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\username\AppData\Local\Programs\Python\Python36\tcl\tk8.6'
base = None
if (sys.platform == "win32"):
base = "Win32GUI" # Tells the build script to hide the console.
elif (sys.platform == "win64"):
base = "Win64GUI" # Tells the build script to hide the console.
setup(
name='KutsalAklinNerde?',
version='0.1', #Further information about its version
description='Parse stuff', #It's description
executables=[Executable(fileName + ".py", base=base)])

cx_freeze shortcut doesn't work

Hello I'm a newbie in python.
I am trying to make an msi file.
It works fine when I run the exe file in the built directory.
But the shortcut that I made on the DesktopFolder won't work.
The reason seems like the shortcut file can't reference UI files.
When I copy & paste the UI files on the DesktopFolder, it does work.
Is there a way to execute shortcut file properly without moving UI files?
Here's my source code.
import sys
from cx_Freeze import setup, Executable
company_name = '####'
product_name = 'Robot Parser V1.0'
exebuildOptions = dict(packages=[],
excludes=[],
includes=['atexit'],
include_files=['qwed.ui', 'sub.ui'])
bdist_msi_options = { 'upgrade_code': '{parser-04291986}',
'add_to_path': False,
'initial_target_dir': r'[ProgramFilesFolder]\%s\%s' % (company_name, product_name)}
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
executables = [Executable('__init__.py', base=base, shortcutName="Robot.ini Parser V1.0",
shortcutDir="DesktopFolder")]
setup(name='Roboparser',
version='0.1',
description='Sample cx_Freeze PyQt4 script',
options={'build_exe' : exebuildOptions, 'bdist_msi': bdist_msi_options},
executables=executables
)
Thank you for your help in advance.

cx_freeze & bundling files

At present I am using pyinstaller for bundling my python application. I am equally migrating to pyGObject (due to pygtk being depreciated).
Now pyinstaller does not support pyGObject and I have as of yet not figured out the required hooks... One of the other downsides of pyinstaller is how it bundles into a single executable - it causes the company installed virus scanner to check quite intensively every time the exe is run ==> quite slow startup.
Looking into using cx_freeze due to the pyGObject & py3 support I note it does not have a single-executable option. That in itself isn't an issue if the working directory can be cleaned up, be it via the pyd/dll being bundled into a second zip or into a subdirectory.
Searching around (stackoverflow and other sites), it is illuded to that it can be done, but I am not getting the expected results. Any idea#s?
setup.py is based around this one: http://wiki.wxpython.org/cx_freeze
ok solved:
1) setup.py
import sys
from cx_Freeze import setup, Executable
EXE1 = Executable(
# what to build
script = "foo.py",
initScript = None,
base = 'Win32GUI',
targetDir = "dist",
targetName = "foo.exe",
compress = True,
copyDependentFiles = True,
appendScriptToExe = True,
appendScriptToLibrary = False,
icon = 'foo.ico'
)
setup(
version = "9999",
description = "...",
author = "...",
name = "...",
options = {"build_exe": {"includes": includes,
"excludes": excludes,
"packages": packages,
"path": sys.path,
"append_script_to_exe":False,
"build_exe":"dist/bin",
"compressed":True,
"copy_dependent_files":True,
"create_shared_zip":True,
"include_in_shared_zip":True,
"optimize":2,
}
},
executables = [EXE1]
)
2) foo.py header:
import os
import sys
if getattr(sys,'frozen',False):
# if trap for frozen script wrapping
sys.path.append(os.path.join(os.path.dirname(sys.executable),'bin'))
sys.path.append(os.path.join(os.path.dirname(sys.executable),'bin\\library.zip'))
os.environ['TCL_LIBRARY'] = os.path.join(os.path.dirname(sys.executable),'bin\\tcl')
os.environ['TK_LIBRARY'] = os.path.join(os.path.dirname(sys.executable),'bin\\tk')
os.environ['MATPLOTLIBDATA'] = os.path.join(os.path.dirname(sys.executable),'bin\\mpl-data')

Set an icon for a .exe file

I've made a .exe file of a game I made in Python, but I'm not sure how to make that exe file have an icon, because it looks dull and boring. I want to somehow make the icon be a picture of an asteroid let's say, because I made the game asteroids. I used cx_freeze to compile it.
Add it to the options in your setup.py file:
setup(
...
options={
"build_exe": {
"icon": "path/to/icon.ico"
}
}
)
This is easier to understand!
import cx_Freeze
import sys
base = None
if sys.platform == 'win32':
base = "Win32GUI"
executables = [cx_Freeze.Executable("example.pyw", base=base)]
cx_Freeze.setup(
name = "SeaofBTC-Client",
options = {"build_exe": {"packages":["tkinter",],"icon":"example.ico"}},
version = "0.01",
description = "Sea of BTC trading application",
executables = executables
)

Compiling with Py2Exe - Black Box Error

When trying to compile a GUI program using Py2Exe, whenever I load the GUI, a black box appears behind it.
Is there anyway to prevent this?
In your py2exe script, specify windows=['myscript.py'], instead of console=['myscript.py'],
Like so:
setup(
windows=['myscript.py'],
options={
"py2exe":{
"unbuffered": True,
"optimize": 2,
}
}
)
See py2exe List Of Options
You need to use the windows option to Setup rather than the console option.
setup(
# windows = [RPMApp],
console = [RPMApp, DBMigrate],
zipfile = 'common.bin',
options = options,
data_files = files,
cmdclass = {'py2exe': FileCollector}
)
Here, I actually have the console enabled for debugging, but I'll uncomment the windows option when I finish building for deployment.

Categories

Resources