安装pyqt5:pip install PyQt5
安装qt designer : pip install PyQt5-tools
在setting>tools>extrnal tools里,点击加号添加外部工具
name设置为py designer,也可自定义。
program路径设置为designer.exe路径,我的是D:\ProgramsForWork\anaconda\Ana\Lib\site-packages\pyqt5_tools\designer.exe。
working directory设置为项目目录,填$ProjectFileDir$。
uic设置方法类似,program填pyuic5.exe的路径,我的是D:\ProgramsForWork\anaconda\Ana\Scripts\pyuic5.exe,
arguments填$FileName$ -o $FileNameWithoutExtension$.py,
working directory填$FileDir$。
设置成功将能在tools>external tools>下看到qt designer和pyuic这两个工具选项
依次点击tools>external tools>qt designer打开designer,
创建一个mainwindow主窗口界面,从左侧选择各种组件添加到窗口中。下面所示图标可以添加信号和槽
保存到项目目录下,命名name.ui,在pycharm里可以看到项目下多了name.ui这个文件
右键该name.ui文件,从external tools选项选择pyuic
经uic处理生成name.py文件
该name.py即刚才设计的界面文件,工具以经帮我们转换成代码了,我们只需做些适当添加,比如给button添加clicked信号和槽函数on_button_clicked()。
self.push_button_1.clicked.connect(self.on_push_button_1_clicked)
self.push_button_2.clicked.connect(self.on_push_button_2_clicked)
self.push_button_3.clicked.connect(self.on_push_button_3_clicked)
def on_push_button_1_clicked(self):
...
def on_push_button_2_clicked(self):
...
可以看到其格式为self.button.clicked.connect(on_button_clicked)
在init函数里面修改背景颜色 ,也可以在qt designer里面手动改
def __init__(self, parent=None):
self.setStyleSheet("#MainWindow{background-color: pink}")
安装cryptodome : pip install cryptodome
# aes.py
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
class PrpCrypt(object):
def __init__(self, key):
self.key = key.encode('utf-8')
self.mode = AES.MODE_CBC
# 加密函数,如果text不足16位就用空格补足为16位,
# 如果大于16当时不是16的倍数,那就补足为16的倍数。
def encrypt(self, text):
text = text.encode('utf-8')
cryptor = AES.new(self.key, self.mode, b'0000000000000000')
# 这里密钥key 长度必须为16(AES-128),
# 24(AES-192),或者32 (AES-256)Bytes 长度
# 目前AES-128 足够目前使用
length = 16
count = len(text)
if count < length:
add = (length - count)
# \0 backspace
# text = text + ('\0' * add)
text = text + ('\0' * add).encode('utf-8')
elif count > length:
add = (length - (count % length))
# text = text + ('\0' * add)
text = text + ('\0' * add).encode('utf-8')
self.ciphertext = cryptor.encrypt(text)
# 因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题
# 所以这里统一把加密后的字符串转化为16进制字符串
return b2a_hex(self.ciphertext)
# 解密后,去掉补足的空格用strip() 去掉
def decrypt(self, text):
cryptor = AES.new(self.key, self.mode, b'0000000000000000')
plain_text = cryptor.decrypt(a2b_hex(text))
# return plain_text.rstrip('\0')
return bytes.decode(plain_text).rstrip('\0')
if __name__ == '__main__':
key = PrpCrypt('ilikechinesebest') # 初始化密钥
e = key.encrypt("i am a student,you are a pig") # 加密
d = key.decrypt(e) # 解密
print("加密:", e)
print("解密:", d)
如果本文件名==执行文件名,就执行以下代码(一般是测试代码),方便调试。而如果本文件作为模块被执行文件调用,执行文件中不会运行下面代码。
# gui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'encrypt.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
#
# Writen By Mr. REDHAT From HUST
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import aes
class UiMainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(UiMainWindow, self).__init__(parent)
self.setObjectName("MainWindow")
self.resize(800, 600)
self.setStyleSheet("#MainWindow{background-color: pink}")
font = QtGui.QFont()
font.setPointSize(16)
self.setFont(font)
self.central_widget = QtWidgets.QWidget(self)
self.central_widget.setObjectName("central_widget")
self.label = QtWidgets.QLabel(self.central_widget)
self.label.setGeometry(QtCore.QRect(20, 90, 111, 31))
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.central_widget)
self.label_2.setGeometry(QtCore.QRect(23, 170, 101, 31))
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.central_widget)
self.label_3.setGeometry(QtCore.QRect(20, 270, 111, 31))
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.central_widget)
self.label_4.setGeometry(QtCore.QRect(20, 390, 111, 31))
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.input_text = QtWidgets.QPlainTextEdit(self.central_widget)
self.input_text.setGeometry(QtCore.QRect(130, 60, 500, 111))
self.input_text.setObjectName("input_text")
self.key_text = QtWidgets.QPlainTextEdit(self.central_widget)
self.key_text.setGeometry(QtCore.QRect(130, 175, 500, 31))
self.key_text.setObjectName("key_text")
self.encrypted_text = QtWidgets.QPlainTextEdit(self.central_widget)
self.encrypted_text.setGeometry(QtCore.QRect(130, 220, 500, 111))
self.encrypted_text.setObjectName("encrypted_text")
self.decrypted_text = QtWidgets.QPlainTextEdit(self.central_widget)
self.decrypted_text.setGeometry(QtCore.QRect(130, 350, 500, 111))
self.decrypted_text.setObjectName("decrypted_text")
self.push_button_1 = QtWidgets.QPushButton(self.central_widget)
self.push_button_1.setGeometry(QtCore.QRect(660, 175, 81, 31))
self.push_button_1.setFont(font)
self.push_button_1.setObjectName("pushButton_1")
self.push_button_2 = QtWidgets.QPushButton(self.central_widget)
self.push_button_2.setGeometry(QtCore.QRect(660, 262, 81, 31))
self.push_button_2.setFont(font)
self.push_button_2.setObjectName("pushButton_2")
self.push_button_3 = QtWidgets.QPushButton(self.central_widget)
self.push_button_3.setGeometry(QtCore.QRect(660, 390, 81, 31))
self.push_button_3.setFont(font)
self.push_button_3.setObjectName("pushButton_3")
self.setCentralWidget(self.central_widget)
self.menu_bar = QtWidgets.QMenuBar(self)
self.menu_bar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menu_bar.setObjectName("menu_bar")
self.setMenuBar(self.menu_bar)
self.status_bar = QtWidgets.QStatusBar(self)
self.status_bar.setObjectName("status_bar")
self.setStatusBar(self.status_bar)
QtCore.QMetaObject.connectSlotsByName(self)
_translate = QtCore.QCoreApplication.translate
self.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "input:"))
self.label_2.setText(_translate("MainWindow", "key:"))
self.label_3.setText(_translate("MainWindow", "encrypted:"))
self.label_4.setText(_translate("MainWindow", "decrypted:"))
self.push_button_1.setText(_translate("MainWindow", "encrypt"))
self.push_button_2.setText(_translate("MainWindow", "decrypt"))
self.push_button_3.setText(_translate("MainWindow", "quit"))
self.push_button_1.clicked.connect(self.on_push_button_1_clicked)
self.push_button_2.clicked.connect(self.on_push_button_2_clicked)
self.push_button_3.clicked.connect(self.on_push_button_3_clicked)
def on_push_button_1_clicked(self):
if str(self.input_text.toPlainText()) and len(str(self.key_text.toPlainText())) == 16:
key = aes.PrpCrypt(str(self.key_text.toPlainText())) # 初始化密钥
e = key.encrypt(str(self.input_text.toPlainText())) # 加密
self.encrypted_text.setPlainText(bytes.decode(e)) # 字节码e转utf-8
print('encrypted!')
elif len(str(self.key_text.toPlainText())) == 16:
QtWidgets.QMessageBox.warning(self, "Warning!", 'Please input your text to encrypt!')
elif str(self.key_text.toPlainText()) and str(self.input_text.toPlainText()):
QtWidgets.QMessageBox.warning(self, "Warning!", 'The key must be 16bits!')
elif str(self.input_text.toPlainText()):
QtWidgets.QMessageBox.warning(self, "Warning!", 'Please enter your key!')
else:
QtWidgets.QMessageBox.warning(self, "Warning!", 'Please check the input_text and the key!')
def on_push_button_2_clicked(self):
if len(str(self.encrypted_text.toPlainText())) % 32 == 0 and len(str(self.key_text.toPlainText())) == 16:
key = aes.PrpCrypt(str(self.key_text.toPlainText()))
try:
d = key.decrypt(str(self.encrypted_text.toPlainText())) # 解码
except Exception as e:
print(e)
QtWidgets.QMessageBox.warning(self, "Warning!", 'Appcrash happened! Please check the encrypted and key')
# try语句转为异常处理防止程序崩溃
else:
self.decrypted_text.setPlainText(d)
else:
QtWidgets.QMessageBox.warning(self, "Warning!", 'Incorrect key or encrypted text!')
def on_push_button_3_clicked(self):
sys.exit()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = UiMainWindow()
window.show()
sys.exit(app.exec_())
注意Qt里面输入框内容text类型为QString,用.toPlainText()方法获得其text内容后用str()转换类型为string。 当Appcrash发生即程序崩溃时,改为try语句格式转为异常处理防止程序崩溃。