我正在使用python和Qt开发一个应用程序。
我使用Qt设计了2个主窗口,即.QMainWindow(不是QWidget或QDialog)。
让它成为。
1.LoginWindow - LoginUI(Qt)
2.StuffWindow --- StuffUI
首先我应该显示登录窗口。
然后我应该将用户名传递给StaffWindow(管理内容所需的用户名)
- 醇>
应显示StaffWindow并关闭LoginWindow ..
我怎样才能实现这一目标?救救我..
答案 0 :(得分:4)
无论您的描述如何,我认为您的LoginWindow应该是QDialog,而您的StuffWIndow应该是MainWindow,并且功能如下......
login()
方法创建并执行()您的登录QDialog作为应用程序MODAL对话框这是一个快速概述:
class MainWindow():
def login():
loginDialog = LoginDialog()
# this is modal. wait for it to close
if loginDialog.exec_():
# dialog was accepted. check its values and maybe:
self.show()
else:
# maybe reshow the login dialog if they rejected it?
loginDialog.exec_()
if __name__ == "__main__":
app = QApp
win = MainWindow()
win.login()
app.exec_()
答案 1 :(得分:3)
我同意大多数提出的jdi点,但我更喜欢略有不同的方法。
LoginWindow
应为QDialog
以MODAL开头。exec_()
(即accept/reject
)的退货情况,以便登录或取消/退出。LoginWindow
MainWindow
在看到jdi的答案之前,我开始编写一个简单的例子。我不妨把它放在这里。
import sys
from PyQt4 import QtGui, QtCore
class LoginDialog(QtGui.QDialog):
def __init__(self, parent=None):
super(LoginDialog, self).__init__(parent)
self.username = QtGui.QLineEdit()
self.password = QtGui.QLineEdit()
loginLayout = QtGui.QFormLayout()
loginLayout.addRow("Username", self.username)
loginLayout.addRow("Password", self.password)
self.buttons = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel)
self.buttons.accepted.connect(self.check)
self.buttons.rejected.connect(self.reject)
layout = QtGui.QVBoxLayout()
layout.addLayout(loginLayout)
layout.addWidget(self.buttons)
self.setLayout(layout)
def check(self):
if str(self.password.text()) == "12345": # do actual login check
self.accept()
else:
pass # or inform the user about bad username/password
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.label = QtGui.QLabel()
self.setCentralWidget(self.label)
def setUsername(self, username):
# do whatever you want with the username
self.username = username
self.label.setText("Username entered: %s" % self.username)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
login = LoginDialog()
if not login.exec_(): # 'reject': user pressed 'Cancel', so quit
sys.exit(-1)
# 'accept': continue
main = MainWindow()
main.setUsername(login.username.text()) # get the username, and supply it to main window
main.show()
sys.exit(app.exec_())
答案 2 :(得分:3)
虽然这与您的问题没有直接关系,但您应始终为密码字段设置QLineEdit.EchoMode,如下所示(请参阅here):
self.password.setEchoMode(QtGui.QLineEdit.Password)
答案 3 :(得分:0)
这是Avari的PyQt5更新版本。添加了一些异常处理以显示如何捕获一些错误(在编写您的东西时。享受!
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Ref to this OP question. https://stackoverflow.com/questions/9689053/how-to-communicate-or-switch-between-two-windows-in-pyqt4
import sys
from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QDialog, QDialogButtonBox, QFormLayout, QLabel, QLineEdit, QWidget, QVBoxLayout
class LoginDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(LoginDialog, self).__init__(parent)
self.username = QLineEdit()
self.password = QLineEdit()
loginLayout = QFormLayout()
loginLayout.addRow("Username", self.username)
loginLayout.addRow("Password", self.password)
self.buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
self.buttons.accepted.connect(self.check)
self.buttons.rejected.connect(self.reject)
layout = QVBoxLayout()
layout.addLayout(loginLayout)
layout.addWidget(self.buttons)
self.setLayout(layout)
def check(self):
if str(self.password.text()) == "12345": # do actual login check
self.accept()
else:
pass # or inform the user about bad username/password
def my_exception_hook(exctype, value, traceback):
# Print the error and traceback
print(exctype, value, traceback)
# Call the normal Exception hook after
sys._excepthook(exctype, value, traceback)
sys.exit(1)
# Back up the reference to the exceptionhook
sys._excepthook = sys.excepthook
# Set the exception hook to our wrapping function
sys.excepthook = my_exception_hook
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.label = QLabel()
self.setCentralWidget(self.label)
def setUsername(self, username):
# do whatever you want with the username
self.username = username
self.label.setText("Username entered: %s" % self.username)
if __name__ == "__main__":
app = QApplication(sys.argv)
login = LoginDialog()
if not login.exec_(): # 'reject': user pressed 'Cancel', so quit
sys.exit(-1) # instead of -1 another action can be triggered here.
# 'accept': continue
main = MainWindow()
# get the username, and supply it to main window
main.setUsername(login.username.text())
main.show()
sys.exit(app.exec_())
答案 4 :(得分:0)
匹配用户名和密码。
'get /min/*': {
skipAssets: false,
fn: [
require('serve-static')('.tmp/public', {
maxAge: process.env.NODE_ENV !== 'production' ? 0 : 31557600000,
etag: false
}),
]
}