简体   繁体   中英

How can I get the text from QlineEdits in two different windows? I'm using PySide2 within Maya

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()
    

The classes are not "reset to defaults": you're creating new instances of both dialogs, instead of using the existing ones.

You're also creating a new instance for the parent in the second window as an argument for the __init__ , which is pointless other than a terrible choice.

Use the parent argument with the existing instance, and make the function an instance method.

class FirstWindow(QtWidgets.QDialog):
    # ...
    def open_SecondWindow(self):
        window_ui = SecondWindow(self)
        window_ui.show()


class SecondWindow(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(SecondWindow, self).__init__(parent)
        # ...
        self.pushbutton.clicked.connect(self.put_first_and_second_text_together)

    def put_first_and_second_text_together(self):
        first_text = self.parent().lineedit.text()
        second_text = self.lineedit.text()
        print(first_text + second_text)

Also note that your try/except block is wrong, as FirstWindow is the class, while deleteLater only works on an instance .

I strongly urge you to do more research on what classes, instances and methods are and how they work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM