PyQt5编程(9):窗口在屏幕的位置与管理
(2017-07-06 22:13:43)
标签:
杂谈 |
分类: PYTHON |
window.resize(300, 100)
window.move(10, 10)
rect = window.geometry()
print(rect.left(),
rect.top())
print(rect.width(),
rect.height())
rect = window.frameGeometry()
print(rect.left(),
rect.top())
print(rect.width(),
rect.height())
要让窗口显示在屏幕中央,就需要知道屏幕的尺寸。可调用静态函数QApplication.desktop()来获取代表桌面的QDesktopWidget
width():屏幕宽度的像素值;
height()::屏幕高度的像素值;
screenGeomtry():返回整个屏幕的位置和尺寸的QRect对象;
availableGeomtry():返回屏幕可用区域的位置和尺寸的QRect对象,即扣除任务栏的区域。
例1:
# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets
import sys
window = QtWidgets.QWidget()
window.setWindowTitle("在屏幕中央显示窗口")
window.resize(300, 100)
desktop = QtWidgets.QApplication.desktop()
x = (desktop.width() - window.width()) // 2
y = (desktop.height() - window.height()) // 2
window.move(x, y)
window.show()
sys.exit(app.exec_())
例2:
# -*- coding:
utf-8 -*-
from PyQt5 import QtWidgets
import sys
window = QtWidgets.QWidget()
window.setWindowTitle("在屏幕中央显示窗口")
window.resize(300, 100)
window.move(window.width() * -2,
0)
window.show()
desktop = QtWidgets.QApplication.desktop()
x = (desktop.width() - window.frameSize().width()) // 2
y = (desktop.height() - window.frameSize().height()) // 2
window.move(x, y)
sys.exit(app.exec_())

加载中…