使用PyQt5為YoloV5新增介面(一)

專注的阿熊發表於2022-04-21

# -*- coding: utf-8 -*-

# @Modified by: Ruihao

# @ProjectName:yolov5-pyqt5

import sys

from datetime import datetime

from PyQt5 import QtWidgets

from PyQt5.QtWidgets import *

from utils.id_utils import get_id_info, sava_id_info # 賬號資訊工具函式

from lib.share import shareInfo # 公共變數名

# 匯入QT-Design生成的UI

from ui.login_ui import Login_Ui_Form

from ui.registe_ui import Ui_Dialog

# 匯入設計好的檢測介面

from detect_logical import UI_Logic_Window

# 介面登入

class win_Login(QMainWindow):

    def __init__(self, parent = None):

        super(win_Login, self).__init__(parent)

        self.ui_login = Login_Ui_Form()

        self.ui_login.setupUi(self)

        self.init_slots()

        self.hidden_pwd()

    # 密碼輸入框隱藏

    def hidden_pwd(self):

        self.ui_login.edit_password.setEchoMode(QLineEdit.Password)

    # 繫結訊號槽

    def init_slots(self):

        self.ui_login.btn_login.clicked.connect(self.onSignIn) # 點選按鈕登入

        self.ui_login.edit_password.returnPressed.connect(self.onSignIn) # 按下回車登入

        self.ui_login.btn_regeist.clicked.connect(self.create_id)

    # 跳轉到註冊介面

    def create_id(self):

        shareInfo.createWin = win_Register()

        shareInfo.createWin.show()

    # 儲存登入日誌

    def sava_login_log(self, username):

        with open('login_log.txt', 'a', encoding='utf-8') as f:

            f.write(username + '\t log in at' + datetime.now().strftimestrftime+ '\r')

    # 登入

    def onSignIn(self):

        print("You pressed sign in")

        # 從登陸介面獲得輸入賬戶名與密碼

        username = self.ui_login.edit_username.text().strip()

        password = self.ui_login.edit_password.text().strip()

        # 獲得賬號資訊

        USER_PWD = get_id_info()

        # print(USER_PWD)

        if username not in USER_PWD.keys():

            replay =外匯跟單gendan5.com QMessageBox.warning(self,"登陸失敗!", "賬號或密碼輸入錯誤", QMessageBox.Yes)

        else:

            # 若登陸成功,則跳轉主介面

            if USER_PWD.get(username) == password:

                print("Jump to main window")

                # # 例項化新視窗

                # # 寫法1:

                # self.ui_new = win_Main()

                # # 顯示新視窗

                # self.ui_new.show()

                # 寫法2:

                # 不用self.ui_new,因為這個子視窗不是從屬於當前視窗,寫法不好

                # 所以使用公用變數名

                shareInfo.mainWin = UI_Logic_Window()

                shareInfo.mainWin.show()

                # 關閉當前視窗

                self.close()

            else:

                replay = QMessageBox.warning(self, "!", "賬號或密碼輸入錯誤", QMessageBox.Yes)

# 註冊介面

class win_Register(QDialog):

    def __init__(self, parent = None):

        super(win_Register, self).__init__(parent)

        self.ui_register = Ui_Dialog()

        self.ui_register.setupUi(self)

        self.init_slots()

    # 繫結槽訊號

    def init_slots(self):

        self.ui_register.pushButton_regiser.clicked.connect(self.new_account)

        self.ui_register.pushButton_cancer.clicked.connect(self.cancel)

    # 建立新賬戶

    def new_account(self):

        print("Create new account")

        USER_PWD = get_id_info()

        # print(USER_PWD)

        new_username = self.ui_register.edit_username.text().strip()

        new_password = self.ui_register.edit_password.text().strip()

        # 判斷使用者名稱是否為空

        if new_username == "":

            replay = QMessageBox.warning(self, "!", "賬號不準為空", QMessageBox.Yes)

        else:

            # 判斷賬號是否存在

            if new_username in USER_PWD.keys():

                replay = QMessageBox.warning(self, "!", "賬號已存在", QMessageBox.Yes)

            else:

                # 判斷密碼是否為空

                if new_password == "":

                    replay = QMessageBox.warning(self, "!", "密碼不能為空", QMessageBox.Yes)

                else:

                    # 註冊成功

                    print("Successful!")

                    sava_id_info(new_username, new_password)

                    replay = QMessageBox.warning(self,  "!", "註冊成功!", QMessageBox.Yes)

                    # 關閉介面

                    self.close()

    # 取消註冊

    def cancel(self):

        self.close() # 關閉當前介面

if __name__ == "__main__":

    app = QApplication(sys.argv)

    # 利用共享變數名來例項化物件

    shareInfo.loginWin = win_Login() # 登入介面作為主介面

    shareInfo.loginWin.show()

    sys.exit(app.exec_())


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2888138/,如需轉載,請註明出處,否則將追究法律責任。

相關文章