AttributeError:“StartQT4”对象没有属性“accept”

2024-10-01 17:21:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用QT4和Python编写一个快速对话程序。 我使用pyuic4生成了Python类,并尝试创建一个小Python脚本来启动它:

import sys
from PyQt4 import QtCore, QtGui
from ConfigGUI import Ui_ConfigGUI

class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_ConfigGUI()
        self.ui.setupUi(self)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())

当我试图运行它时,它显示AttributeError: 'StartQT4' object has no attribute 'accept'。在

我做错什么了?在


Tags: fromimportselfappuiinitsysmyapp
1条回答
网友
1楼 · 发布于 2024-10-01 17:21:28

我设法重现了你的问题。您基于QtDesigner中的对话框选择了一个窗体,但正试图在QMainWindow中构造它。在

Form base selection dialog in QtDesigner

UI代码尝试将其按钮绑定到默认对话框槽accept和{},这两个窗口在QMainWindow中不可用。在

来自配置GUI.py:

QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)

The class contains a method called setupUi(). This takes a single argument which is the widget in which the user interface is created. The type of this argument (typically QDialog, QWidget or QMainWindow) is set in Designer. We refer to this type as the Qt base class.

http://pyqt.sourceforge.net/Docs/PyQt4/designer.html

因此,要么在设计器中选择mainwindow作为基类,要么将StartQT4的继承改为QtGui.QDialog。在

相关问题 更多 >

    热门问题