如何在PyQt中的两个窗口之间进行通信或切换?
问题描述:
我正在开发一个应用程序使用python和Qt。如何在PyQt中的两个窗口之间进行通信或切换?
我设计了2个主窗口,即使用Qt的Q..MainWindow(不是QWidget或QDialog)。
让它成为。
1.LoginWindow - LoginUI(QT)
2.StuffWindow --- StuffUI
首先我应该显示登录窗口。
那么我应该将用户名传递给StaffWindow(需要管理的东西用户名)
StaffWindow应该显示和登录窗口应关闭..
我怎样才能做到这一点..?帮助我..
答
不管你的描述,我觉得你的登录窗口应该是一个QDialog的,和你的StuffWIndow是主窗口,而像这样的功能...
- 你StuffWindow主窗口被创建(不所示)
- 呼叫,作为一个应用程序模式对话框创建和exec_()登录了QDialog
- 启动app.exec_()事件循环现在,并等待用户与登录
- 用户交互的
login()
方法与登录dialo进行交互g,然后关闭对话框的结果将允许您的应用程序检查其值并选择显示其主界面。
下面是一个简单的轮廓:
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_()
答
我同意大多数提出的观点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_())
答
这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_())
是啊,这几乎是完全一样的方法,因为我的,除了事实,你移动逻辑到__main__而不是让MainWindow管理它,每种方式都有效, – jdi 2012-03-13 22:26:27