Skip to content

Commit

Permalink
Added quiz game example with Tk
Browse files Browse the repository at this point in the history
  • Loading branch information
YAtOff committed Mar 18, 2018
1 parent fc9c38c commit 510494e
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
61 changes: 61 additions & 0 deletions examples/quiz/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import random

from tkinter import *
from tkinter import ttk

from questions import questions

root = Tk()
root.title("Викторина")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)


def get_question():
unanswered_questions = [q for q in questions if 'answered' not in q]
if len(unanswered_questions) == 0:
return None
question = random.choice(unanswered_questions)
return question


def play():
for widget in mainframe.winfo_children():
widget.destroy()

question = get_question()
if question is None:
ttk.Label(mainframe, text='Край!').grid(column=1, columnspan=2, sticky=(W, E))
return

choosed_answer = StringVar()
result = StringVar()

def submit_answer():
if choosed_answer.get() == question['answer']:
result.set('Вярно!')
else:
result.set('Грешно!')
question['answered'] = True
button_next.configure(state=NORMAL)

ttk.Label(mainframe, text=question['question']).grid(column=1, columnspan=2, sticky=(W, E))

for answer in question['answers']:
ttk.Radiobutton(mainframe, text=answer, variable=choosed_answer , value=answer)\
.grid(column=1, sticky=W)

ttk.Button(mainframe, text="Отговори", command=submit_answer)\
.grid(column=2, sticky=W)

ttk.Label(mainframe, textvariable=result).grid(column=1, sticky=(W, E))
button_next = ttk.Button(mainframe, text="Следващ", command=play, state=DISABLED)
button_next.grid(column=2, sticky=W)

for child in mainframe.winfo_children():
child.grid_configure(padx=5, pady=5)

play()
root.mainloop()
77 changes: 77 additions & 0 deletions examples/quiz/questions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
questions = [
{
"question": "Коя е столицата на България?",
"answers": ["Пловдив", "София", "Варна"],
"answer": "София",
},
{
"question": "Кое от изброените е име на планина?",
"answers": ["Пирин", "Марица", "Видин", "Леденика"],
"answer": "Пирин",
},
{
"question": "Колко букви има в българската азбука?",
"answers": ["31", "29", "30", "28"],
"answer": "30",
},
{
"question": "Колко е (11-7)+2?",
"answers": ["2", "6", "9", "7"],
"answer": "6",

},
{
"question": "В какъв цвят се боядисва първото?",
"answers": ["син", "жълт", "червен", "зелен"],
"answer": "червен",

},
{
"question": "Колко гласни има в азбуката?",
"answers": ["26", "6", "4", "24"],
"answer": "6",

},
{
"question": "Кой български автор е написал 'Аз съм българче'?",
"answers": ["Йордан Йовков", "Иван Вазов", "Христо Ботев", "Дора изледователката"],
"answer": "Иван Вазов",

},
{
"question": "Колко букви и звука има думата 'щъркел'?",
"answers": ["6 букви, 6 звука", "7 букви, 7 звука", "6 букви, 7 звука", "7 букви, 6 звука"],
"answer": "6 букви, 7 звука",

},
{
"question": "Коя от думите е написана правилно?",
"answers": ["збор", "бръмбър", "ябалка", "куршум"],
"answer": "курщум",

},
{
"question": "През кой сезон птиците отлитат на юг?",
"answers": ["пролет", "зима", "лято", "есен"],
"answer": "есен",

},
{
"question": "Коя от думите е съшествително име?",
"answers": ["къща", "светъл", "играя", "вкусен"],
"answer": "къща",

},
{
"question": "Колко е 3+4*5?",
"answers": ["35", "12", "23", "19"],
"answer": "23",

},
{
"question": "В коя професия хората лекуват домашните любимци?",
"answers": ["ветеринар", "полицай", "учител", "адвокат"],
"answer": "ветеринар",

}
]

0 comments on commit 510494e

Please sign in to comment.