1

我正在尝试在我的主窗口上连接一个按钮以在我单击它时打开一个新的小部件。但是每次我运行程序时都会出现这个错误:

AttributeError:“dataAcquisition”对象没有属性“gpioButton”

如果有人可以提供帮助,我将不胜感激。

这是我的代码:

from PyQt4 import QtCore, QtGui
from mainwindow import Ui_MainWindow
from gpiodialog import Ui_GPIODialog
import sys

class gpioDialog(QtGui.QDialog, Ui_GPIODialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        flags = QtCore.Qt.Drawer | QtCore.Qt.WindowStaysOnTopHint
        self.setWidowFlags(flags)
        self.ui = Ui_GPIODialog
        self.ui.setupUi(self)

        self.gpioOKButton.clicked.connect(self.acceptOKButtonClicked)
    def acceptOKButtonClicked(self):
        self.close()        


class dataAcquisition(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.gpioButton.clicked.connect(self.gpioButton_clicked)
        self.popGPIO = gpioDialog()

    def gpioButton_clicked(self):
        self.popGPIO.show()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    MainWindow = dataAcquisition()
    MainWindow.show()
    sys.exit(app.exec_())

mainwindow.py 代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Tue Oct 18 02:57:57 2016
#      by: PyQt4 UI code generator 4.11.2
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(700, 600)
        MainWindow.setMinimumSize(QtCore.QSize(700, 600))
        MainWindow.setMaximumSize(QtCore.QSize(700, 600))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.gpioButton = QtGui.QToolButton(self.centralwidget)
        self.gpioButton.setGeometry(QtCore.QRect(20, 80, 181, 61))
        self.gpioButton.setObjectName(_fromUtf8("gpioButton"))
        self.procButton = QtGui.QPushButton(self.centralwidget)
        self.procButton.setGeometry(QtCore.QRect(20, 180, 181, 61))
        self.procButton.setObjectName(_fromUtf8("procButton"))
        self.startButton = QtGui.QPushButton(self.centralwidget)
        self.startButton.setEnabled(True)
        self.startButton.setGeometry(QtCore.QRect(20, 360, 131, 51))
        self.startButton.setObjectName(_fromUtf8("startButton"))
        self.stopButton = QtGui.QPushButton(self.centralwidget)
        self.stopButton.setGeometry(QtCore.QRect(190, 360, 131, 51))
        self.stopButton.setObjectName(_fromUtf8("stopButton"))
        self.plotButton = QtGui.QPushButton(self.centralwidget)
        self.plotButton.setGeometry(QtCore.QRect(20, 460, 181, 61))
        self.plotButton.setObjectName(_fromUtf8("plotButton"))
        self.columnView = QtGui.QColumnView(self.centralwidget)
        self.columnView.setGeometry(QtCore.QRect(340, 50, 341, 251))
        self.columnView.setObjectName(_fromUtf8("columnView"))
        self.label_2 = QtGui.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(280, 20, 221, 21))
        font = QtGui.QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.saveButton = QtGui.QPushButton(self.centralwidget)
        self.saveButton.setEnabled(True)
        self.saveButton.setGeometry(QtCore.QRect(410, 420, 211, 31))
        self.saveButton.setObjectName(_fromUtf8("saveButton"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 700, 27))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.gpioButton.setText(_translate("MainWindow", "GPIO Number of inputs", None))
        self.procButton.setText(_translate("MainWindow", "Processing", None))
        self.startButton.setText(_translate("MainWindow", "Start Acquiring", None))
        self.stopButton.setText(_translate("MainWindow", "Stop Acquiring", None))
        self.plotButton.setText(_translate("MainWindow", "Start Live Plot", None))
        self.label_2.setText(_translate("MainWindow", "Data Acquisition System", None))
        self.saveButton.setText(_translate("MainWindow", "Save this session\'s data", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

gpiodialog.py 代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'gpiodialog.ui'
#
# Created: Tue Oct 18 03:11:51 2016
#      by: PyQt4 UI code generator 4.11.2
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_GPIODialog(object):
    def setupUi(self, GPIODialog):
        GPIODialog.setObjectName(_fromUtf8("GPIODialog"))
        GPIODialog.resize(400, 300)
        self.gpioOKButton = QtGui.QPushButton(GPIODialog)
        self.gpioOKButton.setGeometry(QtCore.QRect(170, 240, 101, 31))
        self.gpioOKButton.setObjectName(_fromUtf8("gpioOKButton"))
        self.label = QtGui.QLabel(GPIODialog)
        self.label.setGeometry(QtCore.QRect(90, 20, 191, 21))
        font = QtGui.QFont()
        font.setPointSize(13)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName(_fromUtf8("label"))

        self.retranslateUi(GPIODialog)
        QtCore.QMetaObject.connectSlotsByName(GPIODialog)

    def retranslateUi(self, GPIODialog):
        GPIODialog.setWindowTitle(_translate("GPIODialog", "Dialog", None))
        self.gpioOKButton.setText(_translate("GPIODialog", "OK", None))
        self.label.setText(_translate("GPIODialog", "Choose GPIO input bins", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    GPIODialog = QtGui.QDialog()
    ui = Ui_GPIODialog()
    ui.setupUi(GPIODialog)
    GPIODialog.show()
    sys.exit(app.exec_())
4

1 回答 1

0

您为 ui 创建了一个单独的命名空间,因此您需要像这样访问按钮:

    self.ui.gpioButton.clicked.connect(self.gpioButton_clicked)

但是,如果您希望将所有小部件作为窗口的属性,请执行以下操作:

class gpioDialog(QtGui.QDialog, Ui_GPIODialog):
    def __init__(self, parent=None):
        super(gpioDialog, self).__init__(parent)
        flags = QtCore.Qt.Drawer | QtCore.Qt.WindowStaysOnTopHint
        self.setWindowFlags(flags)
        self.setupUi(self)
        self.gpioOKButton.clicked.connect(self.acceptOKButtonClicked)


class dataAcquisition(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(dataAcquisition, self).__init__(parent)
        self.setupUi(self)
        self.gpioButton.clicked.connect(self.gpioButton_clicked)
        self.popGPIO = gpioDialog()
于 2016-10-20T13:20:12.773 回答