How to show/hide windows console in systray? - python

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.

Related

Issue when trying to convert .py to .exe

Hello so I'm kind of new with programming and I buildt a program with python and kivy. The issue is that I am using auto-py-to-exe to convert the py files to exe but when I do it, it doesn't work well. The primary issue is that a windows command prompt opens when I run the app and if I close it, the app closes too. The other problem is that I can't share the file to someone else so that they can run it in another computer, and what happens is that the same command prompt kind of appears but then closes and the app doesn't open at all. I would appreciate it if someone could help me to understand how I can debug it as I don't even understand the problem in the first place.
I have been looking online for other people with the same issues but I haven't find any answer that applied to my case.
Try to choose Window Based(hide the console) and it wil works
If you choose “Console Based,” the console will open after running the executable file, which is recommended if your script generates console-based outputs. However, if you don’t want to show the console outputs when running the executable file, choose “Window Based”

Hide console window of python .py on windows

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.

When the console is selected, how do I get the main window to come to the front?

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

Integration of console window with GUI using TkInter

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.

Is it possible to minimize the console in python with the standard librairy (without extra module)?

I wrote a program that uses the console. Most of the time, the user must see the console informations. For a specific function from command line, I would like to run the script without the console rises. I just don't want see the window but it can be in the task bar. I know I can use extra modules (gui, win32,..) to do that but I would like to use the standard python librairy.
Is it possible to do that?
The program should run on Windows. (python 2.7)
I specify... I know I can use pythonw.exe too. The question then is how to launch the same script with python.exe sometimes and with pythonw.exe (from command line) for a specific function?
Found this question via google, so to answer the question, how to minimize (not completely hide) the console window on Windows when running a Python script:
# Python 3
import ctypes
ctypes.windll.user32.ShowWindow( ctypes.windll.kernel32.GetConsoleWindow(), 6 )
GetConsoleWindow() will return the window handle for the current console.
ShowWindow(hWnd, nCmdShow) will set the properties for the specific window. 6 is SW_MINIMIZE. Click on the link for other parameters.
on windows there are two python executables in your installation, one is "python.exe", which is the one you typically use. There is another called "pythonw.exe" which is for gui programs. It works just like python.exe, but does not display a console at all.
My first idea would be to have a .pyw-script for that specific task, that is launched in this case. Then only the original script's console pops up for a short time.

Categories

Resources