PyQt5笔记-让窗口居中

it2024-11-13  3

实现代码如下:

#通过QDesktopWidget获得整个屏幕的尺寸 import sys from PyQt5.QtWidgets import QDesktopWidget,QMainWindow,QApplication class 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()) / 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_())
最新回复(0)