6

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!

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
Alex Rosenbach
  • 75
  • 1
  • 1
  • 7
  • Windows CMD can't do full screen anyway, AFAIK – OneCricketeer May 14 '17 at 00: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. – Alex Rosenbach May 14 '17 at 00:11
  • 1
    Possible duplicate of [How to make a batch file fullscreen?](http://stackoverflow.com/questions/26626068/how-to-make-a-batch-file-fullscreen) – Alexander O'Mara May 14 '17 at 00: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. – Eryk Sun May 14 '17 at 00:14
  • I understand that you can use alt+enter, but I want the program to do it automatically, is there any way? – Alex Rosenbach May 14 '17 at 00:16
  • Also, Alexander, I have seen that solution, it's not exactly what I am looking for since it doesn't really maximize the command prompt so it ends up being off-centered. – Alex Rosenbach May 14 '17 at 00:17
  • May be a better choice to write a Python script that runs a .bat to adjust the console in question. – pstatix May 14 '17 at 00:22
  • `start /max cmd` – Peter Wood May 14 '17 at 00:24
  • @PeterWood That is so close to what I need... The problem is that the program doesn't run in the new window, unlike mode cons for example. – Alex Rosenbach May 14 '17 at 00:27
  • @PeterWood Is there a way to make the CURRENT window maximized? – Alex Rosenbach May 14 '17 at 00:29
  • You have a Python script running in python.exe, which creates or inherits a console. There's not necessarily any instance of cmd.exe involved here, and it's completely irrelevant to the problem. You can easily have your script maximize its console window by calling `GetConsoleWindow` and `ShowWindow`, but resizing the screen buffer is more difficult at an API level. If you don't mind doing it crudely you can call out to `mode.com` to do the work for you. – Eryk Sun May 14 '17 at 00:29
  • @eryksun I'm a bit of a novice, how would you use the GetConsoleWindow and ShowWindow or mode.com?? – Alex Rosenbach May 14 '17 at 00:34
  • I can't find the release, but in Windows 10 it is now again possible to put a command line window into full screen mode (with a scrollbar) by pressing ALT+ENTER. – z0rberg's May 04 '22 at 22:05

3 Answers3

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)
Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
6

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

Code:

import keyboard
keyboard.press('f11')
Frightera
  • 4,773
  • 2
  • 13
  • 28
Ed u
  • 61
  • 1
  • 1
2

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)