In my project i have created two mainwindow i want to call mainwindow2 from the mainwindow1 (which in running). in mainwindow1 i am already used the app.exec_() (PyQt) and to show maindow2 i am using the maindow2.show() in the click event of the button but does not show anything
Asked
Active
Viewed 6,423 times
1 Answers
6
Calling mainwindow2.show() should work for you. Could you give a more complete example of your code? There may be something wrong somewhere else.
EDIT: Code updated to show an example of how to hide and show windows when opening and closing other windows.
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, \
QLabel, QVBoxLayout, QWidget
from PyQt4.QtCore import pyqtSignal
class MainWindow1(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
button = QPushButton('Test')
button.clicked.connect(self.newWindow)
label = QLabel('MainWindow1')
centralWidget = QWidget()
vbox = QVBoxLayout(centralWidget)
vbox.addWidget(label)
vbox.addWidget(button)
self.setCentralWidget(centralWidget)
def newWindow(self):
self.mainwindow2 = MainWindow2(self)
self.mainwindow2.closed.connect(self.show)
self.mainwindow2.show()
self.hide()
class MainWindow2(QMainWindow):
# QMainWindow doesn't have a closed signal, so we'll make one.
closed = pyqtSignal()
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.parent = parent
label = QLabel('MainWindow2', self)
def closeEvent(self, event):
self.closed.emit()
event.accept()
def startmain():
app = QApplication(sys.argv)
mainwindow1 = MainWindow1()
mainwindow1.show()
sys.exit(app.exec_())
if __name__ == "__main__":
import sys
startmain()

Gary Hughes
- 4,400
- 1
- 26
- 40
-
thanks gray finally done . but i want to make window1 disable and window2 enable and when window2 close then window1 became renable – Arjun Jain Apr 12 '11 at 10:35
-
I've changed the code so that when the second QMainWindow is shown the first is hidden. When the second QMainWindow is closed the first will then be shown again. – Gary Hughes Apr 12 '11 at 11:48
-
Thanks very much for you code @GaryHughes I was struggling about it. My mistake was I was not sending first screen into second one. Can you explain little bit why do we need QMainWindow.__init__(self, parent) send parent? It makes sense to have connection between first and second window but I can not fully make sense why do we need to send parent into supers constructor.Thanks very much again – Can Jan 04 '16 at 20:08