7

I'm trying to figure out how to get my program to open in a fullscreen console window.

Is there any command that you can type within the command prompt to toggle fullscreen?

If so I'd imagine the code going something like:

from os import system system("toggle.fullscreen") {CODE HERE} 

I understand mode con can be used, but that doesn't actually toggle it being maximized, which would be much more useful for me, thanks!

13
  • Windows CMD can't do full screen anyway, AFAIK Commented May 14, 2017 at 0:10
  • That is incorrect. I can press the maximize button on CMD and it works fine. When I say fullscreen I don't mean like the fullscreen where it takes a while to tab out. I mean fullscreen windowed. Sorry for the confusion. Commented May 14, 2017 at 0:11
  • 1
    Possible duplicate of How to make a batch file fullscreen? Commented May 14, 2017 at 0:12
  • 1
    cmd.exe is just a shell that can use a console (instance of conhost.exe). The full-screen feature was dropped in Vista, but it was restored in Windows 10 -- via Alt+Enter and F11. In all versions you can maximize the console window, but prior to Windows 10 or with the legacy console in Windows 10, you have to resize the screen buffer first if you want to take up the whole screen. Commented May 14, 2017 at 0:14
  • I understand that you can use alt+enter, but I want the program to do it automatically, is there any way? Commented May 14, 2017 at 0:16

3 Answers 3

7

Here's a function to maximize the current console window. It uses ctypes to call WinAPI functions. First it calls GetLargestConsoleWindowSize in order to figure how big it can make the window, with the option to specify a number of lines that exceeds this in order to get a scrollback buffer. To do the work of resizing the screen buffer it simply calls mode.com via subprocess.check_call. Finally, it gets the console window handle via GetConsoleWindow and calls ShowWindow to maximize it.

import os import ctypes import msvcrt import subprocess from ctypes import wintypes kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) user32 = ctypes.WinDLL('user32', use_last_error=True) SW_MAXIMIZE = 3 kernel32.GetConsoleWindow.restype = wintypes.HWND kernel32.GetLargestConsoleWindowSize.restype = wintypes._COORD kernel32.GetLargestConsoleWindowSize.argtypes = (wintypes.HANDLE,) user32.ShowWindow.argtypes = (wintypes.HWND, ctypes.c_int) def maximize_console(lines=None): fd = os.open('CONOUT$', os.O_RDWR) try: hCon = msvcrt.get_osfhandle(fd) max_size = kernel32.GetLargestConsoleWindowSize(hCon) if max_size.X == 0 and max_size.Y == 0: raise ctypes.WinError(ctypes.get_last_error()) finally: os.close(fd) cols = max_size.X hWnd = kernel32.GetConsoleWindow() if cols and hWnd: if lines is None: lines = max_size.Y else: lines = max(min(lines, 9999), max_size.Y) subprocess.check_call('mode.com con cols={} lines={}'.format( cols, lines)) user32.ShowWindow(hWnd, SW_MAXIMIZE) 
Sign up to request clarification or add additional context in comments.

1 Comment

Absolutely perfect! Thank you!! :)
7

You can use keyboard.press. Install with pip3 install keyboard if it is not installed.

Code:

import keyboard keyboard.press('f11') 

1 Comment

Smart and hacky, but not optimal in my opinion.
3

I found this awhile back on a different post and it works perfectly for console window maximization:

import win32gui, win32con hwnd = win32gui.GetForegroundWindow() win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.