Reputation: 21
# -*- coding: utf-8 -*-
from Tkinter import *
import Tkinter as ttk
from ttk import *
import random
master = Tk()
master.title("Learn Spanish")
canvas = ttk.Canvas(master, width = 600, height = 600, bg = 'orange')
canvas.grid()
title = canvas.create_text(300, 100, text = 'Learn To Speak Spanish', font = ('Freestyle Script', 60), fill = 'firebrick')
text = canvas.create_text(295, 250, text = 'Welcome! Are you ready to \n learn Spanish today?', font = ('Freestyle Script', 35), fill = 'firebrick')
def nextScreen():
canvas.delete("all")
canvas.create_text(300, 125, text = 'Select one of the activities below to get started.', font = ('Freestyle Script', 30), fill = 'firebrick')
btnW = ttk.Button(master, bg='white', command=wordStudy, text='Word Study', font=('Freestyle Script', 30))
canvas.create_window(300, 300, width=400, height=90, window=btnW)
btnM = ttk.Button(master, bg='white', command=quizStoE, text='Quiz Yourself - Spanish to English', font=('Freestyle Script', 30))
canvas.create_window(300, 400, width=400, height=90, window=btnM)
btnQ = ttk.Button(master, bg='white', command=quizEtoS, text='Quiz Yourself - English to Spanish', font=('Freestyle Script', 30))
canvas.create_window(300, 500, width=400, height=90, window=btnQ)
def quizEtoS(): #I'm right here. randomly selects one of the questions
canvas.delete("all")
qOne = random.choice(quizQs.keys())
qOneText = canvas.create_text(250, 100, text = quizQs[qOne], font = ('Freestyle Script', 30), fill = 'firebrick')
if qOne == 0:
ansOne = canvas.create_text(350, 320, text=quizAs[0], font = ('Freestyle Script', 20), fill = 'white')
ansKey[0]
elif qOne == 1:
ansOne = canvas.create_text(350, 320, text=quizAs[1], font = ('Freestyle Script', 20), fill = 'white')
ansKey[1]
elif qOne == 2:
ansOne = canvas.create_text(350, 320, text=quizAs[2], font = ('Freestyle Script', 20), fill = 'white')
ansKey[2]
elif qOne == 3:
ansOne = canvas.create_text(350, 320, text=quizAs[3], font = ('Freestyle Script', 20), fill = 'white')
ansKey[3]
elif qOne == 4:
ansOne = canvas.create_text(350, 320, text=quizAs[4], font = ('Freestyle Script', 20), fill = 'white')
ansKey[4]
elif qOne == 5:
ansOne = canvas.create_text(350, 320, text=quizAs[5], font = ('Freestyle Script', 20), fill = 'white')
ansKey[5]
elif qOne == 6:
ansOne = canvas.create_text(350, 320, text=quizAs[6], font = ('Freestyle Script', 20), fill = 'white')
ansKey[6]
else:
ansOne = canvas.create_text(350, 320, text=quizQs[7], font = ('Freestyle Script', 20), fill = 'white')
ansKey[7]
canvas.create_window(150, 175, width=100, height=25, window=btnA)
canvas.create_window(150, 275, width=100, height=25, window=btnB)
canvas.create_window(150, 365, width=100, height=25, window=btnC)
canvas.create_window(150, 465, width=100, height=25, window=btnD)
def back(): # i'm not sure how to make back button or make this work!
canvas.delete("all")
nextScreen()
btn = ttk.Button(master, bg='white', command=back, text='Back', font=('Freestyle Script', 15))
canvas.create_window(100, 550, width=50, height=30, window=btn)
def quizStoE():
canvas.delete("all")
def wordStudy(event=None):
canvas.delete("all")
ttk.Canvas(master, width = 600, height = 600, bg = 'orange')
canvas.create_text(300, 70, text = 'Time to study! Press a button below to \n view the Spanish translation.', font = ('Freestyle Script', 30), fill = 'firebrick')
def textvariable(text): # translation of english pop up when buttons pressed
entry.delete(0,END)
entry.insert(0,text)
return
btn1 = ttk.Button(master, text="Good morning.", command=lambda:textvariable("Buenos dias."))
canvas.create_window(150, 200, width=125, height=30, window=btn1)
btn2 = ttk.Button(master, text="Good night.", command=lambda:textvariable("Buenas noches."))
canvas.create_window(150, 250, width=125, height=30, window=btn2)
btn3 = ttk.Button(master, text="Goodbye.", command=lambda:textvariable("Adiós."))
canvas.create_window(150, 300, width=125, height=30, window=btn3)
btn4 = ttk.Button(master, text="Hello.", command=lambda:textvariable("Hola."))
canvas.create_window(150, 350, width=125, height=30, window=btn4)
btn5 = ttk.Button(master, text="What is your name?", command=lambda:textvariable("¿Cómo te llamas?"))
canvas.create_window(450, 200, width=125, height=30, window=btn5)
btn6 = ttk.Button(master, text="How are you?", command=lambda:textvariable("¿Cómo estás?"))
canvas.create_window(450, 250, width=125, height=30, window=btn6)
btn7 = ttk.Button(master, text="Please.", command=lambda:textvariable("Por favor."))
canvas.create_window(450, 300, width=125, height=30, window=btn7)
btn8 = ttk.Button(master, text="Thank you.", command=lambda:textvariable("Gracias."))
canvas.create_window(450, 350, width=125, height=30, window=btn8)
entry = ttk.Entry(canvas, textvariable=textvariable, width=30)
entry.pack(padx=210, pady=(500, 80))
canvas.create_text(295, 485, text = 'Translation:', font = ('Freestyle Script', 25), fill = 'firebrick')
def back():
canvas.delete("all")
entry.pack_forget()
nextScreen()
btn = ttk.Button(master, bg='white', command=back, text='Back', font=('Freestyle Script', 15))
canvas.create_window(100, 550, width=50, height=30, window=btn)
btnStart = ttk.Button(master, bg='white', command=nextScreen, text='Start', font=('Freestyle Script', 35))
canvas.create_window(295, 400, width=400, height=90, window=btnStart)
def ansA():
quizAs(btnA)
def ansB():
quizAs(btnB)
def ansC():
quizAs(btnC)
def ansD():
quizAs(btnD)
quizQs = {0: 'Please.', 1:'Goodnight.', 2:'Good morning.', 3:'How are you?', 4:'Thank you.', 5:'Goodbye.', 6:'Hello.', 7:'What is your name?'}
quizAs = {0: 'Buenas noches.\n\n\nPor favor.\n\n\nGracias.\n\n\n¿Cómo te llamas?',
1: 'Buenos dias.\n\n\nHola.\n\n\nBuenas noches.\n\n\n¿Cómo estás?',
2: 'Por favor.\n\n\nGracias.\n\n\nAdios.\n\n\nBuenos dias.',
3: '¿Cómo estás?\n\n\nBuenas noches.\n\n\n¿Cómo te llamas?\n\n\nHola.',
4: 'Adios.\n\n\nGracias.\n\n\nHola.\n\n\nPor favor.',
5: 'Buenos dias.\n\n\n¿Cómo estás?\n\n\nBuenas noches.\n\n\nAdios.',
6: 'Hola.\n\n\nAdios.\n\n\nPor favor.\n\n\nGracias.',
7: '¿Cómo estás?\n\n\n¿Cómo te llamas?\n\n\nPor favor.\n\n\nBuenas noches.'}
ansKey = {0: ansB, 1: ansC, 2: ansD, 3: ansA, 4: ansB, 5: ansD, 6: ansA, 7: ansB}
btnA = ttk.Button(master, text='A', command = ansA)
btnB = ttk.Button(master, text='B', command = ansB)
btnC = ttk.Button(master, text='C', command = ansC)
btnD = ttk.Button(master, text='D', command = ansD)
mainloop()
I'm working on a Spanish app on Python. I created the quiz (def quizEtoS
), which randomly selects one of the questions and set of answers from a string. I use the same buttons for all questions (A, B, C, D). However, I'm not quite sure how to create some sort of function so whenever the user selects the correct answer, another question pops up. Help is appreciated!
Upvotes: 0
Views: 366
Reputation: 7176
There are issues with the imports. You import everything from Tkinter and then tkinter as ttk, and finally everything from ttk. This gets confusing and differences in my Python setup may explain why I can't run your code.
I get an error originating from ttk but if I change the imports to only Tkinter it works ok.
Selecting random key
They've changed slightly how dictionaries work in Python 3, dict_keys no longer support indexing. You'll have to convert keys / values to list.
quizQs_keys = list(quizQs.keys())
qOne = random.choice(quizQs_keys)
More explanations
The first error I got was the:
TypeError: 'dict_keys' object does not support indexing
which had to do with Python 3 dicts.
After fixing that I get:
TypeError: 'dict' object is not callable
on the function:
def ansA():
quizAs(btnA)
This is because you use parenthesis for the dict key when you should use square brackets. eg. quizAs[btnA].
Then I get:
KeyError: <tkinter.Button object .!button2>
because you are using btnA as key which is the reference to the top button. I guess that you would like to get the value with index of the correct answer, but then you must save the correct answer from the quizEtoS() function.
The back button works ok for me.
Upvotes: 1