PyQt5相同的按钮,一个起作用一个不起作用

时间:2019-05-06 00:49:03

标签: python python-3.x pyqt5

我尝试使用pyqt5编写一些代码,并具有两个主窗口和按钮,在两种情况下,我都做过相同的事情,但其中一个对clicked.connect起作用,而一个没有。粘贴该按钮的代码无效。关闭第一个窗口后,我叫此窗口

我尝试在类之外创建模块,该模块虽然有效,但是在引用lineEdit对象时却遇到困难

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

app = QtWidgets.QApplication(sys.argv)
PREMIX = QtWidgets.QMainWindow()

logo = QtGui.QIcon()


def init_login():
    ui = Ui_PREMIXlogin()
    ui.setupUi(PREMIX)
    PREMIX.show()
    sys.exit(app.exec_())


def close_login():
    PREMIX.hide()


def init_main():
    ui = Ui_PREMIX()
    ui.setupUi(PREMIX)
    PREMIX.show()


class Ui_PREMIXlogin(object):
    def setupUi(self, PREMIXlogin):
        PREMIXlogin.setObjectName("PREMIXlogin")
        PREMIXlogin.setEnabled(True)
        PREMIXlogin.resize(280, 319)
        sizePolicy = QtWidgets.QSizePolicy(
            QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed
        )
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(
            PREMIXlogin.sizePolicy().hasHeightForWidth()
        )
        PREMIXlogin.setSizePolicy(sizePolicy)
        PREMIXlogin.setMinimumSize(QtCore.QSize(280, 319))
        PREMIXlogin.setMaximumSize(QtCore.QSize(280, 319))
        PREMIXlogin.setMouseTracking(False)
        PREMIXlogin.setWindowIcon(logo)
        PREMIXlogin.setAutoFillBackground(False)
        PREMIXlogin.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.centralwidget = QtWidgets.QWidget(PREMIXlogin)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(36, 190, 61, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(36, 220, 61, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(110, 190, 113, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(110, 220, 113, 20))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit_2.setFont(font)
        self.lineEdit_2.setInputMethodHints(
            QtCore.Qt.ImhHiddenText
            | QtCore.Qt.ImhNoAutoUppercase
            | QtCore.Qt.ImhNoPredictiveText
            | QtCore.Qt.ImhSensitiveData
        )
        self.lineEdit_2.setFrame(True)
        self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(100, 260, 81, 31))
        font = QtGui.QFont()
        font.setPointSize(11)
        font.setBold(False)
        font.setWeight(50)
        self.pushButton.setFont(font)
        self.pushButton.setAutoFillBackground(False)
        self.pushButton.setStyleSheet("background-color: rgb(222, 31, 38);")
        self.pushButton.setObjectName("pushButton")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(30, 20, 231, 151))
        self.frame.setStyleSheet(
            "image: url(:/images/59301307_2353273704903079_3349139235981492224_n.png);"
        )
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        PREMIXlogin.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(PREMIXlogin)
        self.statusbar.setObjectName("statusbar")
        PREMIXlogin.setStatusBar(self.statusbar)

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

    def retranslateUi(self, PREMIXlogin):
        _translate = QtCore.QCoreApplication.translate
        PREMIXlogin.setWindowTitle(_translate("PREMIXlogin", "PREMIX Login"))
        self.label.setText(_translate("PREMIXlogin", "Username"))
        self.label_2.setText(_translate("PREMIXlogin", "Password"))
        self.pushButton.setText(_translate("PREMIXlogin", "Log in"))
        self.pushButton.clicked.connect(self.on_click)

    def on_click(self):
        if True:
            close_login()
            init_main()


class Ui_PREMIX(object):
    def setupUi(self, PREMIX):
        PREMIX.setObjectName("PREMIX")
        PREMIX.resize(473, 179)
        sizePolicy = QtWidgets.QSizePolicy(
            QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed
        )
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(PREMIX.sizePolicy().hasHeightForWidth())
        PREMIX.setSizePolicy(sizePolicy)
        PREMIX.setMinimumSize(QtCore.QSize(473, 179))
        PREMIX.setMaximumSize(QtCore.QSize(473, 179))
        PREMIX.setStyleSheet("background-color: rgb(255, 255, 255);")
        PREMIX.setWindowIcon(logo)
        self.centralwidget = QtWidgets.QWidget(PREMIX)
        self.centralwidget.setObjectName("centralwidget")
        self.Product = QtWidgets.QLabel(self.centralwidget)
        self.Product.setGeometry(QtCore.QRect(30, 20, 61, 20))
        font = QtGui.QFont()
        font.setPointSize(13)
        self.Product.setFont(font)
        self.Product.setObjectName("Product")

        self.Productname = QtWidgets.QLineEdit(self.centralwidget)
        self.Productname.setGeometry(QtCore.QRect(100, 20, 251, 20))
        self.Productname.setObjectName("Productname")

        self.Checkbutton = QtWidgets.QPushButton(self.centralwidget)
        self.Checkbutton.setGeometry(QtCore.QRect(370, 20, 81, 21))
        font = QtGui.QFont()
        font.setPointSize(11)
        font.setBold(False)
        font.setWeight(50)
        self.Checkbutton.setFont(font)
        self.Checkbutton.setAutoFillBackground(False)
        self.Checkbutton.setStyleSheet("background-color: rgb(222, 31, 38);")
        self.Checkbutton.setObjectName("Checkbutton")
        self.logo = QtWidgets.QFrame(self.centralwidget)
        self.logo.setGeometry(QtCore.QRect(359, 60, 101, 80))
        self.logo.setStyleSheet(
            "image: url(:/images/59301307_2353273704903079_3349139235981492224_n.png);"
        )
        self.logo.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.logo.setFrameShadow(QtWidgets.QFrame.Raised)
        self.logo.setObjectName("logo")

        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(180, 80, 91, 31))
        self.lineEdit.setMinimumSize(QtCore.QSize(89, 31))
        self.lineEdit.setMaximumSize(QtCore.QSize(91, 31))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.setReadOnly(True)

        PREMIX.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(PREMIX)
        self.statusbar.setObjectName("statusbar")
        PREMIX.setStatusBar(self.statusbar)
        self.retranslateUi(PREMIX)
        QtCore.QMetaObject.connectSlotsByName(PREMIX)

    def retranslateUi(self, PREMIX):
        _translate = QtCore.QCoreApplication.translate
        PREMIX.setWindowTitle(_translate("PREMIX", "PREMIX"))
        self.Product.setText(_translate("PREMIX", "Product"))
        self.Checkbutton.setText(_translate("PREMIX", "Check"))
        self.Checkbutton.clicked.connect(self.on_click)

    def on_click(self):
        print("aaaaaa")
        product = self.Productname.text()
        print(product)


def main():
    init_login()


if __name__ == "__main__":
    main()

我希望点击事件上的Checkbutton起作用,请帮助

1 个答案:

答案 0 :(得分:0)

对于Ui_Premix,使用self.Checkbutton.clicked.connect(Ui_PREMIX.on_click)代替 self.on_click即使on_click事件在类内部,也可以正常工作