使用pyqt5,我无法在任何小部件上附加功能,这是codeself.toolButton1.clicked()

时间:2019-02-01 08:25:26

标签: python pyqt5

我正在使用PYQT5制作GUI。我无法将功能附加到任何小部件。在第11行中,在“ self.toolButton1.clicked()”之后找不到“ connect”方法。 使用最新版本的Pyqt 请帮助

from UI_Scripts import QT_test1
from PyQt5 import QtGui, QtCore, QtWidgets

      class MyQtApp(QT_test1.Ui_MainWindow, QtWidgets.QMainWindow):
        def __init__(self):
            super(MyQtApp, self).__init__()
            self.setupUi(self)
            self.showMaximized()
            self.setWindowTitle("Subhanshu")
            self.toolButton1.clicked().?

        def print_n(self):
            print('Subhanshu')

    if __name__ == '__main__':
        import sys

        app = QtWidgets.QApplication(sys.argv)
        qt_app = MyQtApp()
        qt_app.show()
        sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

尝试一下:

main.py

#from UI_Scripts import QT_test1
from UI_Scripts.QT_test1 import Ui_MainWindow
from PyQt5 import QtGui, QtCore, QtWidgets

#class MyQtApp(QT_test1.Ui_MainWindow, QtWidgets.QMainWindow):
class MyQtApp(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MyQtApp, self).__init__()
        self.setupUi(self)
        self.showMaximized()
        self.setWindowTitle("Subhanshu")

#       self.toolButton1.clicked().?
        self.toolButton1.clicked.connect(self.print_n)    

    def print_n(self):
        print('Subhanshu: txt_user->{}, txt_pass->{}'
              ''.format(self.txt_user.text(), self.txt_pass.text()))

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    qt_app = MyQtApp()
    qt_app.show()
    sys.exit(app.exec_())

UI_Scripts.QT_test1.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(600, 350)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.txt_user = QtWidgets.QLineEdit(self.centralwidget)
        self.txt_user.setGeometry(QtCore.QRect(320, 130, 120, 20))
        self.txt_pass = QtWidgets.QLineEdit(self.centralwidget)
        self.txt_pass.setGeometry(QtCore.QRect(320, 170, 120, 20))

        self.toolButton1=QtWidgets.QToolButton(self.centralwidget)
        self.toolButton1.setIcon(QtGui.QIcon('D:/_Qt/img/py-qt.png'))
        self.toolButton1.setAutoRaise(True)
        self.toolButton1.setToolTip("ToolButton")
        self.toolButton1.setIconSize(QtCore.QSize(70, 70))
        self.toolButton1.setGeometry(QtCore.QRect(320, 200, 120, 120))

        MainWindow.setCentralWidget(self.centralwidget)  

        # ...

enter image description here