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:
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With