python - pyqt5: 显示两种形式时出错,名称 'Window' 未定义
python - pyqt5: error in show two form, name 'Window' is not defined
欢迎..
我有两种形式:Form1=>Window & form2 =>MainWindow
在主window中按下退出按钮后出现问题。在哪里应该return 再次登录window。
form1(Window 登录)代码:
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(主要Window)代码:
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()
当运行时:
名称 'Window' 未定义
这对我来说似乎是某种 circular import 问题,但我无法复制它。您使用的 Python 是哪个版本?
无论如何,首先我会尝试更改 form1,以便在使用 MainWindow 之前导入是本地的,看看是否能解决问题。 IE。删除 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你刚刚关闭。这可能会导致新 window 甚至在它出现之前就被杀死(在我的例子中,没有出现并且 python 以代码 1 退出),因此 self
参数可能应该被删除:
def logout_action(self):
self.close()
a_window = Window() # no 'self' argument
a_window.show()
a_window.clear_box()
欢迎..
我有两种形式:Form1=>Window & form2 =>MainWindow
在主window中按下退出按钮后出现问题。在哪里应该return 再次登录window。
form1(Window 登录)代码:
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(主要Window)代码:
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()
当运行时: 名称 'Window' 未定义
这对我来说似乎是某种 circular import 问题,但我无法复制它。您使用的 Python 是哪个版本?
无论如何,首先我会尝试更改 form1,以便在使用 MainWindow 之前导入是本地的,看看是否能解决问题。 IE。删除 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你刚刚关闭。这可能会导致新 window 甚至在它出现之前就被杀死(在我的例子中,没有出现并且 python 以代码 1 退出),因此 self
参数可能应该被删除:
def logout_action(self):
self.close()
a_window = Window() # no 'self' argument
a_window.show()
a_window.clear_box()