Alright so there are a few ways you can do this. You're on the right track for the signal slot thing. However, since you have multiple calls in each class to the other class, things get a little messed up.
One way to solve your problem is to simply change the __init__
function for your Window2
class so that it accepts text, and sets the QLineEdit
text to the inputted text. This will work once. When you click the button, the second window will show, and subsequent clicks won't change the text. Look below if you want the button to always update the text in the other window.
One time click
import sys
from PyQt4 import QtGui, QtCore
class Window1(QtGui.QMainWindow):
textSaved = QtCore.pyqtSignal(str)
def __init__(self):
super(Window1,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Signal Signal Emitter")
self.home()
def home(self):
self.__line=QtGui.QLineEdit("howdy", self)
self.__line.move(120,100)
btn=QtGui.QPushButton("Send Signal", self)
btn.clicked.connect(self.send_signal)
btn.move(0,100)
self.show()
def send_signal(self):
signal=self.__line.displayText()
self.textSaved.emit(signal)
self.Window2=Window2(signal) # instantiate Window2 with the text
self.Window2.show()
class Window2(QtGui.QMainWindow):
def __init__(self, txt):
self.text = txt
super(Window2,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Signal Slot Receiver")
self.home()
def home(self):
self.line_response=QtGui.QLineEdit(self.text, self)
self.line_response.move(120,100)
@QtCore.pyqtSlot()
def showMessage(self, message):
self.bigbox(self, "The message is:"+ message)
self.show()
def run():
app=QtGui.QApplication(sys.argv)
GUI=Window1()
sys.exit(app.exec_())
run()
I also don't know what self.bigbox
is, do you want that to update? When I tried running it, I changed it to self.line_response
because that's what I thought you meant.
This solution will work in the sense that when you click the Send Signal button on Window1
, you will get Window2
with the contents of the QLineEdit
of the first window in the corresponding one in the second window.
If you want these two windows to be linked so that whenever you press that button, the second window's QLineEdit
will change again to the current text in Window1
, you can add a simple if else statement in your send_signal
method inside your first class. You will also need to pass the actual Window1
class into Window2
If it's the first time pressing the button, instantiate the Window2
class with the current text. If it's the second + time pressing the button, you can emit a signal from class one and change the text in the second class. Here's some code that works this way:
Linked windows
import sys
from PyQt4 import QtGui, QtCore
class Window1(QtGui.QMainWindow):
textSaved = QtCore.pyqtSignal(str)
def __init__(self):
super(Window1,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Signal Signal Emitter")
self.home()
self.counter = 0
def home(self):
self.__line=QtGui.QLineEdit("howdy", self)
self.__line.move(120,100)
btn=QtGui.QPushButton("Send Signal", self)
btn.clicked.connect(self.send_signal)
btn.move(0,100)
self.show()
def send_signal(self):
if self.counter == 0:
signal=self.__line.displayText()
self.Window2=Window2(signal, self)
self.Window2.show()
self.textSaved.connect(self.Window2.showMessage)
self.counter = 1
else:
signal = self.__line.displayText()
self.textSaved.emit(signal)
class Window2(QtGui.QDialog):
def __init__(self, txt, window1):
self.text = txt
self.signal1 = window1.textSaved
super(Window2,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Signal Slot Receiver")
self.home()
self.signal1.connect(self.showMessage)
def home(self):
self.line_response=QtGui.QLineEdit(self.text, self)
self.line_response.move(120,100)
def showMessage(self, message):
self.line_response.setText(message)
def run():
app=QtGui.QApplication(sys.argv)
GUI=Window1()
sys.exit(app.exec_())
run()
If you have any questions, please comment! I wrote this kinda quickly; I will edit with more explanations when I have more time!