Reputation: 1215
I have two windows, (lets name them X & Y. When X gets a signal, it's supposed to kill itself and run window Y.
How i try to do this:
My X and Y look like this:
This is X which calls Y
from PyQt4 import QtCore, QtGui
import sqlite3,hashlib,time
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def __init__(self):
self.server = sqlite3.connect('ems.db')
self.cursor = self.server.cursor()
def dummy(self,*args):
self.close()
#close("Kallu")
def setupUi(self, MainWindow):
# A button calls dummy here
# Other boring stuff
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
#Boring
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
def close(user): #Routine for closing window
app.closeAllWindows()
app.quit()
app.exit()
import y #here i import Y
y.main(app) #Dosent works even if i pass the app as i have to run
#app again there but it'll show already running
if __name__ == "__main__":
MainWindow.show()
sys.exit(app.exec_())
This is the Y code
import time
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(746, 503)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
import sys
def main(user):
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I don't need you to modify the code, rather make me understand how to do it and why it dosent works?
Upvotes: 0
Views: 210
Reputation: 244282
A program is not a set of files, but a set of interacting objects, so the idea is that both windows are in the same place, and when closing the other opens. On the other hand PyQt recommends not modifying the class generated by Qt Designer but creating a new class that inherits the appropriate widget and uses the initial class as an interface (for more information read the docs).
x.py
from PyQt4 import QtCore, QtGui
from y import YMainWindow
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.button = QtGui.QPushButton("Close")
MainWindow.setCentralWidget(self.button)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
pass
class XMainWindow(QtGui.QMainWindow, Ui_MainWindow):
closed = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(XMainWindow, self).__init__(parent)
self.setupUi(self)
self.button.clicked.connect(self.dummy)
@QtCore.pyqtSlot()
def dummy(self):
self.closed.emit()
self.close()
import sys
def main():
app = QtGui.QApplication.instance()
if app is None:
app = QtGui.QApplication(sys.argv)
wx = XMainWindow()
wy = YMainWindow()
wx.closed.connect(wy.show)
wx.show()
return app.exec_()
if __name__ == "__main__":
sys.exit(main())
y.py
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(746, 503)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
pass
class YMainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(YMainWindow, self).__init__(parent)
self.setupUi(self)
Upvotes: 1