2012-03-13 83 views
2

我正在開發一個應用程序使用python和Qt。如何在PyQt中的兩個窗口之間進行通信或切換?

我設計了2個主窗口,即使用Qt的Q..MainWindow(不是QWidget或QDialog)。

讓它成爲。

1.LoginWindow - LoginUI(QT)

2.StuffWindow --- StuffUI

  1. 首先我應該顯示登錄窗口。

  2. 那麼我應該將用戶名傳遞給StaffWindow(需要管理的東西用戶名)

  3. StaffWindow應該顯示和登錄窗口應關閉..

我怎樣才能做到這一點..?幫助我..

回答

4

不管你的描述,我覺得你的登錄窗口應該是一個QDialog的,和你的StuffWIndow是主窗口,而像這樣的功能...

  1. 你StuffWindow主窗口被創建(不所示)
  2. 呼叫,作爲一個應用程序模式對話框創建和exec_()登錄了QDialog
  3. 啓動app.exec_()事件循環現在,並等待用戶與登錄
  4. 用戶交互的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_() 
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_()) 
+0

是啊,這幾乎是完全一樣的方法,因爲我的,除了事實,你移動邏輯到__main__而不是讓MainWindow管理它,每種方式都有效, – jdi 2012-03-13 22:26:27

2

儘管這與您的問題沒有直接關係,您應該始終設置QLineEdit。EchoMode爲如下密碼字段(見here):

self.password.setEchoMode(QtGui.QLineEdit.Password) 
+0

確實有點偏離主題,但是100%關於「我忘記設置此值」的主題。 – ZF007 2017-12-04 21:34:19

0

這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_()) 
相關問題