【问题标题】:Module 'PyQt6.QtWidgets' has no attribute 'QDesktopWidget'模块“PyQt6.QtWidgets”没有属性“QDesktopWidget”
【发布时间】:2021-09-03 08:32:33
【问题描述】:

我尝试运行这段代码,但它总是得到这个 AttributeError,我搜索了很多网站但没有任何答案。

QtWidgets.QDesktopWidget().availableGeometry().center()
AttributeError: module 'PyQt6.QtWidgets' has no attribute 'QDesktopWidget'

我的代码:

from PyQt6 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def center(self):
        qr = Form.frameGeometry()
        cp = QtWidgets.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

我正在使用 PyQt6 版本 6.1.0,Python 3.9.5

【问题讨论】:

    标签: python python-3.x pyqt python-3.9 pyqt6


    【解决方案1】:

    来自the docs:

    QDesktopWidget 和 QApplication::desktop()
    QDesktopWidget 已经 在 Qt 5 中已弃用,在 Qt 6 中已被删除,连同 QApplication::desktop()。

    QScreen 提供等效的信息查询功能 关于可用屏幕、构成虚拟桌面的屏幕,以及 屏幕几何形状。

    使用 QWidget::setScreen() 在特定显示器上创建 QWidget; 请注意,这不会将小部件移动到虚拟桌面中的屏幕 设置。

    然后使用:

    cp = QtGui.QGuiApplication.primaryScreen().availableGeometry().center()
    

    【讨论】:

      猜你喜欢
      • 2023-02-06
      • 2020-07-26
      • 1970-01-01
      • 2020-06-04
      • 2021-12-24
      • 2015-05-08
      • 2020-10-17
      • 2020-10-03
      • 2018-08-11
      相关资源
      最近更新 更多