本例骨杂,展示了通過登錄界面打開主界面的實(shí)現(xiàn)方式。
在開始實(shí)現(xiàn)登錄界面前蛤售,先給大家普及一下PyQt5的安裝以及使用
1. pyQt5簡單使用
安裝
pip install PyQt5
pip3.5 install pyqt5-tools
界面化操作
1.在win+R中輸入designer并敲回車悴能,即可啟動Designer雳灾。一般選擇“Main Window”點(diǎn)擊“Create”即可創(chuàng)建。
若在win+R中輸入designer并敲回車后無反應(yīng)炒嘲,可以直接搜designer.exe直接啟動
2.創(chuàng)建后夫凸,可以方便快捷的用Qt Designer畫出對應(yīng)框體寸痢,如通過Combo Box添加下拉選擇的控件;通過Push Button添加按鈕啼止;通過List Widget添加列表框献烦;通過Table Widget添加數(shù)據(jù)表格框巩那,table中設(shè)置列數(shù)(右鍵-Edit Items-Colums),調(diào)整框體位置和文字大小噪生,背景顏色以及windowTitle來優(yōu)化界面顯示跺嗽,使用快捷鍵Ctrl+R預(yù)覽當(dāng)前編寫的GUI顯示如下:
3.點(diǎn)擊保存桨嫁,生成*.ui的文件份帐,本例中為test.ui,保存在D:\py\deploy文件夾下
2.開始實(shí)現(xiàn)登錄界面
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
#創(chuàng)建主窗口
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('主界面')
self.showMaximized()
#對話框
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)
###### 設(shè)置界面控件
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
# 通過驗(yàn)證,關(guān)閉對話框并返回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_())