1 """主窗口居中""" 2 from PyQt5.QtWidgets import QDesktopWidget, QApplication, QMainWindow 3 import sys 4 5 class Winform(QMainWindow): 6 7 def __init__(self): 8 super().__init__() 9 self.setWindowTitle('主窗口放在屏幕中间例子') 10 self.resize(370, 250) 11 self.center() 12 13 14 def center(self): 15 # 计算屏幕的大小 16 screen = QDesktopWidget().screenGeometry() 17 # 获取窗口的大小 18 size = self.geometry() 19 # 将窗口移动到屏幕的中央 20 self.move((screen.width() - size.width())/2, (screen.height() - size.height()) /2 ) 21 22 if __name__ == '__main__': 23 app = QApplication(sys.argv) 24 win = Winform() 25 win.show() 26 sys.exit(app.exec_())