[英]How can I get the text from QlineEdits in two different windows? I'm using PySide2 within Maya
當我運行代碼時,它總是會打印出“First_TextSecond_Text”,無論我在 lineEdits 中放了什么。
我想打印出用戶放入 windows 的任何內容。 運行代碼並獲得“第一個窗口”。
您將能夠在其中編輯文本。 默認文本是“第一個文本”。 按下按鈕“創建第二個窗口”,將創建第二個 window 並具有類似的 QLineEdit 文本輸入。
您也可以編輯此文本。 最后一個按鈕“打印第一個 + 第二個文本”應該打印出用戶在編輯器中輸入的任何內容。
不幸的是,它總是打印出放在一起的默認文本。 我想當我初始化“put_first_and_second_text_together”function 中的類時,它會將類重置為默認值。 如果是這樣,必須有另一種方法來獲取活動窗口的屬性。 任何幫助,將不勝感激。
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()
這些類不是“重置為默認值”:您正在創建兩個對話框的新實例,而不是使用現有的。
您還在第二個 window 中為父級創建一個新實例作為__init__
的參數,除了糟糕的選擇之外,這毫無意義。
將 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)
另請注意,您的try/except
塊是錯誤的,因為FirstWindow
是 class,而deleteLater
僅適用於instance 。
我強烈建議您對什么是類、實例和方法以及它們如何工作進行更多研究。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.