首先我们要从 PyQt5.Widgets 导入 QDesktipWidget这个库。
The QDesktopWidget class provides information about the user's desktop, including the screen size.
意思是这个库提供了一些可以查看用户桌面信息的函数。
代码如下,解释详细:
# coding='utf-8'
from PyQt5.QtWidgets import QWidget, QApplication,\
QDesktopWidget
import sys
class Gui(QWidget):
def __init__(self):
super().__init__()
self.start()
def start(self):
self.resize(500, 600)
self.center()
self.show()
def center(self):
self.move(0, 0)
# 得到我们创建的主窗体的长宽矩阵
f_size = self.frameGeometry()
# 得到用户电脑的中心位置
desktop_center = QDesktopWidget().availableGeometry().center()
# 把我们创建的矩阵(虚拟的)移动到用户电脑的中心位置\
# 我们的主窗体此时还没有移动
f_size.moveCenter(desktop_center)
# 使我们的主窗体的左上角与我们创建的和主窗体长宽相同的矩阵的左上角对齐
# 没有这个就相当于没有移动,只是移动了虚拟的和主窗体长宽相同的矩阵
self.move(f_size.topLeft())
win = QApplication(sys.argv)
gui = Gui()
sys.exit(win.exec_())
运行结果:

网友评论