凌的博客

您现在的位置是: 首页 > 学无止境 > python > 

python

pyqt6 居中显示 pyqt5区别

2022-04-13 python 2261
# pyqt6
def center(self):
    qr = self.frameGeometry()
    cp = QGuiApplication.primaryScreen().availableGeometry().center()
    qr.moveCenter(cp)
    self.move(qr.topLeft())
    
    
# pyqt5
def center(self):
    qr = self.frameGeometry()
    cp = QDesktopWidget().availableGeometry().center()
    qr.moveCenter(cp)
    self.move(qr.topLeft())
    
    
    
# pyqt6 案例
# 安装 pip install -i https://mirrors.aliyun.com/pypi/simple PyQt6
import sys
from PyQt6.QtWidgets import QWidget, QToolTip, QPushButton, QApplication
from PyQt6.QtGui import QFont,QGuiApplication


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        QToolTip.setFont(QFont('Arial', 10))

        self.setToolTip('This is a <b>QWidget</b> widget')

        btn = QPushButton('Button', self)
        btn.setToolTip('This is a <b>QPushButton</b> widget')
        btn.resize(btn.sizeHint())
        btn.move(50, 50)

        self.setGeometry(300, 300, 300, 200)

        self.setWindowTitle('Tooltips')

        self.center()
        self.show()

    def center(self):
        qr = self.frameGeometry()
        cp = QGuiApplication.primaryScreen().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

def main():
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()


文章评论

0条评论