使用 PyQt5 将 window 正确居中到其 parent window 的中心
Centering a window to its parent window's center properly with PyQt5
想到了一个简单的算法,实现了这样:
def parentcenter(self):
self.move(self.parent().frameGeometry().center().x() - self.frameGeometry().width()/2, self.parent().frameGeometry().center().y() - self.frameGeometry().height()/2)
它在纸面上看起来不错,但不能正常工作。 child window 将自己放置在 self.parent().frameGeometry().center()
中。我还尝试通过执行 print(self.frameGeometry().width())
来打印 child window 的宽度和高度,以查看是否有任何错误,确实如此。值 width 和 height 显示的比我预期的要少得多。我的 child window 在填充屏幕的 1/3 时宽度值为 100。我的屏幕分辨率不是 300x300 lol
为什么会这样?或者我仍然可以在不使用上述方法的情况下将我的 child window 居中吗?
感谢three_pineapples,我已经解决了这个问题。我唯一做的就是在 __init__
的 SetupUi
之后调用 parentcenter
。
想到了一个简单的算法,实现了这样:
def parentcenter(self):
self.move(self.parent().frameGeometry().center().x() - self.frameGeometry().width()/2, self.parent().frameGeometry().center().y() - self.frameGeometry().height()/2)
它在纸面上看起来不错,但不能正常工作。 child window 将自己放置在 self.parent().frameGeometry().center()
中。我还尝试通过执行 print(self.frameGeometry().width())
来打印 child window 的宽度和高度,以查看是否有任何错误,确实如此。值 width 和 height 显示的比我预期的要少得多。我的 child window 在填充屏幕的 1/3 时宽度值为 100。我的屏幕分辨率不是 300x300 lol
为什么会这样?或者我仍然可以在不使用上述方法的情况下将我的 child window 居中吗?
感谢three_pineapples,我已经解决了这个问题。我唯一做的就是在 __init__
的 SetupUi
之后调用 parentcenter
。