Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Newbie question - connect to label PyQt

Newbie question - connect to label PyQt

Scheduled Pinned Locked Moved Solved Qt for Python
8 Posts 3 Posters 587 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EarlyBird
    wrote on 6 Oct 2024, 06:10 last edited by
    #1

    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.

    alt text

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Axel Spoerl
      Moderators
      wrote on 6 Oct 2024, 07:28 last edited by
      #2

      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.

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      4
      • S SGaist moved this topic from General and Desktop on 6 Oct 2024, 19:01
      • E Offline
        E Offline
        EarlyBird
        wrote on 7 Oct 2024, 17:35 last edited by
        #3

        Thank you guys. Will investigate and read some more.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          EarlyBird
          wrote on 7 Oct 2024, 19:15 last edited by
          #4

          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())
          
          
          J 1 Reply Last reply 7 Oct 2024, 20:46
          1
          • E EarlyBird has marked this topic as solved on 7 Oct 2024, 19:16
          • E EarlyBird
            7 Oct 2024, 19:15

            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())
            
            
            J Offline
            J Offline
            JonB
            wrote on 7 Oct 2024, 20:46 last edited by JonB 10 Jul 2024, 20:49
            #5

            @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. Your line & line_edit should not be "global" variables. They belong inside your class MainWindow as member variables, use Python self.label & self.line_edit to create them in __init__() and access them from then on inside MainWindow.

            @Axel-Spoerl mentioned Python lambdas. Just so you know for your button.clicked.connect(self.button_clicked) you could have written

            button.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.

            1 Reply Last reply
            2
            • E Offline
              E Offline
              EarlyBird
              wrote on 8 Oct 2024, 09:17 last edited by
              #6

              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())
              
              
              J 1 Reply Last reply 8 Oct 2024, 09:37
              0
              • E EarlyBird
                8 Oct 2024, 09:17

                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())
                
                
                J Offline
                J Offline
                JonB
                wrote on 8 Oct 2024, 09:37 last edited by
                #7

                @EarlyBird
                Looks better, variables have been moved inside MainWindow.

                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.

                1 Reply Last reply
                2
                • E Offline
                  E Offline
                  EarlyBird
                  wrote on 8 Oct 2024, 15:37 last edited by
                  #8

                  Thank you @Axel-Spoerl and @JonB , works great and I understood all including lambda. Great help

                  1 Reply Last reply
                  0

                  1/8

                  6 Oct 2024, 06:10

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved