PyQt5 Button issue
-
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. -
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.
-
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 ?
-
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 ?
@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])
-
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.
-
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.
@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.
-
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.
@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 !!!