0

I have prepared some tk application. It could be really simple like:

from tkinter import *
 
# create root window
root = Tk()
 
# root window title and dimension
root.title("Welcome to GeekForGeeks")
# Set geometry (widthxheight)
root.geometry('350x200')
 
# all widgets will be here
# Execute Tkinter
root.mainloop()

I have using some method to convert the app to the exe file. What is important, I'm not using and I cannot do it with pyinstaller py2exe etc. I also cannot use method with changing my app.py to app.pyw. But my conversion to .exe is working correctly.

The question - is it even possible to hide/disable/resize(reduce the size) of my console window and make the application still working?

I'm not exactly sure how is it done in pyinstaller py2exe etc, so maybe is it possible to do it inside an application?

tomm
  • 271
  • 2
  • 14
  • *I'm not using and I cannot do it with pyinstaller py2exe etc. I also cannot use method with changing my app.py to app.pyw. But my conversion to .exe is working correctly.* How do you convert your project into exe then and what does the documentation state? – Thingamabobs Oct 11 '22 at 20:10
  • You need to explain what you _are_ doing to create the executable. Windows executables are marked as console or windows applications. If marked as a console application, you will get a console. This is why python has both python.exe and pythonw.exe. – patthoyts Oct 11 '22 at 20:38
  • @Thingamabobs I'm using clicknium (https://www.clicknium.com/) to create a project and there is a possibility to create executable. As I know clicknium is not supporting the gui application - so looking for some different method to remove console :) – tomm Oct 11 '22 at 20:44
  • @patthoyts same comment as above – tomm Oct 11 '22 at 20:45

3 Answers3

0

All right, to solve above problem install:

pip install pywin32

and add code before running your tk gui application:

import win32gui
import win32.lib.win32con as win32con

the_program_to_hide = win32gui.GetForegroundWindow()
win32gui.ShowWindow(the_program_to_hide , win32con.SW_HIDE)

Then you can run the main.py in console, the console will disappear and the gui app will be still visible.

In case when you use pyinstaller etc - you can convert the application without "--noconsole" argument.

When you run the .exe file the console will appear for a second, and disappear. But the gui app will be still visible and usable.

Hope it help somebody somehow :)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
tomm
  • 271
  • 2
  • 14
  • How do you make sure that the foreground window is indeed the window you want to hide and not any other window that becomes foreground window at the time? In addition it is Windows specific and should be mentioned. Also it is unclear why you use `win32con` but import it as `win32con65s`. This answer as is, will unlikely become helpful to others. – Thingamabobs Oct 12 '22 at 16:07
0

I think you should run your script using pythonw.exe instead of python.exe. See .pyw files in python program

EvensF
  • 1,479
  • 1
  • 10
  • 17
0

Does this help if using Toplevel?

from tkinter import *

root = Tk()

root.title("Main Window")

root.geometry("200x200")

def launch():
    global second
    second = Toplevel()
    second.title("Child Window")
    second.geometry("400x400")

def show():
    second.deiconify()

def hide():
    second.withdraw()

Button(root, text="launch Window", command=launch).pack(pady=10)
Button(root, text="Show", command=show).pack(pady=10)
Button(root, text="Hide", command=hide).pack(pady=10)

root.mainloop()
toyota Supra
  • 3,181
  • 4
  • 15
  • 19