I found this answers on Stackoverflow but nothing works for me
How to hide console window in python?
Hiding the console window
Hide console window with Tkinter and cx_Freeze
I'm trying to hide console window, on windows at least. But with solutions above still I have window open
The only difference maybe I have to mark, I run python application with process from C# application, but as I understand it can't be a reason of some key different if I write all directives from the python code. Now I'm not sure how to figure out
You need to use the pythonw.exe program as it won't open a terminal when ran.
Related
I am finishing a python program which I will distribute as exe.
I use Python 3.7 on Windows 10 64b.
The user will double click on exe to run my program. Then a windows console open and display logs in this console.
My program doesn’t need any gui but I would like to give to the users the possibility to hide the console with the logs and to show it back whenever he/she wants from an icon in the systray.
I found various answers here but none was responding to my needs.
I think I am not the one who would like to have this option on our python script. It could be very useful.
I found a tutorial for building a systray icon in python:
https://youtu.be/WM27fMo5Gg8
But it is about opening windows program, not about showing/hiding its own console.
After further investigation, I found the solution. It works when I run my script in the terminal. I still didn't test with the exe of my program. I may come back here to edit my answer if some extra information is necessary for .exe files.
I found this famous script SystrayIcon.py for python 2 which help me to create my icon with a menu very quickly.
I found the Python 3 version of this script here.
Then I use GetConsoleWindow from types and ShowWindow from win32gui to hide and show the console.
def show(sysTrayIcon):
the_program_to_hide = ctypes.windll.kernel32.GetConsoleWindow()
win32gui.ShowWindow(the_program_to_hide, win32con.SW_SHOW)
def hide(sysTrayIcon):
the_program_to_hide = ctypes.windll.kernel32.GetConsoleWindow()
win32gui.ShowWindow(the_program_to_hide, win32con.SW_HIDE)
These 2 functions are connected to the menu of my systray icon.
I hope it will help someone one day.
PS: The StackOverflow editor bug and I couldn't manage to show the code of the 2 functions properly.
I have no idea if this problem has the solution I want but here goes.
I'm making a PyQt4 program that apparently needs the console window to run properly. However, whenever I activate another window, sending the program I'm working on to the back, the only way I can get back to it is by closing all the windows in front of said window. I can't just click on the taskbar because the only thing that comes back is the console window.
I'm curious. Is there a way to have the GUI window activate along with, or independent of, the console window without having to go through the annoying process of closing (minimizing) potentially all the rest of your windows?
Edit: I just realized my question is pretty vague. Let me elaborate.
I'm compiling said program using pyinstaller.
The reason it needs the console window to work properly (I have tried using the .pyw file as well, to no avail) is because there's another program that's the core of this one that prints out to it in a way I can only describe as violently.
Apparently it won't be happy unless it has the console to record it's outbursts.
That being said, I need the console window. However, as I mentioned before, that is the only thing that comes up when the pyinstaller icon is clicked.
There is a gui attached to the console, but there's no way to get it back even after the user would minimize it because the pyinstaller icon insists it doesn't exist.
Maybe it has something to do with how I defined the window while programming it, but I don't see why that would be the case. Is there something in particular pyinstaller doesn't like that would make it act like this?
How are you launching the PyQt application?
If you're launching it with the python executable, it will create a console.
python my_application.py
Instead, launch it with the GUI version of python -- pythonw:
pythonw my_application.py
If the python path isn't in the system path, you may need to specify the whole path to the executable:
C:\python27\pythonw.exe C:\path\to\my_application.py
I have a python script that runs the GUI which is coded using Tkinter.
Problem is when i run the script, there are 2 windows opened. One is the GUI and other is the black console window.
I need to integrate both the windows so that when I start the script only one window appears.
Any ideas are much appreciated.
Thanks in advance.
Rename your Python script as .pyw (not .pyc). This will tell the invoker to to instantiate a console window. Source
Note however, this will work for non-GUI based scripts too which can cause undesireqable behaviour - such as not being able to see your script.
(I am pretty sure there is a better way, but) one way is to change the .pyc object file extension to .pyw and the console will not appear when you launch your GUI using the .pyw file.
I am a complete newbie at Python. Meanwhile, I'm working with Tkinter to integrate a GUI into my application using Python 2.7. This is the code so far:
import Tkinter
top = Tkinter.Tk()
top.mainloop()
However, when I execute the file, a console window and the GUI pops up. How do I get rid of the console window during startup?
Rename your Python script as .pyw (not .pyc). This will tell the invoker to to instantiate a console window. Source
Note however, this will work for non-GUI based scripts too which can cause undesireqable behaviour - such as not being able to see your script.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I hide the console window in a PyQt app running on Windows?
I am programming under Python2.6 + PyQT + Eric4 environment.
All the GUI and Program parts are done, but here is the problem.
When I run my program, two windows pop up.
One is the window (or frame) I want, the other is like a python interpretor window having a all black undergroud color. And I really don't want this to show.
What can I do to get rid of it?
Please help me out of this.
I suppose you are using Windows, the only operating system I know to open a prompt when you double click a script. There are two solutions AFAIK: execute the file with the pythonw.exe executable, as suggested by #Adrien. If you save the file with the .pyw extension, Windows automatically uses pythonw.exe for executing the script when you double click
on windows, you can get rid of the console window by using pythonw.exe to run your script (instead of the standard python.exe)
(i don't know if there is a similar difference on other operating systems)
On Windows, the program window only pops up if you save your python file with the extension .py instead of .pyw.