简体   繁体   English

如何在两个不同的 windows 中从 QlineEdits 获取文本? 我在 Maya 中使用 PySide2

[英]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.当我运行代码时,它总是会打印出“First_TextSecond_Text”,无论我在 lineEdits 中放了什么。

I'd like to print out whatever the user puts in the windows.我想打印出用户放入 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.按下按钮“创建第二个窗口”,将创建第二个 window 并具有类似的 QLineEdit 文本输入。

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.我想当我初始化“put_first_and_second_text_together”function 中的类时,它会将类重置为默认值。 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.您还在第二个 window 中为父级创建一个新实例作为__init__的参数,除了糟糕的选择之外,这毫无意义。

Use the parent argument with the existing instance, and make the function an instance method.将 parent 参数与现有实例一起使用,并使 function 成为实例方法。

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 .另请注意,您的try/except块是错误的,因为FirstWindow是 class,而deleteLater仅适用于instance

I strongly urge you to do more research on what classes, instances and methods are and how they work.我强烈建议您对什么是类、实例和方法以及它们如何工作进行更多研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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