How to make a widget in the center of the screen in PySide/PyQt?

亡梦爱人 提交于 2019-12-03 11:32:54

问题


This code works, but I wonder if there is any simpler way:

def center(self):
    qr = self.frameGeometry()
    cp = gui.QDesktopWidget().availableGeometry().center()
    qr.moveCenter(cp)
    self.move(qr.topLeft())

回答1:


No, it's the simplest way. Here is a snippet I've used in C++:

  QRect desktopRect = QApplication::desktop()->availableGeometry(this);
  QPoint center = desktopRect.center();

  move(center.x() - width() * 0.5, center.y() - height());



回答2:


just add this line to your main windows :

self.move(QtGui.QApplication.desktop().screen().rect().center()- self.rect().center())



回答3:


self.move(QDesktopWidget().availableGeometry().center() - self.frameGeometry().center())



回答4:


Just another "func-style" example. If you use it several times.

screen_center = lambda widget: QApplication.desktop().screen().rect().center()- widget.rect().center()

And every time in code:

widget.move(screen_center(widget))


来源:https://stackoverflow.com/questions/9357944/how-to-make-a-widget-in-the-center-of-the-screen-in-pyside-pyqt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!