PySide6实现窗口居中

pyside 中居中窗口的代码如下:

center = QScreen.availableGeometry(QApplication.primaryScreen()).center()
geo = mainwindow.frameGeometry()
geo.moveCenter(center)
mainwindow.move(geo.topLeft())

 

完整示例代码: 

import sys
from PySide6.QtCore import Slot
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QSizePolicy
from PySide6.QtGui import QScreen


@Slot()
def move():
    center = QScreen.availableGeometry(QApplication.primaryScreen()).center()
    geo = mainwindow.frameGeometry()
    geo.moveCenter(center)
    mainwindow.move(geo.topLeft())


app = QApplication(sys.argv)
mainwindow = QMainWindow()
button = QPushButton("点我居中!", mainwindow)
button.clicked.connect(move)
mainwindow.setGeometry(0, 0, 100, 100)
mainwindow.show()

app.exec_()

 

运行效果:

 

如果您有多个屏幕/显示器,请配合PySide6获取屏幕大小 - 水汐音 - 博客园 (cnblogs.com)食用 

 

参考:

pyside6 Center Window. (loekvandenouweland.com)

Porting Applications from PySide2 to PySide6 — Qt for Python

QScreen Class | Qt GUI 6.2.0

posted @ 2021-10-27 23:04  水汐音  阅读(1256)  评论(0编辑  收藏  举报