我正在尝试使用 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'。
我做错了什么?
请您参考如下方法:
我成功地重现了你的问题。您在 QtDesigner 中选择了一个基于对话框的窗体,但正试图在 QMainWindow 中构建它。
UI 代码尝试将其按钮绑定(bind)到默认对话框插槽 accept 和 reject,这在 QMainWindow 中不可用。
来自 ConfigGUI.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 (typicallyQDialog,QWidgetorQMainWindow) is set in Designer. We refer to this type as the Qt base class.
所以,要么选择Designer中的Main Window作为基类,要么将StartQT4的继承改为QtGui。 QDialog.
