Reputation: 19
I have two .py
files, respectively simple_1.py
and simple_2.py
.
How to display the window of simple_2.py when I click the button in simple_1.py ?
The same , how do display the window of simple_1.py when I click the button in simple_2.py ?
And when the window is called out by another .py file , now this window has to turn off at the same time.
Here is my simple_1.py code:
# -*- coding: utf-8 -*-
#simple_1.py
import sys
from PyQt4 import QtCore, QtGui
from simple import Ui_Form
class StartQT4(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
self.button()
def button(self):
button1= QtGui.QPushButton('show simple_2', self)
button1.setGeometry(80, 80,100, 50)
self.connect(button1, QtCore.SIGNAL('clicked()'),
self.buttonClicked)
def buttonClicked(self):
#to show window of simple_2.py
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
Here is my simple_2.py code:
# -*- coding: utf-8 -*-
#simple_2.py
import sys
from PyQt4 import QtCore, QtGui
class Apple(QtGui.QWidget):
def __init__(self,parent=None):
super().__init__()
self.widget = QtGui.QWidget()
self.resize(250, 150)
self.setWindowTitle('simple2')
self.button()
def button(self):
button1= QtGui.QPushButton('show simple_1', self)
button1.setGeometry(80, 80, 100, 50)
self.connect(button1, QtCore.SIGNAL('clicked()'),
self.buttonClicked)
def buttonClicked(self):
#to show window of simple_1.py
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mywidget = Apple()
mywidget.show()
sys.exit(app.exec_())
Upvotes: 1
Views: 50
Reputation: 7036
There are several errors in your code that prevent the examples you've posted from running. I will ignore them and answer your actual question, which from what I can tell is this:
How can I make my two Qt widgets create and display instances of each other?
First I'd suggest changing the widgets into QDialogs
, which have a convenient exec_()
method. You do this by inheriting from QtGui.QDialog
instead of from QtGui.QWidget
like so:
class Apple(QtGui.QDialog):
The next thing you will need to do is import and run your custom QDialogs in the button callbacks.
def buttonClicked(self):
from simple_1 import StartQT4 # imports your dialog from your other file
sqt = StartQT4() # creates an instance of it
self.close() # closes the current dialog
sqt.exec_() # runs the newly created dialog
Upvotes: 1