PyQt5實戰——《窗口屏幕中居中顯示》


# QDesktopWidget
import sys
from PyQt5.QtWidgets import QDesktopWidget,QMainWindow,QApplication
from PyQt5.QtGui import QIcon

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(int(newLeft),int(newTop))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = CenterForm()
    main.center()  #加上以后效果好多了
    main.show()
    sys.exit(app.exec_())


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM