-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reference 3.0.py
283 lines (232 loc) · 9.56 KB
/
Reference 3.0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""
Book of Mormon Reference Converter 3.0
Programmer: Ron Smith
Date: October 9, 2020
Converts scripture references from RLDS to LDS and vice versa.
With GUI implemented as class.
New in version 3.0:
Redesgned interface: refEntry is Textbox; Vertical layout
"""
from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox
from reference import *
"""Globals:
RLDS, LDS: Tables with (book,chaper:verse) triples
BOM: BookNames,StyleList, Abbreviations, etc.
"""
class ReferenceApp(Frame):
def __init__(self,master=None):
Frame.__init__(self,master)
self.master.title("Book or Mormon Reference Translation")
self.pack()
#Control Variables
self.ref = Reference("RLDS")
self.transRef = Reference("LDS")
self.refEntry = Entry(None) #Redefined in CreateWidgets
self.inVar = StringVar() #Controls refEntry
self.outVar = StringVar() #Controls refOut
self.denomination="" #'LDS' or 'RLDS' defined later
self.rbox = None #Combobox defined later
self.lbox = None #Combobox defined later
self.rldstyle = StringVar() #Combobox control
self.ldstyle = StringVar() #Combobox control
self.createWidgets()
#initialize variables
#Entry/Output
self.denomination = "RLDS"
self.inVar.set("(RLDS) ")
self.refEntry.icursor(END)
self.outVar.set("(LDS) ")
#Style
self.rldstyle.set("RLDS 1908")
self.ldstyle.set("LDS 1982")
self.rldstyle.trace_add(
"write",lambda a,b,c,x="RLDS":self.applyStyle(x)) #Magic
self.ldstyle.trace_add(
"write",lambda a,b,c,x="LDS":self.applyStyle(x)) #Magic
def createWidgets(self):
#set up Frames: endtryFrame,styleFrame,bookFrame,keyFrame
ioFrame = Frame(self,)
ioFrame.grid(sticky = NSEW)
inFrame = Frame(ioFrame,border = 10)
inFrame.grid(row=0,column=0,sticky = EW)
outFrame = Frame(ioFrame,border = 10)
outFrame.grid(row=0,column=1,sticky = EW)
styleFrame = Frame(self,border=10)
styleFrame.grid(sticky = N)
self.lstyleFrame=Frame(styleFrame) #These are moved in switch()
self.rstyleFrame=Frame(styleFrame)
bookFrame = Frame(self,border=10)
bookFrame.grid()
keyFrame = Frame(self,border = 10)
keyFrame.grid()
#Entry widget
self.refEntry = Entry(inFrame,width=35,
textvariable = self.inVar,)
self.refEntry.grid(sticky = N)
self.refEntry.bind("<Return>",self.submit)
#Output widget
self.refOut = Entry(outFrame,width = 35,
textvariable = self.outVar,state="readonly")
self.refOut.grid(sticky = NSEW)
#Style Frame
style = Style()
style.configure("BigTimesText.TButton",font=('times',20,'bold'),
background = "White",)
style.configure("BigCalibriText.TButton",font=('calibri',20,'bold'),
background = "White",)
lbl = Label(self.rstyleFrame,text = "Style (RLDS)")
lbl.grid()
self.rbox = Combobox(self.rstyleFrame,state="readonly",
values = BOM.styleList,
textvariable = self.rldstyle,
width = 10, justify="center")
self.rbox.grid()
self.rstyleFrame.grid(column = 0)
lbl = Label(self.lstyleFrame,text = "Style (LDS)")
lbl.grid()
self.lbox = Combobox(self.lstyleFrame,state="readonly",
values = BOM.styleList,
textvariable = self.ldstyle,
width = 10, justify="center")
self.lbox.grid(row=1)
self.lstyleFrame.grid(row=0,column=2)
btn = Button(styleFrame,text = "<=>",style = "BigTimesText.TButton",
command = self.switchIO,)
btn.grid(row=0,column=1,sticky = S)
#Book buttons
bookStr="1 Nephi,2 Nephi,Jacob,Enos,Jarom,Omni,W of M," + \
"Mosiah,Alma,Helaman,3 Nephi,4 Nephi,Mormon,Ether,Moroni"
bookList = bookStr.split(",")
for j in range(3):
for i in range(5):
n = 5*j+i
btn = Button(bookFrame,text=bookList[n],
command=lambda x=n+1:self.abbreviate(x))
btn.grid(row=i,column=j,sticky = W)
#Set up keys
for i in range(3):
for j in range(3):
n = str(3*i+j+1)
btn = Button(keyFrame,text = n, width=5,
command = lambda x=n:self.entryInsert(x),)
btn.grid(row=i,column=j)
btn = Button(keyFrame,text = "0",
command = lambda x="0":self.entryInsert(x),
width=5,)
btn.grid(row=3,column=1)
btn = Button(keyFrame,text = ":",style = "BigTimesText.TButton",
command = lambda x=":":self.entryInsert(x),
width=4,)
btn.grid(row=4,column=0)
btn = Button(keyFrame,text = ", ",style = "BigTimesText.TButton",
command = lambda x=", ":self.entryInsert(x),
width=4,)
btn.grid(row=4,column=1)
btn = Button(keyFrame,text = "–",style = "BigTimesText.TButton",
command = lambda x="-":self.entryInsert(x),
width=4,)
btn.grid(row=4,column=2)
btn = Button(keyFrame,text = "Enter",style = "BigCalibriText.TButton",
command = self.submit,
width=5,)
btn.grid(row=5,column=1)
btn = Button(keyFrame,text = "Space",
command = lambda x=" ":self.entryInsert(x),
width=5,)
btn.grid(row=3,column=0)
btn = Button(keyFrame,text = "Delete",
command = self.entryDelete,
width=5,)
btn.grid(row=3,column=2)
#Button Control functions
def toggleDenomination(self):
return {"LDS":"RLDS","RLDS":"LDS"}[self.denomination]
def entryInsert(self,c):
self.refEntry.insert(INSERT,c)
self.refEntry.focus_set()
def entryDelete(self):
s = self.inVar.get()
n = self.refEntry.index(INSERT)
if n>0:
if not s[n-1].isalpha():
s=s[:n-1]+s[n:]
else:
while n>0 and s[n-1].isalpha():
s=s[:n-1]+s[n:]
n -= 1
self.inVar.set(s)
self.refEntry.icursor(n)
self.refEntry.focus_set()
def abbreviate(self,bookNum):
den = self.denomination
if den == "LDS":
style = self.lbox.current()
else:
style = self.rbox.current()
name = BOM.spell(bookNum,style)+" "
self.refEntry.insert(INSERT,name)
self.refEntry.focus_set()
def submit(self,*args): #sometimes an event is passed.
try:
ref = self.refEntry.get()
den,s = extractDenomination(ref)
if den:
newSource = Reference(den)
else:
newSource = self.ref.copy()
newSource.reset()
self.denomination = newSource.denomination #***
if s:
newSource.insert(s)
self.ref = newSource
self.trans = newSource.translate()
t = "({}) {}".format(self.trans.denomination,self.trans)
self.outVar.set(t)
self.alignDenomination(self.denomination)
except ValueError:
s = "One or more References is invalid.\nNo translation was made."
messagebox.showerror("Error", s)
def alignDenomination(self,den):
den = den.upper() #In case of hand entry
if den not in ["LDS","RLDS"]:
den = "RLDS" #Default to RLDS
if den != self.ref.denomination: #Switch ref/transRef
t = self.ref
self.ref = self.transRef
self.transRef = t
self.denomination = den
#Now make the Style boxes line up
if den == "RLDS":
self.rstyleFrame.grid(row=0,column=0)
self.lstyleFrame.grid(row=0,column=2)
else:
self.rstyleFrame.grid(row=0,column=2)
self.lstyleFrame.grid(row=0,column=0)
def switchIO(self):
#Switch refEntry, refOut control variables
t=self.inVar.get()
self.inVar.set(self.outVar.get())
self.outVar.set(t)
#Reset cursor positions
self.refEntry.icursor(END)
self.refOut.icursor(END)
#Switch denomination
self.denomination = self.toggleDenomination()
self.alignDenomination(self.denomination)
def applyStyle(self,*args):
"""Change book style in ref or trans"""
changeDen = args[0] #Updated Denomination
style = {"LDS":self.lbox.current(),"RLDS":self.rbox.current()}[changeDen]
if changeDen == self.ref.denomination:
s = BOM.bookStr(self.inVar.get(),style)
self.inVar.set(s)
else:
s = BOM.bookStr(self.outVar.get(),style)
self.outVar.set(s)
self.refEntry.focus_set()
def main():
ReferenceApp()
if __name__ == "__main__":
main()