I'm sorry, I just pasted a code where I used Qt Designer and imported the resulting gui.py (pyuic4 gui.ui gui.py) file. So forget it if you don't use Qt Designer. If you use it, you might want to take a look at here (13.1 Using the Generated Code)
Here's a minimal example where all the stuff is done by code:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def __init__(self):
lineEdit.setText("Just to fill up the dialog")
layout.addWidget(lineEdit)
self.widget.setLayout(layout)
self.setCentralWidget(self.widget)
self.setWindowTitle("Win2")
def __init__(self):
layout.addWidget(button)
self.widget.setLayout(layout)
self.setCentralWidget(self.widget)
self.setWindowTitle("Win1")
self.connect(button, SIGNAL('clicked()'), self.newWindow)
def newWindow(self):
self.myOtherWindow = OtherWindow()
self.myOtherWindow.show()
if __name__ == "__main__":
mainWindow = MainWindow()
mainWindow.setGeometry(100, 100, 200, 200)
mainWindow.show()
sys.exit(app.exec_())
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class OtherWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
layout = QHBoxLayout()
lineEdit = QLineEdit()
lineEdit.setText("Just to fill up the dialog")
layout.addWidget(lineEdit)
self.widget = QWidget()
self.widget.setLayout(layout)
self.setCentralWidget(self.widget)
self.setWindowTitle("Win2")
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
layout = QHBoxLayout()
button = QPushButton()
layout.addWidget(button)
self.widget = QWidget()
self.widget.setLayout(layout)
self.setCentralWidget(self.widget)
self.setWindowTitle("Win1")
self.connect(button, SIGNAL('clicked()'), self.newWindow)
def newWindow(self):
self.myOtherWindow = OtherWindow()
self.myOtherWindow.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.setGeometry(100, 100, 200, 200)
mainWindow.show()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
So the key point is the latter part:
...
self.connect(button, SIGNAL('clicked()'), self.newWindow)
def newWindow(self):
self.myOtherWindow = OtherWindow()
self.myOtherWindow.show()
...
self.connect(button, SIGNAL('clicked()'), self.newWindow)
def newWindow(self):
self.myOtherWindow = OtherWindow()
self.myOtherWindow.show()
To copy to clipboard, switch view to plain text mode
In the init of MainWindow, you connect the clicked signal of button to newWindow method.
newWindow method just creates a second window and shows it. That's it.
Bookmarks