QMainWindow主窗体当窗体大小小于屏幕时打开在左上角显示。
利用QDesktopWidget类可以实现主窗口居中显示。The QDesktopWidget class provides access to screen information on multi-head systems.
# !usr/bin/python # _*_ coding: utf-8 _*_ from PyQt4 import QtGui class QMainWindow(QtGui.QMainWindow): """ Class documentation goes here. """ def __init__(self, parent = None): """ Constructor """ QtGui.QMainWindow.__init__(self, parent) self.resize(400, 300) self.setWindowTitle(u"父窗口") self.center() def center(self): #主窗口居中显示函数 screen=QtGui.QDesktopWidget().screenGeometry() size=self.geometry() self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2) if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) ui=QMainWindow() ui.show() app.exec_()