簡體   English   中英

python-pyqt5:顯示兩種形式的錯誤,未定義名稱“窗口”

[英]python - pyqt5: error in show two form, name 'Window' is not defined

歡迎..

我有兩種形式:Form1 => Window&form2 => MainWindow

在主窗口中按退出按鈕后,就會出現此問題。 應該在何處再次返回登錄窗口。

form1(登錄窗口)代碼:

import sys
from PyQt5.QtWidgets import *
from form2 import *
class Window(QWidget):
    right_uname = "admin"
    right_pword = "password"

    def __init__(self):
        super().__init__()

        self.init_ui()

    def init_ui(self):
        self.lbl_intro = QLabel('Welcome, please login')
        self.lbl_enter_username = QLabel('Username:')
        self.lbl_enter_password = QLabel('Password:')
        self.txt_enter_username = QLineEdit()
        self.txt_enter_password = QLineEdit()
        self.cb_login = QCheckBox('Stay logged in?')
        self.btn_login = QPushButton('Login')


        self.grid = QGridLayout()
        self.grid.setSpacing(5)

        self.grid.addWidget(self.lbl_intro, 1, 1)

        self.grid.addWidget(self.lbl_enter_username, 2, 0)
        self.grid.addWidget(self.txt_enter_username, 2, 1)

        self.grid.addWidget(self.lbl_enter_password, 3, 0)
        self.grid.addWidget(self.txt_enter_password, 3, 1)

        self.grid.addWidget(self.cb_login, 4, 1)
        self.grid.addWidget(self.btn_login, 5, 1)


        self.v_box = QVBoxLayout()
        self.v_box.addStretch(0)
        self.v_box.addLayout(self.grid)
        self.v_box.addStretch(0)

        self.h_box = QHBoxLayout()
        self.h_box.addStretch(0)
        self.h_box.addLayout(self.v_box)
        self.h_box.addStretch(0)

        self.setLayout(self.h_box)

        self.btn_login.clicked.connect(lambda: self.btn_login_clk(self.txt_enter_username, self.txt_enter_password, self.cb_login.isChecked(), self.lbl_intro))


        self.setWindowTitle('Login test')

        self.show()

    def btn_login_clk(self, username, password, cb, intro):
        if username.text() == self.right_uname and password.text() == self.right_pword:
            if cb:
                intro.setText('Welcome,' + ' ' + self.right_uname + ' ' + 'cb ticked')
            else:
                self.mw = MainWindow()
                self.hide()
                self.mw.show()
        else:
            intro.setText('Wrong username or password')
            self.clear_box()

    def clear_box(self):
        self.txt_enter_username.clear()
        self.txt_enter_password.clear()
        self.txt_enter_username.setFocus()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    a_window = Window()
    sys.exit(app.exec())

from2(MainWindow)代碼:

from form1 import *
class MainWindow(Window):


    def __init__(self):
        super().__init__()

        self.init_ui()

    def init_ui(self):
        self.lbl_intro = QLabel('Main Window')
        self.lbl_user_logged = QLabel('Welcome,' + ' ' + self.right_uname)
        self.lbl_append = QLabel('Write something')
        self.txt_write_box = QLineEdit()
        self.btn_append = QPushButton('Append')
        self.btn_logout = QPushButton('Logout')

        layout = QVBoxLayout()
        layout.addWidget(self.lbl_intro)
        layout.addWidget(self.lbl_user_logged)
        layout.addWidget(self.lbl_append)
        layout.addWidget(self.txt_write_box)
        layout.addWidget(self.btn_append)
        layout.addWidget(self.btn_logout)

        self.setLayout(layout)
        self.setWindowTitle('Main')

        self.btn_append.clicked.connect(self.append_clk)
        self.btn_logout.clicked.connect(self.logout_action)

        self.show()

    def append_clk(self):
        self.appendee = open('text.txt', 'a')
        self.appendee.write('hry')
        self.appendee.close()

    def logout_action(self):
        self.close()
        a_window = Window(self)
        a_window.show()
        a_window.clear_box()

運行時:未定義名稱“窗口”

對我來說,這看起來像是某種循環導入問題,但我無法復制。 您正在使用哪個版本的Python?

無論如何,首先我會嘗試更改form1,以便在使用MainWindow之前就可以將導入本地化,以查看是否可以解決問題。 from form2 import * form1頂部的from form2 import *行中刪除,並將btn_login_clk方法更改為此:

def btn_login_clk(self, username, password, cb, intro):
    if username.text() == self.right_uname and password.text() == self.right_pword:
        if cb:
            intro.setText('Welcome,' + ' ' + self.right_uname + ' ' + 'cb ticked')
        else:
            from form2 import MainWindow  # local import
            self.mw = MainWindow()
            self.hide()
            self.mw.show()
    else:
        intro.setText('Wrong username or password')
        self.clear_box()

在一個稍微不相關的主題上,我看到關閉MainWindow(即注銷)時,您將創建一個Window,其父級設置為您剛剛關閉的MainWindow。 這可能會導致新窗口甚至在它出現之前就被殺死(在我的情況下,什么也沒有出現,並且python以代碼1退出),因此應該刪除self參數:

def logout_action(self):
    self.close()
    a_window = Window()  # no 'self' argument
    a_window.show()
    a_window.clear_box()

暫無
暫無

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

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