Python GUI程式設計:高畫質電影線上觀看平臺製作,全網電影免費看
https://www.bilibili.com/video/BV1tz4y1o7Yc/
案例:設計一個使用者登入介面
工具:python PyQt5
程式執行結果如下:(1)初始介面
(3)切換“管理員”和“普通使用者”登入
設計程式如下:# -*- coding: utf-8 -*-from PyQt5 import QtCore, QtGui, QtWidgetsfrom PyQt5.QtGui import QPixmap, QIconfrom PyQt5.QtWidgets import QMessageBoxclass Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(300, 180) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(60, 30, 51, 16)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(60, 60, 51, 16)) self.label_2.setObjectName("label_2") self.lineEdit = QtWidgets.QLineEdit(self.centralwidget) self.lineEdit.setGeometry(QtCore.QRect(120, 30, 113, 20)) self.lineEdit.setObjectName("lineEdit") self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget) self.lineEdit_2.setGeometry(QtCore.QRect(120, 60, 113, 20)) self.lineEdit_2.setObjectName("lineEdit_2") self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password) # 設定文字框為密碼 self.lineEdit_2.setValidator(QtGui.QIntValidator(10000000,99999999)) # 設定只能輸入8位數字 self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(70, 120, 61, 27)) self.pushButton.setObjectName("pushButton") self.pushButton.setIcon(QIcon(QPixmap("login.ico"))) # 為“登入”按鈕設定圖示 self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(60, 30, 51, 16)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(60, 60, 51, 16)) self.label_2.setObjectName("label_2") self.radioButton = QtWidgets.QRadioButton(self.centralwidget) self.radioButton.setGeometry(QtCore.QRect(70, 90, 71, 18)) self.radioButton.setObjectName("radioButton") self.radioButton.setChecked(True) # 設定“管理員單選按鈕預設選中 self.radioButton_2 = QtWidgets.QRadioButton(self.centralwidget) self.radioButton_2.setGeometry(QtCore.QRect(150, 90, 81, 18)) self.radioButton_2.setObjectName("radioButton_2") self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(160, 120, 61, 27)) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_2.setIcon(QIcon(QPixmap("exit.ico"))) # 為“退出”按鈕設定圖示 MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) self.pushButton.clicked.connect(self.login) # 為“登入”按鈕的clicked訊號繫結自定義槽函式 self.pushButton_2.clicked.connect(MainWindow.close) # 為“退出”按鈕的clicked訊號繫結MainWindow視窗自帶的close()槽函式 self.radioButton.toggled.connect(self.select) # 為單選按鈕的toggled訊號繫結自定義槽函式 self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 317, 26)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) QtCore.QMetaObject.connectSlotsByName(MainWindow) def login(self): from PyQt5.QtWidgets import QMessageBox # 使用information()方法彈出資訊提示框 QMessageBox.information(MainWindow, "登入資訊", "使用者名稱: " + self.lineEdit.text() + " 密碼:" + self.lineEdit_2.text(), QMessageBox.Ok) # 自定義槽函式,用來判斷使用者登入身份 def select(self): if self.radioButton.isChecked(): # 判斷是否為管理員登入 QMessageBox.information(MainWindow, "提示", "您選擇的是 管理員 登入", QMessageBox.Ok) elif self.radioButton_2.isChecked(): # 判斷是否為普通使用者登入 QMessageBox.information(MainWindow, "提示", "您選擇的是 普通使用者 登入", QMessageBox.Ok) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "系統登入")) self.label.setText(_translate("MainWindow", "使用者名稱:")) self.label_2.setText(_translate("MainWindow", "密 碼:")) self.pushButton.setText(_translate("MainWindow", "登入")) self.pushButton_2.setText(_translate("MainWindow", "退出")) self.radioButton.setText(_translate("MainWindow", "管理員")) self.radioButton_2.setText(_translate("MainWindow", "普通使用者"))import sysif __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
最新評論