Integration of console window with GUI using TkInter - python

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.

Related

How can I make .py files run using Python Shell by default?

I made a Python GUI using Tkinter, but when I run it directly (double click the file) it opens the black python window but automatically closes by itself in less than half a second. I found a way to make it open the IDLE editor but it just opens the editor and doesn't run it.
I want it to run the way it runs when you open the IDLE editor and press Run Module. This runs it using Python Shell.
Is there a way I can make it automatically run using Python Shell?
Based on Mark Tolonen's comment you should do two things
rename your file to a .pyw from .py to prefer console-less runs
set your system to open .pyw files with pythonw if that's not configured already
Linux: configure xdg-open
Windows: right click and choose an application from the context menu (you may need to find where Python is installed to select the pythonw.exe executable)
Okay, one of the comments on the original question is correct.
As Terry Jan Reedy (user:722804) said,
It is possible that your mygui.py file is missing 'root.mainloop()' or the equivalent to start the GUI. IDLE lets you omit that during development so that one can interact with tkinter to retrieve values and make changes to widgets.
Adding gui.mainloop() to the end of my program worked.

How to show/hide windows console in systray?

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.

Is there a way to use glade application without terminal in back?

I created a little application with Python3 and gtk glade, I'm using the terminal to see if I'm doing something wrong. However I want to launch the application without the terminal in the back at the end of the developement. So my question is, is there a way to do that?
You can change the file extension from .py to .pyw to make the Python script run without the terminal window. More on this here.

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

Categories

Resources