Reputation: 2777
I am using version python 2.7 and use pyqt4 for GUI programming in python. I want to call from Ui_MainWindow1
to Ui_MainWindow2
.Here is code:
Class Ui_MainWindow1
class Ui_MainWindow1(object):
def setupUi(self, MainWindow):
...
...
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(70, 90, 181, 27))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
MainWindow.setCentralWidget(self.centralwidget)
...
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"),self.callSecondWindow)
def callSecondWindow(self):
MainWindow2 = QtGui.QMainWindow()
ui = Ui_MainWindow2()
ui.setupUi(MainWindow2)
MainWindow2.show()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow1()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Ui_MainWindow2
class Ui_MainWindow2(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(336, 277)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(110, 70, 131, 51))
font = QtGui.QFont()
...
...
When I click on pushButton
in Ui_MainWindow1
.It's not showing another GUI(Ui_MainWindow2
) and without giving any error.
How to sort this out? Need Help!
Upvotes: 0
Views: 329
Reputation: 5440
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 that
the 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 directly
most 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
Upvotes: 1