2019-09-02 11:26:04 +02:00
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import random
2019-09-03 18:45:00 +02:00
import time
2019-09-02 11:26:04 +02:00
2019-09-03 18:45:00 +02:00
def swapSides ( ) : # prohodi sloupce v souboru testu (a tim i zmeni co je otazka a co odpoved)
2019-09-02 11:26:04 +02:00
global test
2019-09-03 12:49:38 +02:00
global score
2019-09-02 11:26:04 +02:00
2019-09-02 12:55:41 +02:00
temp = test [ 0 ]
test [ 0 ] = test [ 1 ]
test [ 1 ] = temp
2019-09-02 11:26:04 +02:00
2019-09-03 12:49:38 +02:00
score = 0
currentScore . set ( ' Score: %s ' % score )
2019-09-02 11:26:04 +02:00
newQuestion ( )
2019-09-03 18:45:00 +02:00
def saveTest ( ) : # ulozi soubor testu
2019-09-02 11:26:04 +02:00
print ( test )
2019-09-03 18:45:00 +02:00
print ( filepath )
2019-09-02 12:36:18 +02:00
with open ( filepath , mode = ' w ' , encoding = " UTF-8 " ) as file :
2019-09-02 12:55:41 +02:00
file . write ( str ( test [ 2 ] ) + ' \n ' )
for i in range ( len ( test [ 0 ] ) ) :
for j in range ( len ( test [ 0 ] [ i ] ) ) :
file . write ( test [ 0 ] [ i ] [ j ] )
if j < len ( test [ 0 ] [ i ] ) - 1 :
2019-09-02 11:26:04 +02:00
file . write ( ' , ' )
file . write ( ' / ' )
2019-09-02 12:55:41 +02:00
for j in range ( len ( test [ 1 ] [ i ] ) ) :
file . write ( test [ 1 ] [ i ] [ j ] )
if j < len ( test [ 1 ] [ i ] ) - 1 :
2019-09-02 11:26:04 +02:00
file . write ( ' , ' )
file . write ( ' \n ' )
2019-09-03 18:45:00 +02:00
def newQuestion ( ) : # nastavi novou otazku
2019-09-02 11:26:04 +02:00
global questionPlace
2019-09-03 18:45:00 +02:00
global timeAsked
2019-09-02 12:55:41 +02:00
questionPlace = random . randint ( 0 , len ( test [ 0 ] ) - 1 )
newQuestion = test [ 0 ] [ questionPlace ] [ 0 ]
2019-09-02 11:26:04 +02:00
question . set ( newQuestion )
2019-09-03 18:45:00 +02:00
timeAsked = time . time ( ) - 1
2019-09-02 11:26:04 +02:00
2019-09-03 18:45:00 +02:00
def on_closing ( ) : #zobrazi se pri zavirani programu, pokud se uzivatel rozhodne program zavrit, tak prvne ulozi highscore
2019-09-02 11:26:04 +02:00
if messagebox . askokcancel ( " Quit " , " Do you want to quit? " ) :
saveTest ( )
root . destroy ( )
2019-09-03 18:45:00 +02:00
def submitAnswer ( uselessArgument ) : # precte pole pro zadani odpovedi, zhodnoti ji, a vhodnym zpusobem zmeni score a highscore
2019-09-02 11:26:04 +02:00
global score
2019-10-21 16:27:23 +02:00
for i in test [ 1 ] [ questionPlace ] :
if answerEntry . get ( ) . lower ( ) == i . lower ( ) :
score + = timeMultiplier
currentScore . set ( ' Score: %s ' % ( score ) )
else :
score = 0
currentScore . set ( ' Score: %s ' % ( score ) )
if score > test [ 2 ] :
test [ 2 ] = score
highScore . set ( ' High score: %s ' % ( test [ 2 ] ) )
2019-09-02 11:26:04 +02:00
answerEntry . delete ( 0 , ' end ' )
newQuestion ( )
2019-09-03 18:45:00 +02:00
currentScore . set ( ' Score: %s ' % round ( score , 2 ) )
highScore . set ( ' High score: %s ' % round ( test [ 2 ] , 2 ) )
2019-09-02 11:26:04 +02:00
2019-09-03 18:45:00 +02:00
def formatTest ( filepath ) : # precte soubor a formatuje jej do listu test
2019-09-02 11:26:04 +02:00
global score
2019-09-02 12:36:18 +02:00
with open ( filepath , encoding = " UTF-8 " ) as file :
2019-09-02 11:26:04 +02:00
file = file . readlines ( )
sloupec1 = [ ]
sloupec2 = [ ]
2019-09-03 18:45:00 +02:00
highScore = float ( file [ 0 ] . strip ( ' \n ' ) )
2019-09-02 11:26:04 +02:00
score = 0
2019-09-02 12:55:41 +02:00
for line in file [ 1 : ] :
2019-09-02 11:26:04 +02:00
currentline = line . strip ( ' \n ' )
currentline = currentline . split ( ' / ' )
sloupec1 . append ( currentline [ 0 ] . split ( ' , ' ) )
sloupec2 . append ( currentline [ 1 ] . split ( ' , ' ) )
2019-09-02 12:55:41 +02:00
return sloupec1 , sloupec2 , highScore
2019-09-02 11:26:04 +02:00
2019-09-03 18:45:00 +02:00
def getExtension ( filename ) : # vrati posledni ctyry znaky jmena souboru
2019-09-02 11:26:04 +02:00
return filename [ - 4 : None ]
2019-09-03 18:45:00 +02:00
def openTest ( ) : # ziska adresu testu a nastavi seznam test aby byl novy test, pote reqestuje novou otazku
2019-09-02 11:26:04 +02:00
global test
global filepath
saveTest ( )
gotfile = False
filepath = None
while not gotfile :
filepath = filedialog . askopenfilename ( title = ' Select test file ' )
if getExtension ( filepath ) == ' .tst ' :
gotfile = True
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 ) ) )
test = list ( formatTest ( filepath ) )
2019-09-03 18:45:00 +02:00
#newQuestion()
submitAnswer ( 0 )
2019-09-02 11:26:04 +02:00
2019-09-03 18:45:00 +02:00
def makeMenu ( root ) : # vytvori menu
2019-09-02 11:26:04 +02:00
menu = Menu ( root )
root . config ( menu = menu )
menu . add_command ( label = ' Open ' , command = openTest )
menu . add_command ( label = ' Switch sides ' , command = swapSides )
return menu
2019-09-03 18:45:00 +02:00
def makeRoot ( ) : # vytvori hlavni okno
2019-09-02 11:26:04 +02:00
root = Tk ( )
root . geometry ( ' 500x400 ' )
root . title ( ' Test Me ' )
root . configure ( bg = " gray " )
2019-09-02 12:36:18 +02:00
root . iconbitmap ( ' programfiles/icon.ico ' )
2019-09-02 11:26:04 +02:00
return root
root = makeRoot ( )
2019-09-03 18:45:00 +02:00
2019-09-02 11:26:04 +02:00
filepath = ' programfiles/default.tst '
test = list ( formatTest ( ' programfiles/default.tst ' ) )
questionPlace = 0
2019-09-03 18:45:00 +02:00
score = 0.0
2019-09-02 11:26:04 +02:00
currentScore = StringVar ( )
highScore = StringVar ( )
question = StringVar ( )
2019-09-03 18:45:00 +02:00
timeMultiplier = 1
2019-09-02 11:26:04 +02:00
anotherSpacingLabel = Label ( bg = ' gray ' , padx = 5 )
currentScoreLabel = Label ( root , textvariable = currentScore , bg = ' gray ' )
highScoreLabel = Label ( root , textvariable = highScore , bg = ' gray ' )
questionLabel = Label ( root , textvariable = question , width = 30 )
spacingLabel = Label ( bg = ' gray ' , padx = 10 )
answerEntry = Entry ( root , width = 30 )
2019-09-02 12:55:41 +02:00
answerButton = Button ( root , text = ' submit ' , command = lambda : submitAnswer ( 0 ) )
2019-09-03 18:45:00 +02:00
timeMultiplierLabel = Label ( root , text = ' Your time bonus is: %s ' % timeMultiplier , bg = ' gray ' )
2019-09-02 11:26:04 +02:00
anotherSpacingLabel . grid ( row = 0 , column = 0 )
currentScoreLabel . grid ( row = 0 , column = 1 , columnspan = 2 , sticky = W )
highScoreLabel . grid ( row = 0 , column = 4 , columnspan = 2 , sticky = E )
questionLabel . grid ( row = 1 , column = 2 , sticky = W )
spacingLabel . grid ( row = 1 , column = 3 )
answerEntry . grid ( row = 1 , column = 4 , sticky = E )
answerButton . grid ( row = 1 , column = 5 , sticky = W )
2019-09-03 18:45:00 +02:00
timeMultiplierLabel . grid ( row = 2 , column = 1 , columnspan = 5 )
2019-09-02 11:26:04 +02:00
currentScore . set ( ' Score: %s ' % ( score ) )
2019-09-03 18:47:08 +02:00
highScore . set ( ' High score: %s ' % ( test [ 2 ] ) )
2019-09-02 11:26:04 +02:00
question . set ( ' question will appear here ' )
root . protocol ( " WM_DELETE_WINDOW " , on_closing )
answerEntry . bind ( ' <Return> ' , submitAnswer )
menu = makeMenu ( root )
2019-09-03 18:45:00 +02:00
running = True
while ( running ) :
try :
if time . time ( ) - timeAsked > = 100 :
timeMultiplier = 1
else :
timeMultiplier = ( 1 / ( time . time ( ) - timeAsked ) * 100 )
timeMultiplierLabel . configure ( text = ' Your time bonus is: %s ' % round ( timeMultiplier , 2 ) )
except :
None
root . update ( )