#QDEeskopWidget
import sys#获取参数的api
from PyQt5.QtWidgets import QMainWindow,QDesktopWidget,QApplication#因为需要创建主窗口和应用程序
from PyQt5.QtGui import QIcon#专门用来添加图标的
class CenterForm(QMainWindow):#从主窗口继承
def __init__(self,parent = None):#把这些控件放到parent里
super(CenterForm,self).__init__(parent)
#设置主窗口的标题
self.setWindowTitle("让窗口居中")
#设置窗口尺寸
self.resize(400,300)
def center(self):
#获取屏幕坐标系
screen = QDesktopWidget().screenGeometry()#得到屏幕的坐标系
#获取窗口坐标系
size = self.geometry()
newLeft = (screen.width()-size.width())/2#计算
newTop = (screen.height()-size.height())/2
self.move(newLeft,newTop)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = CenterForm()
main.show()
sys.exit(app.exec_())