Newbie question - connect to label PyQt
-
Hope that I am right here for PyQt6 question.
After reading the the turoial page: link text, I added a line into the code to change the text for label. See line #18.
Got en error message for this.
How do I connect this? Just for learning purpose, I want to change the displayed text when you click the button. -
Please don't post pictures of text. Add your code directly, using the </> code tags.
You can't pass an argument in a connect statement. You have to define a lambda or a slot, connect to it and do the work there. -
S SGaist moved this topic from General and Desktop on
-
Thanks for the answer Axel, understood it and now it works:
here my code: I had to move the objects label & line_edit into the main window:
#pyqt6 import sys from PyQt6.QtWidgets import (QApplication, QWidget, QLineEdit, QPushButton, QVBoxLayout, QLabel) from PyQt6.QtGui import QPixmap class MainWindow(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle('Qt Signal and Slots') button = QPushButton('Click me') button2 = QPushButton('Cear Text') button3 = QPushButton('Print Text') button.clicked.connect(self.button_clicked) button2.clicked.connect(self.Clear_Text) button3.clicked.connect(self.Print_Text) label.setText('Erster Text') line_edit.textChanged.connect(label.setText) #added Line button.clicked.connect(self.Text_Eingabe) layout = QVBoxLayout() self.setLayout(layout) layout.addWidget(button) layout.addWidget(button2) layout.addWidget(button3) layout.addWidget(label) layout.addWidget(line_edit) self.show() def button_clicked(self): print('clicked') def Text_Eingabe(self): print('click') label.setText('angeklicked') def Clear_Text(self): label.clear() line_edit.clear() def Print_Text(self): print(label.text()) if __name__ == '__main__': app = QApplication(sys.argv) label = QLabel() line_edit = QLineEdit() window = MainWindow() #print(dir(QLineEdit())) sys.exit(app.exec())
-
-
Thanks for the answer Axel, understood it and now it works:
here my code: I had to move the objects label & line_edit into the main window:
#pyqt6 import sys from PyQt6.QtWidgets import (QApplication, QWidget, QLineEdit, QPushButton, QVBoxLayout, QLabel) from PyQt6.QtGui import QPixmap class MainWindow(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle('Qt Signal and Slots') button = QPushButton('Click me') button2 = QPushButton('Cear Text') button3 = QPushButton('Print Text') button.clicked.connect(self.button_clicked) button2.clicked.connect(self.Clear_Text) button3.clicked.connect(self.Print_Text) label.setText('Erster Text') line_edit.textChanged.connect(label.setText) #added Line button.clicked.connect(self.Text_Eingabe) layout = QVBoxLayout() self.setLayout(layout) layout.addWidget(button) layout.addWidget(button2) layout.addWidget(button3) layout.addWidget(label) layout.addWidget(line_edit) self.show() def button_clicked(self): print('clicked') def Text_Eingabe(self): print('click') label.setText('angeklicked') def Clear_Text(self): label.clear() line_edit.clear() def Print_Text(self): print(label.text()) if __name__ == '__main__': app = QApplication(sys.argv) label = QLabel() line_edit = QLineEdit() window = MainWindow() #print(dir(QLineEdit())) sys.exit(app.exec())
@EarlyBird
You have done well grasping the basic of signals & slots.I had to move the objects label & line_edit into the main window:
You actually moved them out of
MainWindow
. Yourline
&line_edit
should not be "global" variables. They belong inside yourclass MainWindow
as member variables, use Pythonself.label
&self.line_edit
to create them in__init__()
and access them from then on insideMainWindow
.@Axel-Spoerl mentioned Python lambdas. Just so you know for your
button.clicked.connect(self.button_clicked)
you could have writtenbutton.clicked.connect(lambda: print('clicked'))
There are times in the future where you may need to use a lambda like this as an "anonymous" function to connect as a slot.
-
Thank you JonB, with your tips, I could change it to anything inside MainWindow.
Do you have a link to a good tutorial explaining lambda:?Here my latest development:
#pyqt6 import sys from PyQt6.QtWidgets import (QApplication, QWidget, QLineEdit, QPushButton, QVBoxLayout, QLabel) from PyQt6.QtGui import QPixmap class MainWindow(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle('Qt Signal and Slots') self.setGeometry(100,100,320,210) self.label = QLabel() self.line_edit = QLineEdit() button = QPushButton('Click me') button2 = QPushButton('Cear Text') button3 = QPushButton('Print Text') button.clicked.connect(self.button_clicked) button2.clicked.connect(self.Clear_Text) button3.clicked.connect(self.Print_Text) label2 = QLabel() pixmap1 = QPixmap('IMG_0205.JPG') pix_label = QLabel() pix_label.setPixmap(pixmap1) self.label.setText('Erster Text') label2.setNum(pixmap1.width()) self.line_edit.textChanged.connect(self.label.setText) #added Line button.clicked.connect(self.Text_Eingabe) layout = QVBoxLayout() self.setLayout(layout) layout.addWidget(button) layout.addWidget(button2) layout.addWidget(button3) layout.addWidget(self.label) layout.addWidget(self.line_edit) layout.addWidget(label2) layout.addWidget(pix_label) self.show() def Text_Eingabe(self): print('click') self.label.setText('angeklicked') def Clear_Text(self): self.label.clear() self.line_edit.clear() def Print_Text(self): print(self.label.text()) def button_clicked(self): print('clicked') if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() #print(dir(QLineEdit())) sys.exit(app.exec())
-
Thank you JonB, with your tips, I could change it to anything inside MainWindow.
Do you have a link to a good tutorial explaining lambda:?Here my latest development:
#pyqt6 import sys from PyQt6.QtWidgets import (QApplication, QWidget, QLineEdit, QPushButton, QVBoxLayout, QLabel) from PyQt6.QtGui import QPixmap class MainWindow(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle('Qt Signal and Slots') self.setGeometry(100,100,320,210) self.label = QLabel() self.line_edit = QLineEdit() button = QPushButton('Click me') button2 = QPushButton('Cear Text') button3 = QPushButton('Print Text') button.clicked.connect(self.button_clicked) button2.clicked.connect(self.Clear_Text) button3.clicked.connect(self.Print_Text) label2 = QLabel() pixmap1 = QPixmap('IMG_0205.JPG') pix_label = QLabel() pix_label.setPixmap(pixmap1) self.label.setText('Erster Text') label2.setNum(pixmap1.width()) self.line_edit.textChanged.connect(self.label.setText) #added Line button.clicked.connect(self.Text_Eingabe) layout = QVBoxLayout() self.setLayout(layout) layout.addWidget(button) layout.addWidget(button2) layout.addWidget(button3) layout.addWidget(self.label) layout.addWidget(self.line_edit) layout.addWidget(label2) layout.addWidget(pix_label) self.show() def Text_Eingabe(self): print('click') self.label.setText('angeklicked') def Clear_Text(self): self.label.clear() self.line_edit.clear() def Print_Text(self): print(self.label.text()) def button_clicked(self): print('clicked') if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() #print(dir(QLineEdit())) sys.exit(app.exec())
@EarlyBird
Looks better, variables have been moved insideMainWindow
.Lambdas are just a feature of Python, nothing special for Qt. Something like https://www.w3schools.com/python/python_lambda.asp or https://www.geeksforgeeks.org/python-lambda-anonymous-functions-filter-map-reduce/ describes them, or you can look up in any Python documentation.