How to get the height of Windows Taskbar using Python/PyQT/Win32

Question:

I am trying to make my GUI program align to the bottom-right of the screen on Windows. When the taskbar is not hidden, my program will just stand on top of the taskbar!

When using Python/PyQT/Win32, how can I:

  1. Check if the taskbar’s autohide function is on
  2. Get the height of the taskbar
Asked By: good man

||

Answers:

I think you need to call GetMonitorInfo for the monitor of interest. You then need to read the work area out of MONITORINFO.rcWork. This will exclude any part of the monitor reserved for taskbar and indeed any other reserved areas.

I don’t believe you need to worry yourself about autohide because GetMonitorInfo should account for that. In other words when autohide is enabled the work area will equal the monitor area.

Answered By: David Heffernan

As David Heffernan mentioned, you can use GetMonitorInfo with pywin32 to retrieve the monitor size. In particular, the work area will exclude the size of the taskbar.

To get the work area size (desktop minus taskbar):

from win32api import GetMonitorInfo, MonitorFromPoint

monitor_info = GetMonitorInfo(MonitorFromPoint((0,0)))
work_area = monitor_info.get("Work")
print("The work area size is {}x{}.".format(work_area[2], work_area[3]))

The work area size is 1366×728.

To get the taskbar height:

from win32api import GetMonitorInfo, MonitorFromPoint

monitor_info = GetMonitorInfo(MonitorFromPoint((0,0)))
monitor_area = monitor_info.get("Monitor")
work_area = monitor_info.get("Work")
print("The taskbar height is {}.".format(monitor_area[3]-work_area[3]))

The taskbar height is 40.

Explanation

First, we need to create a handle referencing the primary monitor. The primary monitor always has its upper left corner at 0,0, so we can use:

primary_monitor = MonitorFromPoint((0,0))

We retrieve information about the monitor with GetMonitorInfo().

monitor_info = GetMonitorInfo(primary_monitor)
# {'Monitor': (0, 0, 1366, 768), 'Work': (0, 0, 1366, 728), 'Flags': 1, 'Device': '\\.\DISPLAY1'}

The monitor information is returned as a dict. The first two entries represent the monitor size and the work area size as tuples (x position, y position, height, width).

work_area = monitor_info.get("Work")
# (0, 0, 1366, 728)
Answered By: Stevoisiak

You can use QDesktopWidget to retrieve information about the system’s screens and subtract the working area from the total screen area.

import sys
from PyQt5.QtWidgets import QApplication

app = QApplication(sys.argv)
dw = app.desktop()  # dw = QDesktopWidget() also works if app is created
taskbar_height = dw.screenGeometry().height() - dw.availableGeometry().height()

In the case that the taskbar is on the sides of the screen however, this would return zero which is not particularly helpful. To resolve this issue, find the difference between screenGeometry() and availableGeometry() to find the size of the taskbar (and any other reserved spaces).

When the taskbar is set to autohide, the available geometry is unaware of the taskbar’s size.

Answered By: FThompson

This is how I did it with the builtin ctypes library

from ctypes import windll, wintypes, byref

SPI_GETWORKAREA = 0x0030
    
# This var will receive the result to SystemParametersInfoW  
desktopWorkingArea = wintypes.RECT()

_ = windll.user32.SystemParametersInfoW(SPI_GETWORKAREA,0,byref(desktopWorkingArea),0)

left = desktopWorkingArea.left
top = desktopWorkingArea.top
right = desktopWorkingArea.right
bottom = desktopWorkingArea.bottom

 

and getting your full screen values with taskbar can be as simple as

x,y = windll.user32.GetSystemMetrics(0), windll.user32.GetSystemMetrics(1)

which for me gives

print(left,top,right,bottom)
print(x,y)

0 0 5120 1400
5120 1440

so my taskbar is on the Y axis, 40px.

Answered By: Gab ПК
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.