# -*- 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()
我正在使用Python上的西班牙语应用程序。我创建了测验(def quizEtoS
),它从字符串中随机选择一个问题和一组答案。我对所有问题使用相同的按钮(A,B,C,D)。但是,我不太确定如何创建某种功能,因此每当用户选择正确的答案时,会弹出另一个问题。感谢帮助!
答案 0 :(得分:1)
进口存在问题。您从Tkinter导入所有内容,然后将tkinter 导入为ttk ,最后从ttk导入所有内容。这让人感到困惑,我的Python设置的差异可以解释为什么我不能运行你的代码。
我收到来自ttk的错误,但是如果我将导入更改为仅Tkinter就可以了。
选择随机密钥
他们已经略微改变了字典在Python 3中的工作方式,dict_keys不再支持索引。您必须将键/值转换为列表。
quizQs_keys = list(quizQs.keys())
qOne = random.choice(quizQs_keys)
更多解释
我得到的第一个错误是:
TypeError: 'dict_keys' object does not support indexing
与Python 3 dicts有关。
修好后我得到:
TypeError: 'dict' object is not callable
关于功能:
def ansA():
quizAs(btnA)
这是因为您对dict键使用括号 什么时候应该使用方括号。例如。 quizAs [btnA]。
然后我得到:
KeyError: <tkinter.Button object .!button2>
因为您使用btnA作为引用的键 到顶部按钮。我想你想得到 索引正确答案的值,但是你 必须从quizEtoS()函数中保存正确的答案。
后退按钮对我来说没问题。