Strange behaviour of QPushButton
-
Hi guys, I'm trying to create a simple game on Python with PyQt5.
which the user has to guess correctly the flag , if he presses the right button (name of the flag displayed in the label ) the program will update the label with another flag and update all buttons text which one of them contains the right answer .
If he presses the wrong button, nothing happens .
Basically for the GUI I did it on QtDesigner and I converted it to py through pyuic5 on cmd.
The problem is that when I run the program, initially it works for the first flag but not for others :
for example now the program displays the flag Kenya so I press the first button (Kenya).
Now the program updates the label (so another flag) and the buttons's text. Let's suppose that the next flag is Germany, if I press a random button (so a wrong answer) , the program updates the label and the buttons's text, it's as if I did right even though the answer is totally wrong.
There's another problem and that is : it basically skips some countries without any valid reason, it's hard to explain so I invite you to go to my github and run the script (first you have to unzip the flagcode.zip and put all the files inside a folder called "flagcode", it must be in the same directory of runme.py)
Sometimes it displays an empty label , this is not a problem because there are some files which contain some foreign characters , I can easily fix it but that's not the main problem.
The main problem like I said before is that these buttons behave in a strange way , I already did a debug test but I didn't manage to solve anything.
I would appreciate if you guys can explain to me the mistake I did, thank you very much!
this is the project : Guess-the-Flag -
Hi and welcome to devnet,
From the looks of it, you will end up with all buttons at some point connect and thus they will all trigger the same slot.
So either:
- disconnect all button before setting them up again
- associate the number to the button, connect them all once and in the slot check whether it's the correct button that was pressed
-
This post is deleted!
-
@SGaist Hi ,I choose the first way (because for me it's simple and sincerely I don't know how to do the second way ) but the program crashes after 2 flags
self.pushButton_1.disconnect() self.pushButton_2.disconnect() self.pushButton_3.disconnect() self.pushButton_4.disconnect()
I put them inside the function randomflag(self) before the call function self.retranslateUi()
-
What does the crash tell you ?
-
@SGaist the classic and generic error :
-
What was the issue ?