pyqt5mainwindow出现在屏幕中央
时间: 2023-12-12 21:05:55 浏览: 85
要将PyQt5的MainWindow出现在屏幕中央,可以使用以下代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDesktopWidget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置窗口大小
self.setGeometry(300, 300, 500, 400)
self.setWindowTitle('Centered MainWindow')
# 将窗口移动到屏幕中央
self.center()
self.show()
def center(self):
# 获取窗口大小
qr = self.frameGeometry()
# 获取屏幕中心点
cp = QDesktopWidget().availableGeometry().center()
# 移动窗口到屏幕中心
qr.moveCenter(cp)
self.move(qr.topLeft())
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
sys.exit(app.exec_())
```
这个例子创建了一个名为"Centered MainWindow"的窗口,并将其移动到屏幕中央。在`initUI`方法中,我们使用`self.center()`方法将窗口移动到屏幕中央。
希望这能帮到你!如果还有其他问题,请随时问。
阅读全文
相关推荐

















