2011-07-09 110 views
0

如何在pytq4中调用另一个窗口? 例如,我有两个单独的窗体。我想在登录表单中单击按钮时显示第二个表单。如何调用pyqt4 python中的另一个窗口?

编辑: 例如我有我的登录对话框。 当用户点击一个按钮时,main.py会出现,这个对话框将会关闭。

从PyQt4的进口QtCore LOGIN.py ,QtGui

class Ui_Dialog(object): 
    def setupUi(self, Dialog): 
     Dialog.setObjectName("Dialog") 
     Dialog.resize(379, 184) 
     self.buttonBox = QtGui.QDialogButtonBox(Dialog) 
     self.buttonBox.setGeometry(QtCore.QRect(20, 130, 341, 32)) 
     self.buttonBox.setOrientation(QtCore.Qt.Horizontal) 
     self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok) 
     self.buttonBox.setObjectName("buttonBox") 

     self.retranslateUi(Dialog) 
     QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), Dialog.accept) 
     QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject) 
     QtCore.QMetaObject.connectSlotsByName(Dialog) 

    def retranslateUi(self, Dialog): 
     Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8)) 


if __name__ == "__main__": 
    import sys 
    app = QtGui.QApplication(sys.argv) 
    Dialog = QtGui.QDialog() 
    ui = Ui_Dialog() 
    ui.setupUi(Dialog) 
    Dialog.show() 
    sys.exit(app.exec_()) 

MAIN.py

from PyQt4 import QtCore, QtGui 

class Ui_MainWindow(object): 
    def setupUi(self, MainWindow): 
     MainWindow.setObjectName("MainWindow") 
     MainWindow.resize(595, 315) 
     self.centralwidget = QtGui.QWidget(MainWindow) 
     self.centralwidget.setObjectName("centralwidget") 
     MainWindow.setCentralWidget(self.centralwidget) 

     self.retranslateUi(MainWindow) 
     QtCore.QMetaObject.connectSlotsByName(MainWindow) 

    def retranslateUi(self, MainWindow): 
     MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) 
+0

在某些时候,你需要调用form.show(),以使其他形式出现。关于您的计划的详细信息很难更具体。 – Luke

回答

0

我也有类似的情况,这是我做了什么。不觉得这是理想的解决方案,但它对我有用。

  import Main 
      self.main = Main.Form(user, loginWindow=self) 

      self.hide()   
      self.main.show() 

      # You can do self.close() here OR 
      # loginWindow.close() from Main.py 
      self.close() 

Main.py

class Form(QWidget): 
    def __init__(self, user, parent=None, loginWindow=None): 
     super(Form, self).__init__(parent) 
     # etc... 
+0

我尝试过你的代码,但它给了我一个错误,AttributeError:'module'object has no attribute'Form' – unice

+0

这是我的Main.py类定义'class Form(QWidget): def __init __(self,user,parent = None ,loginWindow = None): super(Form,self).__ init __(parent)' –

相关问题