How to make a widget in the center of the screen i

2020-05-23 03:16发布

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())

4条回答
我只想做你的唯一
2楼-- · 2020-05-23 03:44

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());
查看更多
beautiful°
3楼-- · 2020-05-23 03:56
self.move(QDesktopWidget().availableGeometry().center() - self.frameGeometry().center())
查看更多
ら.Afraid
4楼-- · 2020-05-23 04:02

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))
查看更多
贼婆χ
5楼-- · 2020-05-23 04:05

just add this line to your main windows :

self.move(QtGui.QApplication.desktop().screen().rect().center()- self.rect().center())
查看更多
登录 后发表回答