Apr-20-2023, 01:20 PM
Hello, I'm trying to create a simple myth/fact quiz using tkinter. The game should be for two players - the player who presses the key first (the red team has the "R" key and the blue team has the "B" key) can answer (click) on the myth/fact. If he answers correctly, the score will be increased by 10... statements for which it will be argued whether they are myth or fact are from the file. Each line contains a new statement and also a correct answer (for example, one line from file looks like this: Going into space makes you weightless.=myth)
the problem is that I don't know how to load only one line at a time, wait until someone presses the r/b key, evaluate the answer and only then show the next question. It also dont show the score correctly.
I have tried to change it many times but I am still unsuccessful. Anyone have any ideas? I would be very grateful.
the problem is that I don't know how to load only one line at a time, wait until someone presses the r/b key, evaluate the answer and only then show the next question. It also dont show the score correctly.
I have tried to change it many times but I am still unsuccessful. Anyone have any ideas? I would be very grateful.
import tkinter import random import keyboard root = tkinter.Tk() root.title("Quiz Game") canvas = tkinter.Canvas(root, width=1500, height=800, bg = "lightgreen") canvas.pack() x = 750 y = 400 def score(score_red,score_blue): canvas.create_text(x+500,y-230, text=score_blue, fill = "white", font = "Arial 30") canvas.create_text(x-400,y-230, text=score_red, fill = "white", font = "Arial 30") def mythfact(): canvas.create_text(x,y-250, text = "1", font = "Arial 40") def mytus(): canvas.create_rectangle(x-700,y-50,x-10,y+200, fill = "pink") canvas.create_text(x-350,y+80, text = "MYTH", font = "Arial 60") def fakt(): canvas.create_rectangle(x+10,y-50,x+700,y+200, fill = "pink") canvas.create_text(x+350,y+80, text = "FACT", font = "Arial 60") mytus() fakt() mythfact() def check(): global score_red, score_blue f = open("fakt_mytus.txt", "r", encoding = "utf-8") for i,row in enumerate(f): g = row.split("=") statement = canvas.create_text(x,y-70, text = g[0], font = "Arial 30") def klik_1(suradnice): xx = suradnice.x yy = suradnice.y score(score_red,score_blue) ## if keyboard.read_key() == "b" or "a": if x-700<xx<x-10 and y-50<yy<y+200: if (g[1]=="mytus"): right = canvas.create_text(x,y+250,text= "right", font = "Arial 60") canvas.update() canvas.after(1000) canvas.delete(right) ## if keyboard.read_key() == "b": ## score_blue = score_blue + 10 ## if keyboard.read_key() == "r": ## score_red = score_red +10 else: wrong = canvas.create_text(x,y, text = "wrong", font = "Arial 60") canvas.update() canvas.after(1000) canvas.delete(wrong) if x+10<xx<x+700 and y-50 <yy<y+200: if (g[1]=="fakt"): right = canvas.create_text(x,y,text="rigth", font = "Arial 60") canvas.update() canvas.after(1000) canvas.delete(right) ## if keyboard.read_key() == "b": ## score_blue = score_blue + 10 ## if keyboard.read_key() == "r": ## score_red = score_red +10 else: wrong = canvas.create_text(x,y,text = "wrong", font = "Arial 60") canvas.update() canvas.after(1000) canvas.delete(wrong) canvas.bind("<Button-1>", klik_1) check() score_red = 0 score_blue = 0 root.mainloop()
Attached Files