Python Sample
This application reads a specially formatted text file full of words and their meaning and displays each and every word
for a duration selected by the user. This is very much like a flash card application. It's called JapaneseAutoPrimer, because I wrote it for Japanese, but any language works just as well.
JapaneseAutoPrimer.py Sample:
from tkinter import *
from threading import Thread
import time
exitThread = False
dur = 10.0
pause = False
range_start=0
range_end = 100
range_change = False
position_change = 0 #the value indicates how much forward or backward to move in the list
wrds = []
def display():
global japan_label, romanji_label, english_label, dur, range_start, range_end, range_change
global wrds
global position_change
fo = open("JapaneseTxt.txt", 'r',encoding="utf-8")
for line in fo:
df = []
tmp = line.split('>')
if len(tmp) != 3:
continue
for v in tmp:
df.append(v.strip())
wrds.append(df)
fo.close()
range_end = len(wrds)
print("Range is from ", str(range_start), " to ", str(range_end))
print("")
while(exitThread == False):
i = range_start
while i < range_end:
v = wrds[i]
#print(v)
japan_label.configure(text=v[0])
#v2 = "romanji: " + v
romanji_label.configure(text=v[1])
#v2 = "ランプ english " + v
english_label.configure(text=v[2])
#time.sleep(5)
t1 = time.time()
while(True):
t2 = time.time()
if t2 - t1 >= dur and pause == False:
break
if range_change:
break
if exitThread:
break
if position_change != 0:
break
if position_change != 0:
print("Original i = ",str(i))
i = position_change + i
print("New i = ",str(i))
position_change = 0
if i > (range_end - 1) or i < range_start:
i = range_start
continue
if range_change:
range_change = False
break
if exitThread:
break
i += 1
def increment_count():
global count_value, japan_label
japan_label.configure(text=stuff[count_value])
count_value = count_value + 1
if count_value >= len(stuff):
count_value = 0
def pause_func():
global pause
if pause:
pause = False
pause_button.configure(text="Pause")
else:
pause = True
pause_button.configure(text="Continue")
def command_func():
global ent, dur, range_start, range_end, range_change
global wrds
global position_change
v = ent.get()
vals = v.split(" ")
n = len(vals)
if n == 0:
return
if vals[0].lower() == "range" and n == 3:
if vals[1].isdigit() and vals[2].isdigit():
t1 = int(vals[1])
t2 = int(vals[2])
if t1 < 0:
t1 = 0
if t2 > len(wrds):
t2 = len(wrds)
if t1 >= t2:
return
range_start = t1
range_end = t2
range_change = True
print("New Range is: ", str(range_start), " to ", str(range_end))
elif vals[0].lower() == "dur" and n == 2:
if vals[1].isdigit():
dur = int(vals[1])
print("New Duration is ", vals[1], " seconds")
elif vals[0].lower() == "move" and n == 2:
v = vals[1]
neg = False
if vals[1][0] == '-':
v = v[1:]
neg = True
if v.isdigit():
position_change = int(vals[1])
print("Move by ", vals[1])
elif vals[0].lower() == "help" and n == 1:
print("Commands:")
print("help -- the command just typed! Provides help.")
print("dur time_in_seconds")
print("range startIndex endIndex")
print("move +/-int value")
main_window = Tk()
main_window.wm_title("Japanese Auto Primer")
japan_label = Label(main_window, text="Count: 0", foreground='red', font='Times 20', relief='groove',
borderwidth=3)
romanji_label = Label(main_window, text="Testing: 1", foreground='blue', font='Times 20', relief='groove',
borderwidth=3)
english_label = Label(main_window, text="This is English!", foreground='green', font='Times 20', relief='groove',
borderwidth=3)
japan_label.grid(row=0, column=1)
romanji_label.grid(row=1, column=1)
english_label.grid(row=2,column=1)
count_value = 0
pause_button = Button(main_window, text="Pause", command=pause_func)
pause_button.grid(row=0, column=0)
quit_button = Button(main_window, text="Quit", command=main_window.destroy)
quit_button.grid(row=1, column=0)
ent = Entry(main_window)
ent.grid(row=2,column=0)
command_button = Button(main_window, text="Run Command", command=command_func)
command_button.grid(row=3, column=0)
t = Thread(target=display)
t.start()
mainloop()
exitThread = True