Python PyQt5 - 按下按钮时无法获取setPlaintext()/ toPlainText() - 导致崩溃

时间:2015-08-04 17:32:16

标签: python qt

我在PyQt 5中设计了一个用于密码的GUI,我现在正在尝试集成代码。我在加密和解密正在显示的消息时遇到问题。

我希望它获取一个文本框的内容并通过加密/解密运行它,然后将其显示在另一个文本框中。当我按下任一按钮时它只是崩溃了#34; PythonW.exe没有响应"错误。

from PyQt5 import QtCore, QtGui, QtWidgets
import sys, random
import cipher ## Designed GUI

LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

class CipherDesign(QtWidgets.QMainWindow, cipher.Ui_MainWindow):
    def __init__(self, parent=None):
        super(CipherDesign, self).__init__(parent)
        self.setupUi(self)
        self.BGenerate.clicked.connect(self.generatekey) # Generate Button
        self.BSubmit.clicked.connect(self.submitkey) # Submit Button
        self.BEncrypt.clicked.connect(self.encrypt) # Encrypt Button
        self.BDecrypt.clicked.connect(self.decrypt) # Decrypt Button

    def generatekey(self):
        key = list(LETTERS)
        random.shuffle(key)
        self.keytext.setText(''.join(key))
        print (key) ##### DELETE ####

    def submitkey(self):
        submittedkey = self.keytext_2.text()
        mykey = submittedkey
        print (mykey) #### DELETE ####

    def encrypt(self, mykey, translated):
        return translatedMessage(key, translated, 'encrypt')

    def decrypt(self, mykey, translated):
        return translatedMessage(key, translated, 'decrypt')

    def translatedMessage(self, key, translated, mode):
        message = self.textencrypt.setPlainText()
        translated = ''
        self.textdecrypt.toPlainText(''.join(translated))
        charsA = LETTERS
        charsB = mykey

        if mode == 'decrypt':
            charsA, charsB = charsB, charsA

        for symbol in message:
            if symbol.upper() in charsA:
                symIndex = charsA.find(symbol.upper())
                if symbol.isupper():
                    translated += charsB[symIndex].upper()
                else:
                    translated += charsB[symIndex].lower()
            else:
                translated += symbol
        return translated

def main():
    app = QtWidgets.QApplication(sys.argv)
    form = CipherDesign()
    form.show()
    app.exec_()





if __name__ == "__main__":
    main()

不确定究竟是什么错误。

python.cy代码

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

# Form implementation generated from reading ui file 'cipher.ui'
#
# Created by: PyQt5 UI code generator 5.5
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(473, 288)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        # Encrypt Textbox
        self.textencrypt = QtWidgets.QPlainTextEdit(self.centralwidget)
        self.textencrypt.setGeometry(QtCore.QRect(10, 100, 221, 171))
        self.textencrypt.setObjectName("textencrypt")

        # Decrypt Textbox
        self.textdecrypt = QtWidgets.QPlainTextEdit(self.centralwidget)
        self.textdecrypt.setGeometry(QtCore.QRect(240, 100, 221, 171))
        self.textdecrypt.setObjectName("textdecrypt")

        # Encrypt Button
        self.BEncrypt = QtWidgets.QPushButton(self.centralwidget)
        self.BEncrypt.setGeometry(QtCore.QRect(10, 70, 75, 23))
        self.BEncrypt.setObjectName("BEncrypt")

        # Decrypt Button
        self.BDecrypt = QtWidgets.QPushButton(self.centralwidget)
        self.BDecrypt.setGeometry(QtCore.QRect(90, 70, 75, 23))
        self.BDecrypt.setObjectName("BDecrypt")

        # Generate Key Textbox
        self.keytext = QtWidgets.QLineEdit(self.centralwidget)
        self.keytext.setGeometry(QtCore.QRect(10, 10, 271, 20))
        self.keytext.setInputMask("")
        self.keytext.setText("")
        self.keytext.setMaxLength(32767)
        self.keytext.setObjectName("keytext")

        # Generate Key Button
        self.BGenerate = QtWidgets.QPushButton(self.centralwidget)
        self.BGenerate.setGeometry(QtCore.QRect(290, 10, 75, 23))
        self.BGenerate.setObjectName("BGenerate")

        # Submit Key Textbox
        self.keytext_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.keytext_2.setGeometry(QtCore.QRect(10, 40, 271, 20))
        self.keytext_2.setMaxLength(26)
        self.keytext_2.setObjectName("keytext_2")

        # Submit Key Button
        self.BSubmit = QtWidgets.QPushButton(self.centralwidget)
        self.BSubmit.setGeometry(QtCore.QRect(290, 40, 75, 23))
        self.BSubmit.setObjectName("BSubmit")
        MainWindow.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Cipher"))
        self.BEncrypt.setText(_translate("MainWindow", "Encrypt"))
        self.BDecrypt.setText(_translate("MainWindow", "Decrypt"))
        self.BGenerate.setText(_translate("MainWindow", "Generate Key"))
        self.BSubmit.setText(_translate("MainWindow", "Submit Key"))

0 个答案:

没有答案