在PyQt5中,要将EditorWindow设置为屏幕中心,您可以使用setGeometry()
方法结合QApplication.desktop()
来计算屏幕中心位置,并将窗口的位置设置为该中心点。以下是一个简单的示例代码:
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import Qt
class CenteredWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 计算屏幕分辨率
screen = QApplication.desktop().screenGeometry()
# 计算屏幕中心位置
center_point = screen.center()
# 设置窗口大小、位置使其居中
self.setGeometry(int((screen.width() - self.width()) / 2),
int((screen.height() - self.height()) / 2),
self.width(), self.height())
if __name__ == '__main__':
app = QApplication([])
window = CenteredWindow()
window.show()
app.exec_()
在这个示例中,我们首先导入了必要的模块,然后创建了一个继承自QMainWindow
的CenteredWindow
类。在initUI
方法中,我们计算了屏幕的中心点,并使用setGeometry()
方法将窗口的位置设置为屏幕中心。
领取专属 10元无门槛券
手把手带您无忧上云