|
发表于 2024-4-16 09:55:59
|
显示全部楼层
很抱歉,我在调用`set_default_message_box_geometry`方法时传入了一个多余的`self`参数。在类内部调用方法时,无需显式传递`self`参数,它会自动传递。请修改代码中的`self.set_default_message_box_geometry(100, 200, 80, 100)`为`set_default_message_box_geometry(100, 200, 80, 100)`。以下是修改后的示例代码:
- from PySide6.QtWidgets import QApplication, QMessageBox, QWidget, QPushButton
- def set_default_message_box_geometry(x, y, width, height):
- """
- 设置所有消息框的默认位置和大小
- :param x: 消息框左上角的 x 坐标
- :param y: 消息框左上角的 y 坐标
- :param width: 消息框的宽度
- :param height: 消息框的高度
- """
- QMessageBox.setDefaultGeometry(x, y, width, height)
- class BaseWindow(QWidget):
- def __init__(self):
- super().__init__()
- 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):
- set_default_message_box_geometry(100, 200, 80, 100)
- reply = QMessageBox.question(
- self, "确认", "是否要退出?",
- QMessageBox.Yes | QMessageBox.No
- )
- if reply == QMessageBox.Yes:
- QApplication.instance().quit()
- if __name__ == "__main__":
- app = QApplication()
- widget = MyWidget()
- app.exec()
复制代码
现在运行代码应该不会再报错了。再次感谢您的指导,如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|