bruh memento numero 2

This commit is contained in:
Zdenek Borovec 2019-09-02 11:26:04 +02:00
parent 9d8fb91f86
commit 730ebc242e
3 changed files with 172 additions and 172 deletions

View file

@ -1,3 +1,3 @@
EXAMPLE TEST SUBJECT 1/EXAMPLE TEST SUBJECT 2 EXAMPLE TEST SUBJECT 1/EXAMPLE TEST SUBJECT 2
1 0
question will appear here/question will appear here question will appear here/question will appear here

View file

@ -1,6 +1,6 @@
CESTINA/ANGLICTINA CESTINA/ANGLICTINA
13 0
apple/jablko apple/jablko
car,automobile/auto,automobil car,automobile/auto,automobil
chopper,helicopter/vrtulnik,helikoptera chopper,helicopter/vrtulnik,helikoptera
pear/hruska pear/hruska

View file

@ -1,165 +1,165 @@
from tkinter import * from tkinter import *
from tkinter import filedialog from tkinter import filedialog
from tkinter import messagebox from tkinter import messagebox
import random import random
def swapSides(): def swapSides():
global test global test
temp = test[2] temp = test[2]
test[2] = test[3] test[2] = test[3]
test[3] = temp test[3] = temp
newQuestion() newQuestion()
def saveTest(): def saveTest():
print(test) print(test)
with open(filepath, mode='w') as file: with open(filepath, mode='w') as file:
file.write(test[0]+'/'+test[1]) file.write(test[0]+'/'+test[1])
file.write(str(test[4])+'\n') file.write(str(test[4])+'\n')
for i in range(len(test[2])): for i in range(len(test[2])):
for j in range(len(test[2][i])): for j in range(len(test[2][i])):
file.write(test[2][i][j]) file.write(test[2][i][j])
if j < len(test[2][i])-1: if j < len(test[2][i])-1:
file.write(',') file.write(',')
file.write('/') file.write('/')
for j in range(len(test[3][i])): for j in range(len(test[3][i])):
file.write(test[3][i][j]) file.write(test[3][i][j])
if j < len(test[3][i])-1: if j < len(test[3][i])-1:
file.write(',') file.write(',')
file.write('\n') file.write('\n')
def newQuestion(): def newQuestion():
global questionPlace global questionPlace
questionPlace = random.randint(0, len(test[2])-1) questionPlace = random.randint(0, len(test[2])-1)
newQuestion = test[2][questionPlace][0] newQuestion = test[2][questionPlace][0]
question.set(newQuestion) question.set(newQuestion)
def on_closing(): def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit?"): if messagebox.askokcancel("Quit", "Do you want to quit?"):
saveTest() saveTest()
root.destroy() root.destroy()
def submitAnswer(uselessArgument): def submitAnswer(uselessArgument):
global score global score
if answerEntry.get().lower() in test[3] [questionPlace]: if answerEntry.get().lower() in test[3] [questionPlace]:
score += 1 score += 1
currentScore.set('Score: %s' % (score)) currentScore.set('Score: %s' % (score))
else: else:
score = 0 score = 0
currentScore.set('Score: %s' % (score)) currentScore.set('Score: %s' % (score))
if score > test[4]: if score > test[4]:
test[4] = score test[4] = score
highScore.set('High score: %d' % (test[4])) highScore.set('High score: %d' % (test[4]))
answerEntry.delete(0, 'end') answerEntry.delete(0, 'end')
newQuestion() newQuestion()
def formatTest(filepath): def formatTest(filepath):
global score global score
with open(filepath) as file: with open(filepath) as file:
file = file.readlines() file = file.readlines()
nazev1 = file[0].split('/')[0] nazev1 = file[0].split('/')[0]
nazev2 = file[0].split('/')[1] nazev2 = file[0].split('/')[1]
sloupec1 = [] sloupec1 = []
sloupec2 = [] sloupec2 = []
highScore = int(file[1].strip('\n')) highScore = int(file[1].strip('\n'))
score = 0 score = 0
for line in file[2:]: for line in file[2:]:
currentline = line.strip('\n') currentline = line.strip('\n')
currentline = currentline.split('/') currentline = currentline.split('/')
sloupec1.append(currentline[0].split(',')) sloupec1.append(currentline[0].split(','))
sloupec2.append(currentline[1].split(',')) sloupec2.append(currentline[1].split(','))
return nazev1, nazev2, sloupec1, sloupec2, highScore return nazev1, nazev2, sloupec1, sloupec2, highScore
def getExtension(filename): # returns the last four characters in a filename as a string def getExtension(filename): # returns the last four characters in a filename as a string
return filename[-4:None] return filename[-4:None]
def openTest(): def openTest():
global test global test
global filepath global filepath
saveTest() saveTest()
gotfile = False gotfile = False
filepath = None filepath = None
while not gotfile: while not gotfile:
filepath = filedialog.askopenfilename(title='Select test file') filepath = filedialog.askopenfilename(title='Select test file')
if getExtension(filepath) == '.tst': if getExtension(filepath) == '.tst':
gotfile = True gotfile = True
else: else:
messagebox.showinfo("ERROR: unrecognized file", "We are sorry but files with the '%s' appendix are not supported, we support only '.tst' files." % (getExtension(filepath))) messagebox.showinfo("ERROR: unrecognized file", "We are sorry but files with the '%s' appendix are not supported, we support only '.tst' files." % (getExtension(filepath)))
test = list(formatTest(filepath)) test = list(formatTest(filepath))
newQuestion() newQuestion()
currentScore.set('Score: %s' % score) currentScore.set('Score: %s' % score)
highScore.set('High score: %d' % test[4]) highScore.set('High score: %d' % test[4])
def makeMenu(root): def makeMenu(root):
menu = Menu(root) menu = Menu(root)
root.config(menu=menu) root.config(menu=menu)
menu.add_command(label='Open', command=openTest) menu.add_command(label='Open', command=openTest)
menu.add_command(label='Switch sides', command=swapSides) menu.add_command(label='Switch sides', command=swapSides)
return menu return menu
def makeRoot(): def makeRoot():
root = Tk() root = Tk()
root.geometry('500x400') root.geometry('500x400')
root.title('Test Me') root.title('Test Me')
root.configure(bg="gray") root.configure(bg="gray")
return root return root
root = makeRoot() root = makeRoot()
filepath = 'programfiles/default.tst' filepath = 'programfiles/default.tst'
test = list(formatTest('programfiles/default.tst')) test = list(formatTest('programfiles/default.tst'))
questionPlace = 0 questionPlace = 0
score = 0 score = 0
currentScore = StringVar() currentScore = StringVar()
highScore = StringVar() highScore = StringVar()
question = StringVar() question = StringVar()
timeMultiplier = StringVar() timeMultiplier = StringVar()
anotherSpacingLabel = Label(bg='gray', padx=5) anotherSpacingLabel = Label(bg='gray', padx=5)
currentScoreLabel = Label(root, textvariable=currentScore, bg='gray') currentScoreLabel = Label(root, textvariable=currentScore, bg='gray')
highScoreLabel = Label(root, textvariable=highScore, bg='gray') highScoreLabel = Label(root, textvariable=highScore, bg='gray')
questionLabel = Label(root, textvariable=question, width=30) questionLabel = Label(root, textvariable=question, width=30)
spacingLabel = Label(bg='gray', padx=10) spacingLabel = Label(bg='gray', padx=10)
answerEntry = Entry(root, width=30) answerEntry = Entry(root, width=30)
answerButton = Button(root, text='submit', command=submitAnswer) answerButton = Button(root, text='submit', command=submitAnswer)
timeMultiplierLabel = Label(root, textvariable=timeMultiplier, bg='gray') timeMultiplierLabel = Label(root, textvariable=timeMultiplier, bg='gray')
anotherSpacingLabel.grid(row=0, column=0) anotherSpacingLabel.grid(row=0, column=0)
currentScoreLabel.grid(row=0, column=1, columnspan=2, sticky=W) currentScoreLabel.grid(row=0, column=1, columnspan=2, sticky=W)
highScoreLabel.grid(row=0, column=4, columnspan=2, sticky=E) highScoreLabel.grid(row=0, column=4, columnspan=2, sticky=E)
questionLabel.grid(row=1, column=2, sticky=W) questionLabel.grid(row=1, column=2, sticky=W)
spacingLabel.grid(row=1, column=3) spacingLabel.grid(row=1, column=3)
answerEntry.grid(row=1, column=4, sticky=E) answerEntry.grid(row=1, column=4, sticky=E)
answerButton.grid(row=1, column=5, sticky=W) answerButton.grid(row=1, column=5, sticky=W)
timeMultiplierLabel.grid(row=2, column=1, columnspan=5) timeMultiplierLabel.grid(row=2, column=1, columnspan=5)
currentScore.set('Score: %s' %(score)) currentScore.set('Score: %s' %(score))
highScore.set('High score: %d' %(test[4])) highScore.set('High score: %d' %(test[4]))
question.set('question will appear here') question.set('question will appear here')
timeMultiplier.set('Time multiplier is: 0') timeMultiplier.set('Time multiplier is: 0')
root.protocol("WM_DELETE_WINDOW", on_closing) root.protocol("WM_DELETE_WINDOW", on_closing)
answerEntry.bind('<Return>', submitAnswer) answerEntry.bind('<Return>', submitAnswer)
menu = makeMenu(root) menu = makeMenu(root)
root.mainloop() root.mainloop()