原文:https://blog.csdn.net/zzx188891020/article/details/105940024
课程重点:
就是让窗口居中显示
# QDesktopWidget
import sys
from PyQt5.QtWidgets import QDesktopWidget,QMainWindow,QApplication
from PyQt5.QtGui import QIconclass CenterForm(QMainWindow):def __init__(self):super(CenterForm,self).__init__()# 设置主窗口的标题self.setWindowTitle('让窗口居中')# 设置窗口的尺寸self.resize(400,300)def center(self): # 定义一个函数使得窗口居中显示# 获取屏幕坐标系screen = QDesktopWidget().screenGeometry()# 获取窗口坐标系size = self.geometry()newLeft = (screen.width() - size.width()) / 2newTop = (screen.height() - size.height()) / 2self.move(int(newLeft),int(newTop))if __name__ == '__main__':app = QApplication(sys.argv)main = CenterForm()main.center() #课程的代码里少了这个调用,加上以后效果好多了main.show()sys.exit(app.exec_())