xaiohuo 发表于 2020-10-10 19:09:11

QT使用pyqtSignal跳转窗口问题

登录页面代码:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qdarkstyle
import hashlib
from db.db import DB


class SignInWidget(QWidget):
    is_admin_signal = pyqtSignal()
    is_student_signal = pyqtSignal(str)

    def __init__(self):
      super(SignInWidget, self).__init__()
      self.resize(900, 600)
      self.setWindowTitle("图书馆管理系统")
      self.setUpUI()

    def setUpUI(self):
      self.Vlayout = QVBoxLayout(self)
      self.Hlayout1 = QHBoxLayout()
      self.Hlayout2 = QHBoxLayout()
      self.formlayout = QFormLayout()

      self.label1 = QLabel("学号: ")
      labelFont = QFont()
      labelFont.setPixelSize(18)
      lineEditFont = QFont()
      lineEditFont.setPixelSize(16)
      self.label1.setFont(labelFont)
      self.lineEdit1 = QLineEdit()
      self.lineEdit1.setFixedHeight(32)
      self.lineEdit1.setFixedWidth(200)
      self.lineEdit1.setFont(lineEditFont)
      self.lineEdit1.setMaxLength(16)

      self.formlayout.addRow(self.label1, self.lineEdit1)

      self.label2 = QLabel("密码: ")
      self.label2.setFont(labelFont)
      self.lineEdit2 = QLineEdit()
      self.lineEdit2.setFixedHeight(32)
      self.lineEdit2.setFixedWidth(200)
      self.lineEdit2.setMaxLength(16)

      # 设置验证
      reg = QRegExp("PB{8}")
      pValidator = QRegExpValidator(self)
      pValidator.setRegExp(reg)
      self.lineEdit1.setValidator(pValidator)

      reg = QRegExp("+$")
      pValidator.setRegExp(reg)
      self.lineEdit2.setValidator(pValidator)

      passwordFont = QFont()
      passwordFont.setPixelSize(10)
      self.lineEdit2.setFont(passwordFont)

      self.lineEdit2.setEchoMode(QLineEdit.Password)
      self.formlayout.addRow(self.label2, self.lineEdit2)
      self.signIn = QPushButton("登 录")
      self.signIn.setFixedWidth(160)
      self.signIn.setFixedHeight(40)
      self.signIn.setFont(labelFont)
      self.formlayout.addRow("", self.signIn)

      self.label = QLabel("图书管理系统")
      fontlabel = QFont()
      fontlabel.setPixelSize(30)
      self.label.setFixedWidth(200)
      # self.label.setFixedHeight(80)
      self.label.setFont(fontlabel)
      self.Hlayout1.addWidget(self.label, Qt.AlignCenter)
      self.widget1 = QWidget()
      self.widget1.setLayout(self.Hlayout1)
      self.widget2 = QWidget()
      self.widget2.setFixedWidth(300)
      self.widget2.setFixedHeight(150)
      self.widget2.setLayout(self.formlayout)
      self.Hlayout2.addWidget(self.widget2, Qt.AlignCenter)
      self.widget = QWidget()
      self.widget.setLayout(self.Hlayout2)
      self.Vlayout.addWidget(self.widget1)
      self.Vlayout.addWidget(self.widget, Qt.AlignTop)

      self.signIn.clicked.connect(self.signInCheck)
      self.lineEdit2.returnPressed.connect(self.signInCheck)
      self.lineEdit1.returnPressed.connect(self.signInCheck)

    def signInCheck(self):
      number_Id = self.lineEdit1.text()
      password = self.lineEdit2.text()
      if number_Id == "" or password == "":
            print(QMessageBox.warning(self, "警告", "学号和密码不可为空!", QMessageBox.Yes, QMessageBox.Yes))
            return
      # 打开数据库连接
      db = DB()
      count, res = db.query_super(table_name='user', column_name='StudentId', condition=number_Id)
      print(count, res)
      hash = hashlib.md5()
      hash.update(password.encode(encoding='utf-8'))
      if count == 0:
            print(QMessageBox.information(self, "提示", "该账号不存在!", QMessageBox.Yes, QMessageBox.Yes))
      else:
            if number_Id == res and hash.hexdigest() == res:
                if res == '0':
                  self.is_admin_signal.emit()
                else:
                   self.is_student_signal.emit(number_Id)
            else:
                print(QMessageBox.information(self, "提示", "密码错误!", QMessageBox.Yes, QMessageBox.Yes))
      return


if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon("./images/MainWindow_1.png"))
    app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
    mainMindow = SignInWidget()
    mainMindow.show()
    sys.exit(app.exec_())
学生主页:
import sys

from PyQt5 import sip
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtCore import Qt
import qdarkstyle
from BookStorageViewer import BookStorageViewer
from borrowBookDialog import borrowBookDialog
from returnBookDialog import returnBookDialog
from BorrowStatusViewer import BorrowStatusViewer


class StudentHome(QWidget):
    def __init__(self, studentId):
      super(StudentHome, self).__init__()
      self.StudentId = studentId
      self.resize(700, 600)
      self.setWindowTitle("欢迎使用图书馆管理系统")
      self.setUpUI()

    def setUpUI(self):
      # 总布局
      self.layout = QHBoxLayout(self)
      # 按钮布局
      self.buttonLayout = QVBoxLayout()
      # 按钮
      self.borrowBookButton = QPushButton("借书")
      self.returnBookButton = QPushButton("还书")
      self.myBookStatus = QPushButton("借阅状态")
      self.allBookButton = QPushButton("所有书籍")
      self.buttonLayout.addWidget(self.borrowBookButton)
      self.buttonLayout.addWidget(self.returnBookButton)
      self.buttonLayout.addWidget(self.myBookStatus)
      self.buttonLayout.addWidget(self.allBookButton)
      self.borrowBookButton.setFixedWidth(100)
      self.borrowBookButton.setFixedHeight(42)
      self.returnBookButton.setFixedWidth(100)
      self.returnBookButton.setFixedHeight(42)
      self.myBookStatus.setFixedWidth(100)
      self.myBookStatus.setFixedHeight(42)
      self.allBookButton.setFixedWidth(100)
      self.allBookButton.setFixedHeight(42)
      font = QFont()
      font.setPixelSize(16)
      self.borrowBookButton.setFont(font)
      self.returnBookButton.setFont(font)
      self.myBookStatus.setFont(font)
      self.allBookButton.setFont(font)

      self.storageView = BookStorageViewer()
      self.borrowStatusView = BorrowStatusViewer(self.StudentId)
      self.allBookButton.setEnabled(False)

      self.layout.addLayout(self.buttonLayout)
      self.layout.addWidget(self.storageView)
      self.borrowBookButton.clicked.connect(self.borrowBookButtonClicked)
      self.returnBookButton.clicked.connect(self.returnBookButtonClicked)
      self.myBookStatus.clicked.connect(self.myBookStatusClicked)
      self.allBookButton.clicked.connect(self.allBookButtonClicked)

    def borrowBookButtonClicked(self):
      borrowDialog = borrowBookDialog(self.StudentId, self)
      borrowDialog.borrow_book_success_signal.connect(self.borrowStatusView.borrowedQuery)
      borrowDialog.borrow_book_success_signal.connect(self.storageView.searchButtonClicked)
      borrowDialog.show()
      borrowDialog.exec_()
      return

    def returnBookButtonClicked(self):
      returnDialog = returnBookDialog(self.StudentId, self)
      returnDialog.return_book_success_signal.connect(self.borrowStatusView.returnedQuery)
      returnDialog.return_book_success_signal.connect(self.borrowStatusView.borrowedQuery)
      returnDialog.return_book_success_signal.connect(self.storageView.searchButtonClicked)
      returnDialog.show()
      returnDialog.exec_()

    def myBookStatusClicked(self):
      self.layout.removeWidget(self.storageView)
      sip.delete(self.storageView)
      self.storageView = BookStorageViewer()
      self.borrowStatusView = BorrowStatusViewer(self.StudentId)
      self.layout.addWidget(self.borrowStatusView)
      self.allBookButton.setEnabled(True)
      self.myBookStatus.setEnabled(False)
      return

    def allBookButtonClicked(self):
      self.layout.removeWidget(self.borrowStatusView)
      sip.delete(self.borrowStatusView)
      self.borrowStatusView = BorrowStatusViewer(self.StudentId)
      self.storageView = BookStorageViewer()
      self.layout.addWidget(self.storageView)
      self.allBookButton.setEnabled(False)
      self.myBookStatus.setEnabled(True)
      return


if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon("./images/MainWindow_1.png"))
    app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
    mainMindow = StudentHome("123456")
    mainMindow.show()
    sys.exit(app.exec_())
管理员主页:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import qdarkstyle
from addBookDialog import addBookDialog
from dropBookDialog import dropBookDialog
from BookStorageViewer import BookStorageViewer
from UserManage import UserManage


class AdminHome(QWidget):
    def __init__(self):
      super().__init__()
      self.setUpUI()

    def setUpUI(self):
      self.resize(900, 600)
      self.setWindowTitle("欢迎使用图书馆管理系统")
      self.layout = QHBoxLayout()
      self.buttonlayout = QVBoxLayout()
      self.setLayout(self.layout)

      font = QFont()
      font.setPixelSize(16)
      self.userManageButton = QPushButton("用户管理")
      self.addBookButton = QPushButton("添加书籍")
      self.dropBookButton = QPushButton("淘汰书籍")
      self.userManageButton.setFont(font)
      self.addBookButton.setFont(font)
      self.dropBookButton.setFont(font)
      self.userManageButton.setFixedWidth(100)
      self.userManageButton.setFixedHeight(42)
      self.addBookButton.setFixedWidth(100)
      self.addBookButton.setFixedHeight(42)
      self.dropBookButton.setFixedWidth(100)
      self.dropBookButton.setFixedHeight(42)
      self.buttonlayout.addWidget(self.addBookButton)
      self.buttonlayout.addWidget(self.dropBookButton)
      self.buttonlayout.addWidget(self.userManageButton)
      self.layout.addLayout(self.buttonlayout)
      self.storageView = BookStorageViewer()
      self.layout.addWidget(self.storageView)

      self.addBookButton.clicked.connect(self.addBookButtonClicked)
      self.dropBookButton.clicked.connect(self.dropBookButtonClicked)
      self.userManageButton.clicked.connect(self.userManage)

    def addBookButtonClicked(self):
      addDialog = addBookDialog(self)
      addDialog.add_book_success_signal.connect(self.storageView.searchButtonClicked)
      addDialog.show()
      addDialog.exec_()

    def dropBookButtonClicked(self):
      dropDialog = dropBookDialog(self)
      dropDialog.drop_book_successful_signal.connect(self.storageView.searchButtonClicked)
      dropDialog.show()
      dropDialog.exec_()

    def userManage(self):
      UserDelete = UserManage(self)
      UserDelete.show()
      UserDelete.exec_()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon("./images/MainWindow_1.png"))
    app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
    mainMindow = AdminHome()
    mainMindow.show()
    sys.exit(app.exec_())
数据库文件:

CREATE TABLE `user` (
`id` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`StudentId` varchar(16) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`Name` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`Password` char(32) COLLATE utf8_unicode_ci NOT NULL,
`IsAdmin` bit(1) NOT NULL DEFAULT b'0',
`TimesBorrowed` int(11) NOT NULL DEFAULT '0',
`NumBorrowed` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `Unique_studentID` (`StudentId`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('01aa6e87-eefc-41b6-8627-f426e75cb2d1', '201725210151', 'admin', 'e10adc3949ba59abbe56e057f20f883e', '0', '0', '0');

如何在登录页面登录成功后,跳转到其它两个页面;


疾风怪盗 发表于 2020-10-10 19:24:11

跳转到其他页面,其实就是窗口的隐藏和显示吧

另外加一个__init__.py,来设置,主窗口,和其他窗口的显示隐藏的代码

hrp 发表于 2020-10-10 19:55:36

窗口实例不是有show和close方法么

mrcrisis 发表于 2020-10-10 20:27:40

{:10_277:}当初我自己写的是这么用的
检测先谈一个对话框 对话框有选项检测时学生还是管理员
https://s1.ax1x.com/2020/10/10/06e2Mn.png
页: [1]
查看完整版本: QT使用pyqtSignal跳转窗口问题