You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使PyQt5中QDialog在主窗口中心位置弹出?

让QMessageBox在PyQt5主窗口中心显示的解决方案

嘿,我看你现在遇到的问题是QMessageBox总是弹在主窗口左上角,对吧?其实问题出在你用self.pos()获取的是主窗口的左上角坐标,直接move过去当然就对齐左上角了。这里有两个简单的解决办法:

方法一:给对话框设置父窗口(最简便)

只需要在创建QMessageBox的时候把主窗口self作为参数传进去,Qt会自动处理居中逻辑,完全不用手动计算位置:

def open_box(self):
    # 传入self作为父窗口
    msgb = qtw.QMessageBox(self)
    msgb.setWindowTitle("title")
    msgb.setText("hier sthet was")
    # 不需要额外的move语句!
    run = msgb.exec_()

方法二:手动计算居中位置

如果不想设置父窗口,也可以沿用你之前让主窗口居中的思路,手动计算对话框的居中位置:

def open_box(self):
    msgb = qtw.QMessageBox()
    msgb.setWindowTitle("title")
    msgb.setText("hier sthet was")
    
    # 获取主窗口的中心坐标
    main_window_center = self.geometry().center()
    # 获取对话框的框架几何(包含窗口边框)
    msgb_geometry = msgb.frameGeometry()
    # 将对话框的中心对齐到主窗口的中心
    msgb_geometry.moveCenter(main_window_center)
    # 移动对话框到计算好的左上角位置
    msgb.move(msgb_geometry.topLeft())
    
    run = msgb.exec_()

为什么原来的代码不生效?

self.pos()返回的是主窗口左上角的屏幕坐标,你直接把对话框的左上角移到这个点,自然就和主窗口左上角重合了。而我们需要的是让对话框的中心和主窗口的中心对齐,所以得用moveCenter方法来调整对话框的位置,就像你初始化主窗口时做的那样。


修改后的完整代码

#!/usr/bin/env python
""" startscreen base window remit to specific tests """
import os
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc

class Startscreen(qtw.QWidget):
    ''' remit to one of three tests if widgets toggled/clicked hide its self after '''
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # position
        qtRectangle = self.frameGeometry()
        centerPoint = qtw.QDesktopWidget().availableGeometry().center()
        qtRectangle.moveCenter(centerPoint)
        self.move(qtRectangle.topLeft())
        # size
        self.resize(700, 410)
        # frame title
        self.setWindowTitle("Lambda")
        # heading
        heading_label = qtw.QLabel("Lambda Version 1.0")
        heading_label.setAlignment(qtc.Qt.AlignHCenter)
        # active user
        activeuser_label = qtw.QLabel('Benutzer: ' + os.getlogin())
        activeuser_label.setStyleSheet("background-color: rgb(234, 246, 22)")
        activeuser_label.setAlignment(qtc.Qt.AlignRight | qtc.Qt.AlignTop)
        # groubox for widget positioning
        self.groupbox = qtw.QGroupBox(self)
        # layout and widgets
        vlayout = qtw.QVBoxLayout()
        vlayout.setAlignment(qtc.Qt.AlignHCenter)
        self.particlesize_radiobutton = qtw.QRadioButton("test1")
        vlayout.addWidget(self.particlesize_radiobutton)
        self.dimensionalchange_radiobutton = qtw.QRadioButton("test2")
        vlayout.addWidget(self.dimensionalchange_radiobutton)
        self.dimensionalchangecook_radiobutton = qtw.QRadioButton("test3")
        vlayout.addWidget(self.dimensionalchangecook_radiobutton)
        self.select_button = qtw.QPushButton('select')
        vlayout.addWidget(self.select_button)
        self.groupbox.setLayout(vlayout)
        # mainlayout
        main_layout = qtw.QFormLayout()
        main_layout.addRow(activeuser_label)
        main_layout.addRow(heading_label)
        main_layout.setVerticalSpacing(40)
        main_layout.addRow(self.groupbox)
        self.setLayout(main_layout)
        # functionality
        self.select_button.clicked.connect(self.open_box)
        self.show()

    def open_box(self):
        # 方法一:设置父窗口的写法(推荐)
        msgb = qtw.QMessageBox(self)
        msgb.setWindowTitle("title")
        msgb.setText("hier sthet was")
        run = msgb.exec_()

        # 如果你想用方法二,替换上面的代码为:
        # msgb = qtw.QMessageBox()
        # msgb.setWindowTitle("title")
        # msgb.setText("hier sthet was")
        # main_window_center = self.geometry().center()
        # msgb_geometry = msgb.frameGeometry()
        # msgb_geometry.moveCenter(main_window_center)
        # msgb.move(msgb_geometry.topLeft())
        # run = msgb.exec_()

if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    w = Startscreen()
    sys.exit(app.exec_())

内容的提问来源于stack exchange,提问作者Sator

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 00:47:32