如题,我使用pyqt5写一个计算哈希的工具,有多button_1 ,button_2 ...。
每个button的文本为对应的哈希算法如SHA1,SHA2...,我希望每点击一个按钮时获取其文本,将字符传给我定义的哈希函数 def hash(self) 中的变量 hashtype。请问怎样才能用一个函数实现这个功能,而不是在slot函数中全部去绑定,然后定义每一个button?按钮少的话还可以,假设有100个肯定不能这样。目前我使用了一个sender(),但是报错了,是哪里不对?有其他方法解决这个问题吗?谢谢。
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
import binascii
import hashlib
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(435, 316)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setGeometry(QtCore.QRect(20, 10, 401, 121))
self.plainTextEdit.setObjectName("plainTextEdit")
self.pushButton_1 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_1.setGeometry(QtCore.QRect(20, 140, 75, 23))
self.pushButton_1.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(100, 140, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.plainTextEdit_2 = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit_2.setGeometry(QtCore.QRect(20, 170, 401, 101))
self.plainTextEdit_2.setObjectName("plainTextEdit_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 435, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_1.setText(_translate("MainWindow", "SHA1"))
self.pushButton_2.setText(_translate("MainWindow", "SHA224"))
class TestGUI(Ui_MainWindow):
def __init__(self, MainWindow):
self.setupUi(MainWindow)
self.connect_slot()
def hash(self):
hashdata = self.plainTextEdit.toPlainText()
hashtype = self.sender().text()
myHash = hashlib.new(hashtype)
myHash.update(binascii.a2b_hex(hashdata))
self.plainTextEdit_2.setPlainText(myHash.hexdigest().upper())
def connect_slot(self):
"""连接各控件的槽函数"""
self.pushButton.clicked.connect(self.hash)
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = QMainWindow()
ui = TestGUI(mainWindow)
mainWindow.show()
sys.exit(app.exec_())
def hash(self):
src = self.plainTextEdit.toPlainText()
hashtype = self.pushButton_1.text()
myHash = hashlib.new(hashtype)
myHash.update(binascii.a2b_hex(src))
self.plainTextEdit_2.setPlainText(myHash.hexdigest().upper())
#处代码异常改为
import traceback #捕获异常模块
def hash(self):
try:
src = self.plainTextEdit.toPlainText()
print("src",src)
hashtype = self.pushButton_1.text()
myHash = hashlib.new(hashtype)
myHash.update(src.encode())
self.plainTextEdit_2.setPlainText(myHash.hexdigest().upper())
except:
print(str(traceback.format_exc()))
我修改了下问题,可能之前没描述清楚。这个hashtype这个变量,我是想每点击一个button时,如何实现获取button的文本,传给hashtype。我之前写的hashtype = self.pushButton_1.text(),只是获取一个按钮的值。实际这句得删除,因为我有很多按钮。现在我用了一个sender().text(),获取每次点击button的值,但是没有奏效。请问这里该怎样是实现呢?