开发者

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 scr开发者_C百科een 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


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)


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜