user975135
user975135

Reputation:

GUI program with toggable console?

I've seen some apps allow you to show/hide the console when you need to read log messages. For example Blender3D allows that (blender.org).

I was wondering if this can be done in Python and how.

My main window is a Panda3D (panda3d.org) window. I've read somewhere that one option is to hide the "real" console (pythonw) and create another console and just redirect everything from the "real" one to it, every time you want to "show" the "real" console. No idea how this can be done.

Or at least a way to choose whether to start the program with the console or without it by reading a configuration file or something.

Upvotes: 1

Views: 654

Answers (2)

Preminster
Preminster

Reputation: 495

If the user launches your program from their own console, for example Windows Terminal, hiding the console will minimise their window. To hide only a console launched by your own program, you should check that it is not owned by other processes first.

from win32gui import ShowWindow
from win32con import SW_HIDE, SW_RESTORE
from win32console import (GetConsoleWindow,
                          GetConsoleProcessList)

def toggle_sysconsole(show=False):
    con = GetConsoleWindow()
    if con == 0:
        print("No attached console window")
        return
    if len(GetConsoleProcessList()) == 1:
        ShowWindow(con, SW_RESTORE if show else SW_HIDE)
    else:
        print("This process does not own the attached console")

Notes:

  • GetConsoleWindow returns 0 when there is no attached console. If you call GetConsoleProcessList when there is no attached console, it will throw an invalid handle error.
  • In the win32 API, GetConsoleProcessList takes a buffer to store the list in and returns the length of the list, but in pywin32 it’s different: it takes no arguments and returns the list itself.
  • I used SW_RESTORE instead of SW_SHOW because according to the documentation on SW_RESTORE: “If the window is minimized, maximized, or arranged, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.

Upvotes: 0

lukad
lukad

Reputation: 17863

I'm assuming you are talking about Windows because this console toggling in blender is Windows exclusive. I'm guessing Blender uses GetConsoleWindow and ShowWindow on Windows.

This is how you could do it in python with pywin32:

import win32gui, win32console, win32api, win32con
import time

console_window = win32console.GetConsoleWindow()
time.sleep(1)
win32gui.ShowWindow(console_window, win32con.SW_HIDE)
time.sleep(1)
win32gui.ShowWindow(console_window, win32con.SW_SHOW)
time.sleep(1)

If you run this program with python and not pythonw it will show the console, sleep for a second, hide the console, sleep for another second and then hide it again.

Mind that this code only works on Windows. On other platforms silly stuff like this is not necessary because if you want a program to show a console then you run it from the console.

Upvotes: 2

Related Questions