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 1.9k 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.
  • S Offline
    S Offline
    Samuel Bachorik
    wrote on 30 Mar 2020, 20:07 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 30 Mar 2020, 20:10 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
      • S Offline
        S Offline
        Samuel Bachorik
        wrote on 30 Mar 2020, 20:18 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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 30 Mar 2020, 20:20 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
          • S Offline
            S Offline
            Samuel Bachorik
            wrote on 30 Mar 2020, 20:30 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
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 30 Mar 2020, 21:05 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
              • S Offline
                S Offline
                Samuel Bachorik
                wrote on 31 Mar 2020, 07:05 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 ?

                J 1 Reply Last reply 31 Mar 2020, 07:15
                0
                • S Samuel Bachorik
                  31 Mar 2020, 07:05

                  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 ?

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 31 Mar 2020, 07:15 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
                  • S Offline
                    S Offline
                    Samuel Bachorik
                    wrote on 31 Mar 2020, 07:43 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.

                    J 1 Reply Last reply 31 Mar 2020, 08:25
                    0
                    • S Samuel Bachorik
                      31 Mar 2020, 07:43

                      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.

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 31 Mar 2020, 08:25 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
                      • S Offline
                        S Offline
                        Samuel Bachorik
                        wrote on 31 Mar 2020, 11:28 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.

                        J 1 Reply Last reply 31 Mar 2020, 11:33
                        0
                        • S Samuel Bachorik
                          31 Mar 2020, 11:28

                          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.

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 31 Mar 2020, 11:33 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
                          • S Offline
                            S Offline
                            Samuel Bachorik
                            wrote on 31 Mar 2020, 11:40 last edited by
                            #13

                            THANK YOU !!!

                            1 Reply Last reply
                            0

                            1/13

                            30 Mar 2020, 20:07

                            • Login

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