Congratulations to our 2022 Qt Champions!

PyQt5 Button issue

  • 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 !!!

  • Lifetime Qt Champion

    Hi and welcome to devnet,

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

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


  • Lifetime Qt Champion

    Well it makes me a bit think about QWizard.

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

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


  • Lifetime Qt Champion

    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.

  • Banned

    @Samuel-Bachorik I will strongly warn you against using super( ) python because when you use it introduces 4 known issues that you must be aware of as well as some current bugs --- further you are not really gaining anything from using it over the standard explicit version QMainWindow.__init__(self)

    Also if you had defined your Prompt list as:

    self.Questions = {}
    self.Questions[1] = ["Your stuff"(a),"a"]
    self.Questions[2] = ["Your stuff"(a),"a"]
    self.Questions[3] = ["Your stuff"(a),"a"]

    This would facilitate using a simpler Index to reference them when switching between prompts

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

    self.label = QtWidgets.QLabel(self)
    self.label.move(23, 30)

        self.b1 = QtWidgets.QPushButton(self)
        self.b1.move(130, 99)
        self.b1.resize(100, 32)
        self.textbox = QLineEdit(self)
        self.textbox.move(20, 100)
        self.textbox.resize(100, 30)
        self.textboxValue1 = self.textbox.text()


    def clicked2(self):
        self.textboxValue1 = self.textbox.text()
        score = 0
        for i in self.question_prompts3:
            if self.textboxValue1 == "a":
                score += 1


    What should i change ?

  • Lifetime Qt Champion

    @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.currentIndex += 1
        self.label.setText( self.question_prompts3[self.currentIndex][0])

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

  • Lifetime Qt Champion

    @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 :-)

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

  • Lifetime Qt Champion

    @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 :-)

  • THANK YOU !!!

  • Banned

    @Samuel-Bachorik just to let you know I am working on a MUC (Minimal Usable Code) version of this that I think you might find quite useful and perhaps enlightening using QLabels and QRadioButtons and Python-Qt as its meant to be used ;) I should have it done in a day or two as I squeeze in between my normal work

Log in to reply