【问题标题】:PyQt5 Weird window position issuePyQt5奇怪的窗口位置问题
【发布时间】:2016-06-15 20:55:49
【问题描述】:

上面的代码,奇怪的是有问题。我不明白为什么,但如果我将窗口的y 大小设置为大于923,则窗口不会放在屏幕中间。它转到屏幕的左上角。

from PyQt5.QtWidgets import QApplication,QDesktopWidget,QMainWindow
from PyQt5 import QtCore

import sys

class cssden(QMainWindow):
    def __init__(self):
        super().__init__()


        self.mwidget = QMainWindow(self)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)


        #size
        self.setFixedSize(1100,923) # <--- set this 924 and more
        self.center # <-- function that set the window middle of the screen
        self.show()

    def center(self): # <-- center function
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

app = QApplication(sys.argv)
app.setStyleSheet("QMainWindow{background-color: rgb(30,30,30);border: 1px solid black}")

ex = cssden()
sys.exit(app.exec_())

为什么?这是关于我的屏幕分辨率为 1920x1080 的问题吗?我只是不明白为什么号码是923 以及如何解决这个问题。

编辑:原来 PyQt 自动居中窗口,923 之后为什么它会转到屏幕的左上角?每个分辨率都有默认数字吗?

【问题讨论】:

  • 我注意到你实际上并没有打电话给center()。会不会是这个问题。
  • @BrendanAbel 不,在923 下方,窗口位于屏幕中心。看到self.center
  • @GLHF @BrendanAbel 是对的。虽然窗口可能会居中,但您实际上并没有在上面的代码中调用 self.center() 。你只是得到参考。您需要self.center() 中的括号。 :-)
  • @jszakmeister 但是如果我不调用它,为什么窗口会在屏幕中间923 下方?
  • 我注释掉了你的中心例程和 self.center 行,它仍然使窗口居中。它必须是默认行为,直到您超过屏幕的某个百分比。添加括号可以解决问题。

标签: python python-3.4 pyqt5


【解决方案1】:

从这个答案尝试这个解决方案:PyQt4 center window on active screen

def center(self):
    frameGm = self.frameGeometry()
    screen = QtGui.QApplication.desktop().screenNumber(QtGui.QApplication.desktop().cursor().pos())
    centerPoint = QtGui.QApplication.desktop().screenGeometry(screen).center()
    frameGm.moveCenter(centerPoint)
    self.move(frameGm.topLeft())

此功能基于鼠标点所在的位置。它用 screenNumber 函数来确定鼠标是哪个屏幕 当前激活。然后它找到该监视器的 screenGeometry 和那个屏幕的中心点。使用这种方法,你应该 即使监视器也能够将窗口放置在屏幕的中心 分辨率不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    相关资源
    最近更新 更多