When I run the code, it will ALWAYS print out "First_TextSecond_Text", no matter what I put inside the lineEdits.
I'd like to print out whatever the user puts in the windows. Run the code and get the "First Window".
You'll be able to edit the text in there. The default text is "First Text". Pressing the button, "Create Second Window", will create the second window with a similar QLineEdit text input.
You'll be able to edit this text as well. The final button, "Print First + Second Text" should print out whatever the user has typed in the editors concatenated together.
Unfortunately it always prints out the default text put together. I think when I initialize the classes in the "put_first_and_second_text_together" function, It resets the classes to defaults. If so, there must be another way to get the active windows' attributes. Any help would be appreciated.
from PySide2 import QtWidgets
class FirstWindow(QtWidgets.QDialog):
def __init__(self, parent=None):
super(FirstWindow, self).__init__(parent)
# Set the Title of Window
self.setWindowTitle("First Window")
# Create QlineEdit
self.lineedit = QtWidgets.QLineEdit()
# Create button
self.pushbutton = QtWidgets.QPushButton('Create Second Window')
# Layout
form_layout = QtWidgets.QFormLayout()
form_layout.addRow(self.lineedit)
form_layout.addRow(self.pushbutton)
main_layout = QtWidgets.QVBoxLayout(self)
main_layout.addLayout(form_layout)
# Connections
self.lineedit.setText('First_Text')
self.pushbutton.clicked.connect(self.open_SecondWindow)
def open_SecondWindow(self):
Window_ui = SecondWindow()
Window_ui.show()
class SecondWindow(QtWidgets.QDialog):
def __init__(self, parent=FirstWindow()):
super(SecondWindow, self).__init__(parent)
# Set the Title of Window
self.setWindowTitle("Second Window")
# Create QlineEdit
self.lineedit = QtWidgets.QLineEdit()
# Create button
self.pushbutton = QtWidgets.QPushButton('Print First + Second Text')
# Layout
form_layout = QtWidgets.QFormLayout()
form_layout.addRow(self.lineedit)
form_layout.addRow(self.pushbutton)
main_layout = QtWidgets.QVBoxLayout(self)
main_layout.addLayout(form_layout)
# Connections
self.lineedit.setText('Second_Text')
self.pushbutton.clicked.connect(put_first_and_second_text_together)
def put_first_and_second_text_together():
# Initialise
first_win = FirstWindow()
second_win = SecondWindow()
# Get Text
first_text = first_win.lineedit.text()
second_text = second_win.lineedit.text()
# Print Text
print(first_text + second_text)
if __name__ == "__main__":
try:
FirstWindow_dialog.close()
FirstWindow.deleteLater()
except:
pass
FirstWindow_dialog = FirstWindow()
FirstWindow_dialog.show()