That looks messy. This is a more common way to write something like this:
from PyQt4 import QtGui
from PyQt4.uic.properties import QtCore
class MainWindow1(QtGui.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow1, self).__init__(*args, **kwargs)
self.pushButton = QtGui.QPushButton('pushButton')
self.pushButton.released.connect(self.callSecondWindow)
self.mainWindow2 = MainWindow2()
self.setCentralWidget(self.pushButton)
def callSecondWindow(self):
self.mainWindow2.show()
class MainWindow2(QtGui.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow2, self).__init__(*args, **kwargs)
self.resize(336, 227)
self.setObjectName('centralwidget')
self.label = QtGui.QLabel()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow1()
mainWindow.show()
sys.exit(app.exec_())
A few notes:
there is no need for a
setupUi
function python already has__init__
for thatthe Signal syntax:
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"),self.callSecondWindow)
has been replaced by the more readable:
self.pushButton.released.connect(self.callSecondWindow)
there is usually no reason to hold an instance of
QMainWindow
inside of an extra object, just subclass it directlymost people use lower case-names for variables, and upper-case names for classes, it's just a convention, but it makes the code more readable for others