簡體   English   中英

PyQt5 登錄注冊對話框自動關閉

[英]PyQt5 Login to Register Dialog Auto Close

我有一個登錄和注冊對話框,當然還有一個主要的 window。 所以,我希望用戶能夠在登錄和注冊對話框之間切換,如果用戶輸入正確的憑據,用戶可以 go 到主 window。 如果用戶注冊了新的憑證,那么用戶將被重定向到登錄 window。 也就是說,用戶看到的第一個界面是登錄對話框。 以下是登錄注冊對話框的一些代碼:

class Login(QDialog):
    # Push Button -> Login
    # Push Button 2 -> To Register
    # Push Button 3 -> Exit
    def __init__(self):
        super(Login, self).__init__()
        # Load Login UI
        loadUi("Login.ui", self)
        # Set Translucent Background for the Windows
        self.setWindowFlags(PyQt5.QtCore.Qt.FramelessWindowHint)
        self.setAttribute(PyQt5.QtCore.Qt.WA_TranslucentBackground)
        # Set Push Button 1 to call the function upon clicked
        self.pushButton.clicked.connect(self.check_login)
        # Set Push Button 2 to call the function upon clicked
        self.pushButton_2.clicked.connect(self.redirect_to_register)
        # Set Push Button 3 to call the function upon clicked
        self.pushButton_3.clicked.connect(self.close_window)
        # Show the Window
        self.show()

    # Procedure to redirect to register windows
    def redirect_to_register(self):
        self.close()  # Close the login windows
        # Show the register windows
        self.register = Register()
        self.register.show()
...

class Register(QDialog):
    # Push Button -> Register
    # Push Button 2 -> To Login
    # Push Button 3 -> Exit
    def __init__(self):
        # Load Register UI
        super(Register, self).__init__()
        loadUi("Register.ui", self)
        # Set Translucent Background for the Windows
        self.setWindowFlags(PyQt5.QtCore.Qt.FramelessWindowHint)
        self.setAttribute(PyQt5.QtCore.Qt.WA_TranslucentBackground)
        # Set Push Button 2 to call the function upon clicked
        self.pushButton.clicked.connect(self.insert_to_database)
        # Set Push Button 2 to call the function upon clicked
        self.pushButton_2.clicked.connect(self.redirect_to_login)
        # Set Push Button 3 to call the function upon clicked
        self.pushButton_3.clicked.connect(self.close_window)
        # Show the Window
        self.show()

    # Procedure to redirect to login windows
    def redirect_to_login(self):
        # Close the login windows
        self.close()
        # Show the register windows
        self.login = Login()
        self.login.show()

    def close_window(self):
        self.close()

我加載了兩個對話框的 .ui 文件。 但是,對於主 window,.py 文件是從 .ui 文件轉換而來的。 也就是說,在登錄文件的主文件中,我使用以下代碼調用主文件 window:

import GoodLife
# Call main
if __name__ == "__main__":
    # Init App
    app = QApplication(sys.argv)
    QtGui.QFontDatabase.addApplicationFont("../img/dripicons-v2.ttf")
    # Build the window
    window = Login()
    if(window.exec_() == QDialog.Accepted):
        GoodLife = QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(GoodLife)
        GoodLife.setWindowTitle("Good Life - Your Dear Bestfriend :)")
        GoodLife.setWindowIcon(QtGui.QIcon('../img/goodlife.jpg'))
        GoodLife.show()
        sys.exit(app.exec_())

使用該代碼,我可以在登錄后將 go 進入主 window。但是,每當我嘗試切換到注冊對話框時,注冊對話框都會自動關閉。 我相信這行sys.exit(app.exec_())是它的原因,因為當我取消縮進一次時,我可以 go 到寄存器,但不是主要的 window。 誰可以幫我這個事?

問題是,當一個對話框關閉時,它會退出它的事件循環(使用exec啟動的那個)並返回一個Rejected結果。

在您的情況下,由於拒絕與window.exec_() == QDialog.Accepted不匹配,因此結果是未評估整個塊,並且應用程序簡單地退出。

一個簡單的解決方案是在注冊完成(或被拒絕)之前隱藏登錄,然后再次顯示。

由於無論如何都應該顯示登錄對話框,因此不需要自定義信號或 if 語句,因為在redirect_to_register function 中調用注冊對話框的 exec_ 就足夠了,這樣無論注冊結果如何,您都可以再次顯示登錄。

不幸的是,有一個問題:在 Qt 中,隱藏對話框會導致類似於關閉它的行為(因此,拒絕)。
解決方案是避免 QDialog 的基本實現(覆蓋hide()setVisible() ),而使用 QWidget 的基本實現:

   def redirect_to_register(self):
        QtWidgets.QWidget.setVisible(self, False)
        self.register = Register()
        self.register.exec_()
        self.show()

這也意味着在注冊對話框中您不應嘗試重新創建登錄 window:

class Register(QDialog):
    def __init__(self):
        # ...
        self.pushButton_2.clicked.connect(self.accept)

一些未提出的建議:

  • 在您導入“GoodLife”的最后一個代碼中,您不僅沒有使用它,而且還將它作為變量覆蓋; 你可能犯了一些復制/粘貼/編輯錯誤,但重點是以大寫字母開頭的名稱只能用於類和常量,而不是變量;
  • 如果只調用self.close() ,則無需實現close_window function :只需將信號連接到self.close
  • 更詳細地使用 object 名稱,包括您在 Designer 中創建的小部件:擁有大量pushButton_x絕不是一個好主意,因為您無法輕松區分它們的角色;
  • 注釋應僅限於代碼不立即執行的部分; 諸如“設置按鈕 2 以在單擊時調用 function”或“顯示窗口”之類的評論完全無用且令人分心,因為它們沒有添加任何信息;
  • window 標題和圖標可以在Designer中設置;
  • 小心相對路徑,特別是對於父( ../ )文件夾; 考慮使用 Qt 資源系統(也允許您直接在 Designer 中設置圖標和圖像);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM