PythonProjects/QT/PyQt/2. 居中显示窗口.py
2024-03-01 14:06:22 +08:00

33 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!D:\Python311\python.exe
# -*- coding: utf-8 -*-
# @Time : 2024/2/26 13:34
# @Author : DC_DC
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLabel
from PyQt6.QtGui import QGuiApplication
from PyQt6.QtGui import QIcon
class MyApplication(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.setGeometry(0, 0, 300, 200)
self.setWindowIcon(QIcon('Photos/img.png'))
qr = self.frameGeometry() # 创建一个矩形对象qr它表示窗口的几何形状
cp = QGuiApplication.primaryScreen().availableGeometry().center() # 获取主屏幕的几何属性,计算出屏幕中心点的坐标
qr.moveCenter(cp) # 将窗口的几何形状移动到屏幕中心
self.move(qr.topLeft()) # 将窗口移动到矩形的左上角,以实现窗口剧中显示的效果
QLabel('Hello World!', self)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApplication()
ex.show() # 调用父类方法,显示窗口
sys.exit(app.exec())