0

I have a GUI application where the console is hidden (using the pyw extension does not seem to allow the console to reappear) but now I need to make it reappear to allow user input. Here is my code currently:

import ctypes

kernel32 = ctypes.WinDLL('kernel32')
user32 = ctypes.WinDLL('user32')

SW_HIDE = 0

hWnd = kernel32.GetConsoleWindow()
user32.ShowWindow(hWnd, SW_HIDE)


import tkinter as tk

def show_console():

    kernel32 = ctypes.WinDLL('kernel32')
    user32 = ctypes.WinDLL('user32')

    SW_SHOW = 5

    hWnd = kernel32.GetConsoleWindow()
    user32.ShowWindow(hWnd, SW_SHOW)

tk.Frame()

a = tk.Button(text = 'Make Console Appear', command=show_console)
a.pack()

tk.mainloop()

This hides and shows the console as desired however when it is first launched the windows shows briefly, then disappears. How can I prevent this?

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • 1
    I think it's probably not possible. AFAIK the console appears when the python process starts, and it's visible until your program is loaded, parsed, and executed. Besides, I think what you're trying to do is ill-advised. If the user's PC is configured to show a python terminal while a python program is running, why would you go out of your way to hide that window? What if the user *wants* to see it? What if I'm working in a terminal and I start your program, and it hides the terminal that I was trying to work with? Why do you want to hide and show the terminal like this? – Aran-Fey May 18 '18 at 20:49
  • @Aran-Fey I see what you are saying in the first part. So what should I do? Create a second script and launch that as a separate visible console, rather than trying to integrate everything into one? – Xantium May 18 '18 at 20:58
  • To answer your question I am building a desktop application with its own custom terminal. When a button is clicked I need the terminal to open. – Xantium May 18 '18 at 20:59
  • 1
    I think you should stop trying to hide the terminal. If you want your own custom terminal, why not implement one with a tkinter dialog and a textarea? – Aran-Fey May 18 '18 at 21:01
  • @Aran-Fey I see. Then make it a child window. OK I think that might work. Thanks – Xantium May 18 '18 at 21:04
  • A GUI application doesn't automatically attach to a parent's console or allocate a new console, but it can call `AllocConsole` to get its own console window or `AttachConsole` to attach to the console of another process.. Then open the special file "CONIN$" to read from the console input buffer and "CONOUT$" to write to the console screen buffer. – Eryk Sun May 18 '18 at 22:20
  • @eryksun Thank you. That might be helpful. – Xantium May 18 '18 at 22:47

0 Answers0