以下脚本显示了将窗口放在屏幕中间位置的方法。
#!/usr/bin/python
# -*- coding:utf-8 -*- import sys
from PyQt4 import QtGui class Center(QtGui.QWidget):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
self.setWindowTitle('moonlight poet center')
self.resize(250, 150)
self.center() def center(self):
screen = QtGui.QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width() - size.width())/2, (screen.height() - size.height())/2) app = QtGui.QApplication(sys.argv)
center = Center()
center.show()
sys.exit(app.exec_())
效果:
self.resize(250, 150)
该语句用来设置QWidget窗口的大小为250像素宽,150像素高。
screen = QtGui.QDesktopWidget().screenGeometry()
该语句用来计算显示器的分辨率(screen.width, screen.heigh)
size = self.geometry()
该语句用来获取QWidget窗口的大小(size.width, size.height)
self.move((screen.width() - size.width())/2, (screen.height() - size.height())/2)
该语句将窗口移动到屏幕的中间位置。