Issue
Hii on QPushButton click I want to access QLabel value under current layout and set it in QLineEdit under same current layout in the following example :
from PySide2.QtWidgets import *
def main():
app = QApplication([])
layout1 = QVBoxLayout()
for x in range(0, 9):
layout1.addLayout(nested_layout())
window = QWidget()
window.setLayout(layout1)
window.show()
app.exec_()
def nested_layout():
name_0_lineEdit = QLineEdit()
name_0_label = QLabel('0')
name_0_label.setHidden(True)
name_0_button = QPushButton("Delete")
name_0_button.clicked.connect(set_value)
layout2 = QVBoxLayout()
layout2.addWidget(name_0_lineEdit)
layout2.addWidget(name_0_label)
layout2.addWidget(name_0_button)
return layout2
def set_value():
# Access label value in current layout and show it in QLineEdit widget in the same layout
print("Clicked")
if __name__ == "__main__":
main()
How can we do this I could'nt find any reference or solution to my problem
Solution
You can use a lambda to call the set_value function with the label and linedit widgets as parameters to the callback.
For example:
from PySide2.QtWidgets import *
def main():
app = QApplication([])
layout1 = QVBoxLayout()
for x in range(0, 9):
layout1.addLayout(nested_layout())
window = QWidget()
window.setLayout(layout1)
window.show()
app.exec_()
def nested_layout():
name_0_lineEdit = QLineEdit()
name_0_label = QLabel('0')
name_0_label.setHidden(True)
name_0_button = QPushButton("Delete")
name_0_button.clicked.connect(lambda: set_value(name_0_lineEdit, name_0_label))
layout2 = QVBoxLayout()
layout2.addWidget(name_0_lineEdit)
layout2.addWidget(name_0_label)
layout2.addWidget(name_0_button)
return layout2
def set_value(lineedit, label):
text = label.text()
lineedit.setText(text)
if __name__ == "__main__":
main()
However I highly recommend you look into subclassing and OOP approach to GUI programming... they allow much more flexibility than the procedural approach
Answered By - Alexander
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.