使用的API:QDesktopWidget
import sys
from PyQt5.QtWidgets import QDesktopWidget,QMainWindow,QApplication
from PyQt5.QtGui import QIcon#添加图标
class centerform(QMainWindow):
def init(self):
super(centerform,self).init()
#设置主窗口的标
self.setWindowTitle(‘将窗口设置在屏幕中央’)
#设置窗口的尺寸
self.resize(400,300)
def center(self):
#获取屏幕坐标系
screem = QDesktopWidget.screenGeometry()
#获取窗口坐标系
size = self.geometry()
newLeft = (screem.width()-size.width())/2
newTop = (screem.height()-size.height())/2
self.move(newLeft,newTop )
if name == ‘main’:
app = QApplication(sys.argv)
main = centerform()
main.show()
sys.exit(app.exec_())