活动屏幕上的 PyQt4 中心窗口 [英] PyQt4 center window on active screen

查看:28
本文介绍了活动屏幕上的 PyQt4 中心窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使窗口在活动屏幕上居中而不在一般屏幕上居中?此代码将窗口移动到一般屏幕的中心,而不是活动屏幕:

How I can center window on active screen but not on general screen? This code moves window to center on general screen, not active screen:

import sys
from PyQt4 import QtGui

class MainWindow(QtGui.QWidget):

    def __init__(self):
        super(MainWindow, self).__init__()

        self.initUI()

    def initUI(self):

        self.resize(640, 480)
        self.setWindowTitle('Backlight management')
        self.center()

        self.show()

    def center(self):
        frameGm = self.frameGeometry()
        centerPoint = QtGui.QDesktopWidget().availableGeometry().center()
        frameGm.moveCenter(centerPoint)
        self.move(frameGm.topLeft())

def main():
    app = QtGui.QApplication(sys.argv)
    mainWindow = MainWindow()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

如果我从 initUI() 中删除 self.center() 则窗口在活动屏幕上的 0x0 上打开.我需要在活动屏幕上打开窗口并将此窗口移动到此屏幕的中心.谢谢!

If I removes self.center() from initUI() then window opened on 0x0 on active screen. I need to open window on active screen and move this window on center of this screen. Thansk!

推荐答案

修改你的center方法如下:

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 以及该屏幕的中心点.使用此方法,即使显示器分辨率不同,您也应该能够将窗口放置在屏幕中央.

This function is based on where the mouse point is located. It uses the screenNumber function to determine which screen the mouse is current active on. It then finds the screenGeometry of that monitor and the center point of that screen. Using this method, you should be able to place the window in the center of a screen even if monitor resolutions are different.

这篇关于活动屏幕上的 PyQt4 中心窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆