python + pyqt5 QlineEdit QMessageBox实现信息录入和消息弹框提醒
本人现在在做自动化工具开发的工作,因此,记录下自己平时遇到的问题和解决之道,还有一些简单的小工具
以下为主代码
# --*-- coding:utf-8 --*-- from first import Ui_MainWindow from PyQt5.QtWidgets import QApplication, QMainWindow,QMessageBox import os import sys reload(sys) sys.setdefaultencoding('utf-8') gcardnum = eval(sys.argv[1]) class Mainwindow(QMainWindow, Ui_MainWindow): global card_num def __init__(self): super(Mainwindow, self).__init__() self.setupUi(self) self.click_num = 1 self.sn_list = [] self.card_num = gcardnum self.label_2.setText(u"数量为:%s" % self.card_num) self.label.setText(u"第%s块" % self.click_num) self.button_OK.clicked.connect(self.begin_get) self.button_cancel.clicked.connect(self.cancel) def scan_sn(self, sn): if sn!="": with open("sn.txt", "a+") as fw: if len(sn) == 17: if sn not in self.sn_list: if self.click_num == int(self.card_num): QMessageBox.information(self, '提示信息', '保存成功') self.close() self.sn_list.append(sn) fw.write("%s\n" % sn) self.lineEdit_sn.clear() self.click_num += 1 self.label.setText(u"第%s块" % self.click_num) else: QMessageBox.information(self, '提示信息', '重复') self.lineEdit_sn.clear() else: QMessageBox.information(self, '提示信息', '输入有误') self.lineEdit_sn.clear() else: QMessageBox.information(self, '提示信息', '不能为空') def keyPressEvent(self, event): sn = self.lineEdit_sn.text() if str(event.key()) == "16777220": self.scan_sn(sn) def begin_get(self): sn = self.lineEdit_sn.text() self.scan_sn(sn) def cancel(self): self.lineEdit_sn.clear() if __name__ == '__main__': if os.path.exists("sn.txt"): os.remove("sn.txt") app = QApplication(sys.argv) mainWindow = Mainwindow() mainWindow.show() sys.exit(app.exec_())
以下为pyqt界面设计
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'first.ui' # # Created: Wed Jul 18 14:57:14 2018 # by: PyQt5 UI code generator 5.3.2 # # 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(802, 692) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(310, 150, 211, 51)) font = QtGui.QFont() font.setFamily("Agency FB") font.setPointSize(14) font.setBold(True) font.setWeight(75) self.label_2.setFont(font) self.label_2.setText("") self.label_2.setObjectName("label_2") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(180, 250, 101, 51)) font = QtGui.QFont() font.setPointSize(11) self.label.setFont(font) self.label.setObjectName("label") self.lineEdit_sn = QtWidgets.QLineEdit(self.centralwidget) self.lineEdit_sn.setGeometry(QtCore.QRect(290, 260, 301, 41)) font = QtGui.QFont() font.setPointSize(12) self.lineEdit_sn.setFont(font) self.lineEdit_sn.setObjectName("lineEdit_sn") self.button_OK = QtWidgets.QPushButton(self.centralwidget) self.button_OK.setGeometry(QtCore.QRect(280, 370, 112, 34)) self.button_OK.setObjectName("button_OK") self.button_cancel = QtWidgets.QPushButton(self.centralwidget) self.button_cancel.setGeometry(QtCore.QRect(470, 370, 112, 34)) self.button_cancel.setObjectName("button_cancel") self.label_3 = QtWidgets.QLabel(self.centralwidget) self.label_3.setGeometry(QtCore.QRect(290, 50, 301, 51)) font = QtGui.QFont() font.setPointSize(18) font.setBold(True) font.setWeight(75) self.label_3.setFont(font) self.label_3.setObjectName("label_3") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 802, 30)) 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.label.setText(_translate("MainWindow", "输入")) self.button_OK.setText(_translate("MainWindow", "OK")) self.button_cancel.setText(_translate("MainWindow", "Cancel")) self.label_3.setText(_translate("MainWindow", "扫描输入"))