Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 217
good man Avatar asked Dec 05 '10 03:12

good man


People also ask

How do I find the size of my taskbar?

Click and hold the top of the taskbar where the desktop and taskbar meet. When the mouse hovers over this area, it should change to a double-sided arrow. Drag downward to make the taskbar smaller. Let go when it's at the size you want (stopping at the bottom of the screen is the smallest it can be with this method).

How do I increase taskbar height?

First, position your mouse cursor on the edge of the taskbar. The pointer cursor will change into the resize cursor, which looks like a short vertical line with an arrow head on each end. Once you see the resize cursor, click and drag the mouse up or down to change the height of the taskbar.


3 Answers

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 1366x728.

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)
like image 75
Stevoisiak Avatar answered Sep 27 '22 07:09

Stevoisiak


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.

like image 31
David Heffernan Avatar answered Sep 23 '22 07:09

David Heffernan


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.

like image 39
FThompson Avatar answered Sep 25 '22 07:09

FThompson