PyQt5 QCombobox: how do I use the selected value of the user to execute a specific function?

时间:2018-03-25 20:09:08

标签: python python-3.x combobox pyqt5 qcombobox

"""I have defined certain functions to carry out password encryption. Though the encryptions are functioning properly, I'm unable to use the GUI, written using PyQt5. To be more specific, I'm unable to execute my functions based on the type of encryption selected by the user in the drop-down box/QCombobox. """

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'Password encryption'
        self.left = 10
        self.top = 10
        self.width = 400
        self.height = 200
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        # Create textbox
        self.textbox = QLineEdit(self)
        self.textbox.move(30, 35)
        self.textbox.resize(350, 30)

        # Create a button in the window
        drop = QComboBox(self)
        drop.addItem("None")
        drop.addItem("modlensq")
        drop.addItem("fishcrypt")
        drop.addItem("bitman")
        drop.addItem("caesarcipher")
        drop.addItem("encryptcipher2")
        drop.addItem("blowfishencryption")
        drop.addItem("AES")
        drop.move(45, 90)

        inp = QLabel(self)
        inp.setText("Enter the password:")
        inp.move(5, 5)

        # Create a drop down list to select in the window
        self.button = QPushButton('Encrypt', self)
        self.button.move(135, 150)

        # connect button to function on_click
        self.button.clicked.connect(self.on_click)
        self.show()


    @pyqtSlot()

    def on_click(self):
        textboxValue = self.textbox.text()

        QMessageBox.question(self, 'password after encryption', "encrypted form: " + textboxValue , QMessageBox.Ok,
                             QMessageBox.Ok)
        self.textbox.setText("")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

添加信号:

 drop.activated[str].connect(self.onActivated)

http://doc.qt.io/qt-5/qcombobox.html#activated-1 当用户选择组合框中的项目时,将发送此信号。

试一试:

from PyQt5 import Qt

class App(Qt.QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'Password encryption'
        self.left = 500
        self.top = 100
        self.width = 400
        self.height = 200
        self.comboText = None                                       # +++
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        # Create textbox
        self.textbox = Qt.QLineEdit(self)
        self.textbox.move(30, 35)
        self.textbox.resize(350, 30)

        # Create a button in the window
        drop = Qt.QComboBox(self)
        drop.addItem("None")
        drop.addItem("modlensq")
        drop.addItem("fishcrypt")
        drop.addItem("bitman")
        drop.addItem("caesarcipher")
        drop.addItem("encryptcipher2")
        drop.addItem("blowfishencryption")
        drop.addItem("AES")
        drop.move(45, 90)
        drop.activated[str].connect(self.onActivated)                  # +++

        inp = Qt.QLabel(self)
        inp.setText("Enter the password:")
        inp.move(5, 5)

        # Create a drop down list to select in the window
        self.button = Qt.QPushButton('Encrypt', self)
        self.button.move(135, 150)

        # connect button to function on_click
        self.button.clicked.connect(self.on_click)
        self.show()


    @Qt.pyqtSlot()
    def on_click(self):
        textboxValue = self.textbox.text()

        Qt.QMessageBox.question(self, 'password after encryption', 
                                "encrypted form: {}, <br>ComboBoxItem: {} "\
                                .format(textboxValue, self.comboText) ,        # +++
                                Qt.QMessageBox.Ok, Qt.QMessageBox.Ok)
        self.textbox.setText("")

    # +++     
    def onActivated(self, text):
        self.comboText = text        

if __name__ == '__main__':
    import sys
    app = Qt.QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

enter image description here