【问题标题】:AttributeError: 'bool' object has no attribute 'accept'AttributeError:“布尔”对象没有“接受”属性
【发布时间】:2015-11-21 21:29:42
【问题描述】:

我是使用 python 和 PyQt 的新手,我正在为我的大学制作一个 PyQt 项目。我正在写这段代码:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import uic

class Ventana(QMainWindow):
    def __init__(self):
        super(QMainWindow, self).__init__()
        uic.loadUi("MainVentanaNew.ui", self)
        self.showMaximized()
        self.setMinimumSize(250,200)

#Menubar elements
        self.actionNuevo.setShortcut("Ctrl+n")
        self.actionNuevo.setStatusTip("Crea un nuevo archivo")

        self.actionSalir.setShortcut("Alt+F4")
        self.actionSalir.setStatusTip("Cierra la aplicación")
        self.actionSalir.triggered.connect(self.closeEvent)

    def closeEvent(self, event):
        self.statusBar().showMessage("Saliendo de la aplicación")
        preguntar = QMessageBox.question(self, "Salir...", 
        "¿Seguro desea salir", QMessageBox.Yes | QMessageBox.No)
        if preguntar == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

app = QApplication(sys.argv)
window = Ventana()
window.show()
sys.exit(app.exec_())

我的代码中的缩进是正确的,也许我在这里复制它时犯了一些错误,但没关系。所以,我的问题是,当我构建它时,它会抛出这个错误:

"AttributeError: 'bool' 对象没有属性'accept'",

当我想使用菜单栏的元素“self.actionSalir”关闭主窗口时。但是,当我以任何其他方式关闭它时,它可以工作。

我该怎么做才能做到这一点?我的意思是,使用菜单栏的元素关闭主窗口?

感谢您的宝贵时间和您的回答。

【问题讨论】:

  • self.actionSalir.triggered.connect(self.close).
  • @ekhumoro:你为什么不把这个放在答案中?这样,问题就可以被标记为已回答,并以这种方式显示在概览中。我知道这对你来说可能是一个小评论,但它仍然是一个正确的答案。
  • @titusjan。这是一个不太可能帮助其他用户的小修复 - 问题可能应该被关闭(没有双关语)。
  • @ekhumoro 很抱歉不同意,但是像这样的“小修复”可以帮助像我一样从 python 和/或 pyqt 开始的用户,或者其他看不到解决方案的用户,因为我不知道不知道,他们的代码太长,很难看。不要误解我,作为一个感恩的用户,我以最好的方式告诉你这一点。再次感谢您的回答,对我帮助很大。

标签: python-2.7 pyqt4 attributeerror mainwindow


【解决方案1】:
self.actionSalir.triggered.connect(self.close)

对我来说效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2012-12-01
    • 2021-06-15
    • 2021-04-19
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多