|
发表于 2024-4-16 10:21:17
|
显示全部楼层
非常抱歉,之前的回答是错误的。设置消息框的位置和大小应该使用`setGeometry`方法而不是`setFixedWidth`、`setFixedHeight`和`move`方法。以下是修改后的代码:
- from PySide6.QtWidgets import QApplication, QMessageBox, QWidget, QPushButton
- class BaseWindow(QWidget):
- def __init__(self):
- super().__init__()
- def set_default_message_box_geometry(self, msg_box, x, y, width, height):
- """
- 设置消息框的位置和大小
- :param msg_box: 消息框对象
- :param x: 消息框左上角的 x 坐标
- :param y: 消息框左上角的 y 坐标
- :param width: 消息框的宽度
- :param height: 消息框的高度
- """
- msg_box.setGeometry(x, y, width, height)
- class MyWidget(BaseWindow):
- def __init__(self):
- super().__init__()
- self.initUI()
- def initUI(self):
- self.exit_button = QPushButton("退出", self)
- self.exit_button.clicked.connect(self.confirm_exit)
- self.show()
- def confirm_exit(self):
- msg_box = QMessageBox(self)
- self.set_default_message_box_geometry(msg_box, 100, 200, 200, 150)
- reply = msg_box.question(
- self, "确认", "是否要退出?",
- QMessageBox.Yes | QMessageBox.No
- )
- if reply == QMessageBox.Yes:
- QApplication.instance().quit()
- if __name__ == "__main__":
- app = QApplication()
- widget = MyWidget()
- app.exec()
复制代码
将消息框的位置和大小设置为`(x, y, width, height)` 使用 `setGeometry` 方法即可。现在应该可以正确地改变消息框的大小和位置了。如果还有其他问题,请随时告诉我。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|