如何使用python PyQt5确定我的应用程序窗口的活动屏幕(监视器)?

问题描述 投票:0回答:1

我正在使用许多小部件(QGroupBox,QVBoxLayout,QHBoxLayout)的应用程序上工作。最初,它是在普通高清监视器上开发的。但是,最近我们许多人将其显示器升级为4K分辨率。现在,一些按钮,滑块被压缩得很小,无法使用。

现在我尝试进行一些更改,以便可以在HD和4K显示器中使用该应用程序。

我开始阅读下面的链接

https://leomoon.com/journal/python/high-dpi-scaling-in-pyqt5/ enter link description here

我以为只要在特定的监视器中打开窗口,我就可以调用特定的行代码

if pixel_x > 1920 and pixel_y > 1080:
  Qapp.setAttribute(Qt.AA_EnableHighDpiScaling, True)
  Qapp.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
else:
  Qapp.setAttribute(Qt.AA_EnableHighDpiScaling, False)
  Qapp.setAttribute(Qt.AA_UseHighDpiPixmaps, False)

现在,我尝试通过使用相关文章here使用以下代码来计算pixel_x和pixel_y。

import sys, ctypes

user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
screen_width  = 0 #78
screen_height = 1 #79
[pixel_x , pixel_y ] = [user32.GetSystemMetrics(screen_width), user32.GetSystemMetrics(screen_height)]

screen_width = 0,screen_height = 1为我提供了主显示器的分辨率(在我们的情况下,大多数笔记本电脑为高清)。 screen_width = 78,screen_height = 79为我提供了虚拟机的综合分辨率。但是,我不明白如何根据打开应用程序的位置动态获取这些值。

我的应用程序窗口的开发方式是,它将在上次关闭的同一监视器中打开。现在的问题是,我想在调用GUI时获得活动的监视器分辨率,并适应该分辨率。如果有人可以帮助我,我会感到很高兴。

我有兴趣知道每次将窗口从HD拖动到4K时都可以调用屏幕分辨率计算,反之亦然。

编辑:我在这篇文章here中发现了类似的东西,但是我从中不能得到很多。

提前感谢

python user-interface pyqt5 resolution pythoninterpreter
1个回答
0
投票

我来到across的一个解决方案是使用临时QApplication()

import sys
from PyQt5 import QtWidgets, QtCore, QtGui

# fire up a temporary QApplication
def get_resolution():

    app = QtWidgets.QApplication(sys.argv)

    print(app.primaryScreen())

    d = app.desktop()

    print(d.screenGeometry())
    print(d.availableGeometry())
    print(d.screenCount())    

    g = d.screenGeometry()
    return (g.width(), g.height())

x, y = get_resolution()

if x > 1920 and y > 1080:
  QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
  QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
else:
  QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, False)
  QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, False)

# Now your code ...
© www.soinside.com 2019 - 2024. All rights reserved.