pyqt5 登入介面的實現模板(加強版)
阿新 • • 發佈:2018-12-15
說明
本例,在登入介面第一版的基礎上,增加了主介面的登出功能和退出功能。
登出功能
# 動作一:登出
def on_printAction1_triggered(self):
self.close()
dialog = logindialog()
if dialog.exec_()==QDialog.Accepted:
the_window = MainWindow()
self.windowList.append(the_window) #這句一定要寫,不然無法重新登入
the_window. show()
退出功能
# 動作二:退出
def on_printAction2_triggered(self):
self.close()
還是很簡單的吧,好了,下面給出完整程式碼。
完整程式碼
【如下程式碼,完全複製,直接執行,即可使用】
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
################################################
#######建立主視窗
################################################
class MainWindow(QMainWindow):
windowList = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('主介面')
self.showMaximized()
# 建立選單欄
self.createMenus()
def createMenus(self):
# 建立動作 登出
self. printAction1 = QAction(self.tr("登出"), self)
self.printAction1.triggered.connect(self.on_printAction1_triggered)
# 建立動作 退出
self.printAction2 = QAction(self.tr("退出"), self)
self.printAction2.triggered.connect(self.on_printAction2_triggered)
# 建立選單,新增動作
self.printMenu = self.menuBar().addMenu(self.tr("登出和退出"))
self.printMenu.addAction(self.printAction1)
self.printMenu.addAction(self.printAction2)
# 動作一:登出
def on_printAction1_triggered(self):
self.close()
dialog = logindialog()
if dialog.exec_()==QDialog.Accepted:
the_window = MainWindow()
self.windowList.append(the_window) #這句一定要寫,不然無法重新登入
the_window.show()
# 動作二:退出
def on_printAction2_triggered(self):
self.close()
# 關閉介面觸發事件
def closeEvent(self, event):
print(999999999)
pass
################################################
#######對話方塊
################################################
class logindialog(QDialog):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('登入介面')
self.resize(200, 200)
self.setFixedSize(self.width(), self.height())
self.setWindowFlags(Qt.WindowCloseButtonHint)
###### 設定介面控制元件
self.frame = QFrame(self)
self.verticalLayout = QVBoxLayout(self.frame)
self.lineEdit_account = QLineEdit()
self.lineEdit_account.setPlaceholderText("請輸入賬號")
self.verticalLayout.addWidget(self.lineEdit_account)
self.lineEdit_password = QLineEdit()
self.lineEdit_password.setPlaceholderText("請輸入密碼")
self.verticalLayout.addWidget(self.lineEdit_password)
self.pushButton_enter = QPushButton()
self.pushButton_enter.setText("確定")
self.verticalLayout.addWidget(self.pushButton_enter)
self.pushButton_quit = QPushButton()
self.pushButton_quit.setText("取消")
self.verticalLayout.addWidget(self.pushButton_quit)
###### 繫結按鈕事件
self.pushButton_enter.clicked.connect(self.on_pushButton_enter_clicked)
self.pushButton_quit.clicked.connect(QCoreApplication.instance().quit)
def on_pushButton_enter_clicked(self):
# 賬號判斷
if self.lineEdit_account.text() == "":
return
# 密碼判斷
if self.lineEdit_password.text() == "":
return
# 通過驗證,關閉對話方塊並返回1
self.accept()
################################################
#######程式入門
################################################
if __name__ == "__main__":
app = QApplication(sys.argv)
dialog = logindialog()
if dialog.exec_()==QDialog.Accepted:
the_window = MainWindow()
the_window.show()
sys.exit(app.exec_())
本文如有幫助,敬請留言鼓勵。 本文如有錯誤,敬請留言改進。