I want to control usage of the [X]
button (the exit window button), which is generally present in the top-right corner when running a console-based Python
application. I will be providing an in-program call to: sys.exit()
when required. I know there is a similar option available even for Java
GUI
programming.
Asked
Active
Viewed 3,476 times
3

matsjoyce
- 5,744
- 6
- 31
- 38

Sarvesh EB
- 93
- 2
- 10
-
@SarveshEB Which type of terminal are you using? – matsjoyce Mar 27 '15 at 17:23
-
@matsjoyce Just the default. This is how mine looks: [link](http://i.imgur.com/f50S3N7.png?1) – Sarvesh EB Mar 28 '15 at 01:29
1 Answers
6
Try:
import win32console, win32gui, win32con
hwnd = win32console.GetConsoleWindow()
if hwnd:
hMenu = win32gui.GetSystemMenu(hwnd, 0)
if hMenu:
win32gui.DeleteMenu(hMenu, win32con.SC_CLOSE, win32con.MF_BYCOMMAND)
It requires the pywin32 modules to be installed. Note: I have not tested this, as I haven't got access to a Windows machine ATM.
References: