【问题标题】:PyQt5 QCombobox: how do I use the selected value of the user to execute a specific function?PyQt5 QCombobox:如何使用用户的选定值来执行特定功能?
【发布时间】:2018-03-25 20:09:07
【问题描述】:

"""我已经定义了某些函数来执行密码加密。虽然加密功能正常,但我无法使用使用 PyQt5 编写的 GUI。更具体地说,我无法执行我的函数基于用户在下拉框/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_())

【问题讨论】:

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


    【解决方案1】:

    添加信号:

     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_())
    

    【讨论】:

      猜你喜欢
      • 2014-05-03
      • 2023-03-08
      • 1970-01-01
      • 2019-05-30
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多