Apr-20-2023, 06:13 PM
I think you are making this very difficult for yourself.
Instead of canvas and text you should use frames tkinter controls. I would make the score display and question a label and the myth and fact buttons.
Instead of using an event loop to occasionally check for a key press I would bind the key press to call a function.
Instead of looking for click events I would make myth and fact buttons and use button command callbacks.
This makes the logic fairly easy.
loop begin
ask question
wait for key press
wait for myth or fact button press
update score
repeat loop
But this being a GUI program, the waiting and the looping are event driven. There should be no sleep() statements or for or while loops.
When program starts, display the first question.
When a player key is pressed, lock out the keyboard and enable the Myth and Fact buttons
When a myth or fact button is pressed update the score, disable the Myth and Fact buttons, unlock the keyboard and display the next question.
I would use a variable to "lock" the keyboard. I would set the variable to None to indicate that no player has buzzed in. When a player hits their key, it calls a function. The function checks if the variable. If it is None, the function sets the variable to indicate what player buzzed in and enables the Myth and Fact buttons to provide feedback to the players. It might also update a label or some other display to indicate who buzzed in first. If the variable is not None, somebody already buzzed in and the keypress is ignored.
Instead of canvas and text you should use frames tkinter controls. I would make the score display and question a label and the myth and fact buttons.
Instead of using an event loop to occasionally check for a key press I would bind the key press to call a function.
Instead of looking for click events I would make myth and fact buttons and use button command callbacks.
This makes the logic fairly easy.
loop begin
ask question
wait for key press
wait for myth or fact button press
update score
repeat loop
But this being a GUI program, the waiting and the looping are event driven. There should be no sleep() statements or for or while loops.
When program starts, display the first question.
When a player key is pressed, lock out the keyboard and enable the Myth and Fact buttons
When a myth or fact button is pressed update the score, disable the Myth and Fact buttons, unlock the keyboard and display the next question.
I would use a variable to "lock" the keyboard. I would set the variable to None to indicate that no player has buzzed in. When a player hits their key, it calls a function. The function checks if the variable. If it is None, the function sets the variable to indicate what player buzzed in and enables the Myth and Fact buttons to provide feedback to the players. It might also update a label or some other display to indicate who buzzed in first. If the variable is not None, somebody already buzzed in and the keypress is ignored.