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. PyQt5 Button issue
Forum Updated to NodeBB v4.3 + New Features

PyQt5 Button issue

Scheduled Pinned Locked Moved Unsolved Qt for Python
13 Posts 4 Posters 2.0k Views 1 Watching
  • 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.
  • Samuel BachorikS Offline
    Samuel BachorikS Offline
    Samuel Bachorik
    wrote on last edited by
    #1

    Hello dear users, i am new in Qt and i want to ask little complicated question. Let me explain it. I have simple program where i have label with text and button. When i click button text in label changes. Is possible to make this button work more times ? I mean if i click 3 times my button label will change 3 times to something else.

    Thank you a lot for your help !!!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Yes it is possible. What exactly do you have in mind ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Samuel BachorikS Offline
        Samuel BachorikS Offline
        Samuel Bachorik
        wrote on last edited by
        #3

        Hello SGaist thank you for your fast reply. I am trying to make quiz in PyQt5. And it consists of - Button, label and textbox. Iam trying to make this work like - My label shows question and i should put correct answer to the textbox and click button to change question and store answer from text box as variable. I have also same problem with that textbox, when i want to store input from textbox as variable it work only with first question. For other it is not working. Do you know what i mean ? This quiz works for me if i make for every question new button new text box and new label. And i think that is not the right way.

        cbd228d3-2333-466a-bd30-e96ad4959f57-image.png

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Well it makes me a bit think about QWizard.

          In any case, where are you getting your questions and answer from ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Samuel BachorikS Offline
            Samuel BachorikS Offline
            Samuel Bachorik
            wrote on last edited by
            #5

            I defined them in code. I just need to i dont know how to say it but kind of refresh my all widgets or how to call that.
            I need to use them for 3 questions. When i put answer to textbox i store this value with self.textboxValue1 = self.textbox.text(). But when i try to store second value with same textbox it is still first value so i have no idea how to store two different values from one text box. Same with button. I am sorry for replying with delay but i am a new member and i can post only every 10 minutes.

            eb7e01f6-d8aa-49fc-8ade-49695a67350b-image.png

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Since you have a list, just increment the index you use to retrieve the data each time you click on the button and update the UI using that updated index.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • Samuel BachorikS Offline
                Samuel BachorikS Offline
                Samuel Bachorik
                wrote on last edited by
                #7

                Thank you for your help guys. Let me show you mine button and function.

                self.label = QtWidgets.QLabel(self)
                self.label.setText(str(self.question_prompts[0]))
                self.label.move(23, 30)
                self.update()

                    self.b1 = QtWidgets.QPushButton(self)
                    self.b1.setText("Enter")
                    self.b1.move(130, 99)
                    self.b1.resize(100, 32)
                    self.b1.clicked.connect(self.clicked2)
                
                
                    self.textbox = QLineEdit(self)
                    self.textbox.move(20, 100)
                    self.textbox.resize(100, 30)
                
                    self.textboxValue1 = self.textbox.text()
                

                HERE ARE MY WIDGETS

                def clicked2(self):
                    self.textboxValue1 = self.textbox.text()
                    self.textbox.setText("")
                    score = 0
                    for i in self.question_prompts3:
                        self.label.setText(i[0])
                        self.update()
                
                        if self.textboxValue1 == "a":
                            score += 1
                            self.label.setText("good")
                
                        else:
                            self.label.setText("bad")
                

                AND HERE IS MY FUNCTION FOR BUTTON

                What should i change ?

                jsulmJ 1 Reply Last reply
                0
                • Samuel BachorikS Samuel Bachorik

                  Thank you for your help guys. Let me show you mine button and function.

                  self.label = QtWidgets.QLabel(self)
                  self.label.setText(str(self.question_prompts[0]))
                  self.label.move(23, 30)
                  self.update()

                      self.b1 = QtWidgets.QPushButton(self)
                      self.b1.setText("Enter")
                      self.b1.move(130, 99)
                      self.b1.resize(100, 32)
                      self.b1.clicked.connect(self.clicked2)
                  
                  
                      self.textbox = QLineEdit(self)
                      self.textbox.move(20, 100)
                      self.textbox.resize(100, 30)
                  
                      self.textboxValue1 = self.textbox.text()
                  

                  HERE ARE MY WIDGETS

                  def clicked2(self):
                      self.textboxValue1 = self.textbox.text()
                      self.textbox.setText("")
                      score = 0
                      for i in self.question_prompts3:
                          self.label.setText(i[0])
                          self.update()
                  
                          if self.textboxValue1 == "a":
                              score += 1
                              self.label.setText("good")
                  
                          else:
                              self.label.setText("bad")
                  

                  AND HERE IS MY FUNCTION FOR BUTTON

                  What should i change ?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Samuel-Bachorik Why do you have this loop inside clicked2()? In your class you should have a variable storing current index in self.question_prompts3. When user presses the button you increment this index and show next entry from self.question_prompts3 without any loops in clicked2():

                  def clicked2(self):
                      if self.textbox.text() == self.question_prompts3[self.currentIndex][1]:
                          self.score += 1
                  
                      self.textbox.setText("")
                      self.currentIndex += 1
                      self.label.setText( self.question_prompts3[self.currentIndex][0])
                  

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • Samuel BachorikS Offline
                    Samuel BachorikS Offline
                    Samuel Bachorik
                    wrote on last edited by Samuel Bachorik
                    #9

                    cff1b363-4a18-40cd-93f5-7c4852ddfefd-image.png

                    When i make my program without Qt it looks like this, that is why i used for loop. I think this project is just too much for me :D I just cant get it to work. I can paste here full code if it can help.

                    jsulmJ 1 Reply Last reply
                    0
                    • Samuel BachorikS Samuel Bachorik

                      cff1b363-4a18-40cd-93f5-7c4852ddfefd-image.png

                      When i make my program without Qt it looks like this, that is why i used for loop. I think this project is just too much for me :D I just cant get it to work. I can paste here full code if it can help.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Samuel-Bachorik There is one important thing to learn when using asynchronous frameworks like Qt: such frameworks have an event loop and you should never block this loop. It can be hard at the beginning to learn and understand how such frameworks and aplications using them work. But once you understood the basics it is not hard any-more :-)
                      In your non-Qt app such a loop works perfectly, but not in an asynchronous/event-loop based application.

                      "I just cant get it to work" - keep trying and ask here if you have problems :-)

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • Samuel BachorikS Offline
                        Samuel BachorikS Offline
                        Samuel Bachorik
                        wrote on last edited by
                        #11

                        Understand, so my last question is. Do you have any example code which have button that is able to change label to something else so many times as i want ? This is my biggest problem i do not know how to make button useable more times. i dont know for example when click first time change label to this. If i click second time do this. If i click third time do this.

                        Thank you again for your time man. I really appreciate it.

                        jsulmJ 1 Reply Last reply
                        0
                        • Samuel BachorikS Samuel Bachorik

                          Understand, so my last question is. Do you have any example code which have button that is able to change label to something else so many times as i want ? This is my biggest problem i do not know how to make button useable more times. i dont know for example when click first time change label to this. If i click second time do this. If i click third time do this.

                          Thank you again for your time man. I really appreciate it.

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Samuel-Bachorik Did you check the code I provided above? It should show next question each time you press the button. It is not complete as I don not initialise self.currentIndex and don't check whether it is still in range, but this exercise is up to you :-)

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          2
                          • Samuel BachorikS Offline
                            Samuel BachorikS Offline
                            Samuel Bachorik
                            wrote on last edited by
                            #13

                            THANK YOU !!!

                            1 Reply Last reply
                            0

                            • Login

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