Skip to content

Instantly share code, notes, and snippets.

@pleabargain
Created February 9, 2023 13:21
Show Gist options
  • Save pleabargain/dd0d92517a3684617666bcd0e7b0e337 to your computer and use it in GitHub Desktop.
Save pleabargain/dd0d92517a3684617666bcd0e7b0e337 to your computer and use it in GitHub Desktop.
google sheets talking to a flet app credit: https://www.youtube.com/@databeliever
# watch the series to see how the whole thing is assembled
#source https://www.youtube.com/watch?v=tQprNCwjMlk&t=71s
# credit https://www.youtube.com/@databeliever
import gspread
import random
from datetime import datetime
import flet as ft
from time import sleep
def main(page: ft.Page):
page.title = "Quiz Game"
page.window_height = 500
page.window_width = 500
page.window_center()
page.update()
gc = gspread.service_account("secret_key.json")
sh = gc.open("Countries and capitals")
wk = sh.worksheet("Data")
global points
points = 0
today = datetime.today()
day = today.strftime("%Y-%m-%d")
time_hm = today.strftime("%H:%M")
# num_rows = len(wk.col_values(1))
num_rows = 10
logs = sh.worksheet("Logs")
def get_random():
while True:
random_row = random.randint(2, num_rows)
if wk.cell(random_row, 6).value != str(day):
return random_row
def no_tests(e):
global tests
tests = txt_name.value
if tests == "":
txt_name.error_text = "Give a number"
page.update()
elif not int(tests.isdigit()):
txt_name.error_text = "Wrong number"
page.update()
else:
tests = int(txt_name.value)
id = logs.acell("A2").value or 0
logs.insert_row([int(id) + 1, day, time_hm, tests], 2)
do_test(tests, "True")
def do_test(tests, play):
while int(tests) > 0 and play:
global capital
global country
play = False
tests -= 1
random_row = get_random()
country = wk.cell(random_row, 1).value
capital = wk.cell(random_row, 2).value
no_of_tests = wk.cell(random_row, 4).value or 0
correct = wk.cell(random_row, 5).value or 0
wk.update_cell(random_row, 6, str(day))
wk.update_cell(random_row, 4, int(no_of_tests) + 1)
def check_answer(e):
global capital
global points
if (user_answer.value == capital):
page.add(ft.Text(
value=f"✔ Correct", color="green"))
points += 1
wk.update_cell(random_row, 5, int(correct) + 1)
else:
page.add(ft.Text(
value=f"❌ Wrong. Correct answer is {capital}", color="red"))
sleep(.5)
if int(tests) < 1:
page.clean()
t = ft.Text(
value=f"🏆 You have {points} points. 🏆", color="blue", size=20, text_align="center", weight="bold")
page.controls.append(t)
page.update()
logs.update_cell(2, 5, points)
sleep(.5)
else:
do_test(int(tests), "True")
page.clean()
t = ft.Text(
value=f"What is the capital of {country!r}?", color="blue", size=20, text_align="center")
page.controls.append(t)
user_answer = ft.TextField(
hint_text="Answer", width=300, autofocus=True, on_submit=check_answer, text_size=20)
page.add(
user_answer, ft.ElevatedButton("Check", on_click=check_answer))
txt_name = ft.TextField(
hint_text="How many tests?", width=300, autofocus=True, on_submit=no_tests, text_size=20)
page.add(txt_name, ft.ElevatedButton(
"Enter", on_click=no_tests))
# ft.app(target=main, view=ft.WEB_BROWSER)
ft.app(target=main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment