11
11
from functools import partial
12
12
from WordSearch import WordSearch
13
13
14
+
14
15
class WordBoard :
15
16
"""
16
17
The WordBoard is the GUI for the WordSearch app. The grid is displayed
@@ -22,7 +23,7 @@ class WordBoard:
22
23
LaTeX and String form. This file also contains the solution.
23
24
"""
24
25
25
- def __init__ (self , size = 16 , color = ' yellow' , file_name = ' words.txt' , words = None ):
26
+ def __init__ (self , size = 16 , color = " yellow" , file_name = " words.txt" , words = None ):
26
27
"""
27
28
Initializes a WordBoard GUI.
28
29
@@ -35,16 +36,16 @@ def __init__(self, size=16, color='yellow', file_name='words.txt', words=None):
35
36
file_name: File that contains a list of words separated by newline (\n )
36
37
characters. Used to select random words for the Word Search.
37
38
Default is words.txt. If no file is given or words.txt is not included
38
- in the current directory and words is None, then a FileNotFoundError
39
+ in the current directory and words is None, then a FileNotFoundError
39
40
is raised.
40
41
words: A list of words entered by the user. Allows for customized word searches
41
42
with custom words. Default is None. If words is None, then words will be
42
43
randomly chosen from words.txt or the file given by file_name.
43
44
"""
44
- assert size > 3 , ' Size must be greater than 3'
45
+ assert size > 3 , " Size must be greater than 3"
45
46
46
47
root = tk .Tk ()
47
- root .title (' Word Search' )
48
+ root .title (" Word Search" )
48
49
root .resizable (width = False , height = False )
49
50
50
51
self ._word_grid = tk .Frame (root )
@@ -54,60 +55,72 @@ def __init__(self, size=16, color='yellow', file_name='words.txt', words=None):
54
55
self ._solution_shown = False
55
56
self ._size = size
56
57
self ._color = color
57
-
58
+
58
59
# If file_name is not present in the current directory, the
59
60
# New Words button will be disabled.
60
61
new_words_button = tk .DISABLED
61
62
if file_name in listdir (getcwd ()):
62
63
new_words_button = tk .NORMAL
63
- with open (file_name , mode = 'r' ) as f :
64
- self ._wordstxt = filter (None , f .read ().split ('\n ' ))
65
- self ._wordstxt = list (filter (lambda x : len (x ) < self ._size - 3 , self ._wordstxt ))
64
+ with open (file_name , mode = "r" ) as f :
65
+ self ._wordstxt = filter (None , f .read ().split ("\n " ))
66
+ self ._wordstxt = list (
67
+ filter (lambda x : len (x ) < self ._size - 3 , self ._wordstxt )
68
+ )
66
69
elif words is None :
67
- raise FileNotFoundError (f'''{ file_name } not present in the current directory. { file_name }
68
- must contain words separated by newline (\\ n) characters.''' )
70
+ raise FileNotFoundError (
71
+ f"""{ file_name } not present in the current directory. { file_name }
72
+ must contain words separated by newline (\\ n) characters."""
73
+ )
69
74
70
75
# Buttons that have been pushed
71
76
self ._pushed = set ()
72
-
77
+
73
78
self ._words = words
74
79
if self ._words is None :
75
80
self ._choose_random_words ()
76
81
else :
77
82
self ._words = list (set (map (str .upper , self ._words )))
78
-
83
+
79
84
# Create empty SIZExSIZE grid of buttons
80
85
self ._buttons = []
81
86
for i in range (self ._size ):
82
87
row = []
83
88
for j in range (self ._size ):
84
- row .append (tk .Button (
85
- self ._word_grid , padx = 5 , command = partial (self ._pressed , i , j )
86
- ))
87
- row [- 1 ].grid (row = i , column = j , sticky = 'ew' )
89
+ row .append (
90
+ tk .Button (
91
+ self ._word_grid , padx = 5 , command = partial (self ._pressed , i , j )
92
+ )
93
+ )
94
+ row [- 1 ].grid (row = i , column = j , sticky = "ew" )
88
95
self ._buttons .append (row )
89
96
90
97
# Menu Buttons at the top right of the GUI
91
98
# Menu Label
92
- tk .Label (
93
- self . _menu , text = 'Menu' , pady = 5 , font = tkFont . Font ( weight = 'bold' )
94
- ). grid ( row = 0 , column = 0 , columnspan = 2 , sticky = 'ew' )
99
+ tk .Label (self . _menu , text = "Menu" , pady = 5 , font = tkFont . Font ( weight = "bold" )). grid (
100
+ row = 0 , column = 0 , columnspan = 2 , sticky = "ew"
101
+ )
95
102
# "New Words" Button
96
103
tk .Button (
97
- self ._menu , text = 'New Words' , padx = 1 , pady = 1 ,
98
- state = new_words_button , command = self ._select_new
99
- ).grid (row = 1 , column = 0 , sticky = 'ew' )
104
+ self ._menu ,
105
+ text = "New Words" ,
106
+ padx = 1 ,
107
+ pady = 1 ,
108
+ state = new_words_button ,
109
+ command = self ._select_new ,
110
+ ).grid (row = 1 , column = 0 , sticky = "ew" )
100
111
# "Export" Button
101
- self ._export_button = tk .Button (self ._menu , text = 'Export' , padx = 1 , pady = 1 , command = self ._export )
102
- self ._export_button .grid (row = 1 , column = 1 , sticky = 'ew' )
112
+ self ._export_button = tk .Button (
113
+ self ._menu , text = "Export" , padx = 1 , pady = 1 , command = self ._export
114
+ )
115
+ self ._export_button .grid (row = 1 , column = 1 , sticky = "ew" )
103
116
# "Solution" Button
104
117
tk .Button (
105
- self ._menu , text = ' Solution' , padx = 1 , pady = 1 , command = self ._solution
106
- ).grid (row = 2 , column = 0 , sticky = 'ew' )
118
+ self ._menu , text = " Solution" , padx = 1 , pady = 1 , command = self ._solution
119
+ ).grid (row = 2 , column = 0 , sticky = "ew" )
107
120
# "Reshuffle" Button
108
121
tk .Button (
109
- self ._menu , text = ' Reshuffle' , padx = 1 , pady = 1 , command = self ._reshuffle
110
- ).grid (row = 2 , column = 1 , sticky = 'ew' )
122
+ self ._menu , text = " Reshuffle" , padx = 1 , pady = 1 , command = self ._reshuffle
123
+ ).grid (row = 2 , column = 1 , sticky = "ew" )
111
124
112
125
self ._labels = {}
113
126
self ._word_search = None
@@ -127,11 +140,17 @@ def _create_labels(self):
127
140
for label in self ._labels .values ():
128
141
label .destroy ()
129
142
self ._labels .clear ()
130
- self ._labels = {'Words' : tk .Label (self ._word_list , text = 'Words' , pady = 5 , font = tkFont .Font (weight = 'bold' ))}
131
- self ._labels ['Words' ].grid (row = 2 , column = 0 , columnspan = 2 )
143
+ self ._labels = {
144
+ "Words" : tk .Label (
145
+ self ._word_list , text = "Words" , pady = 5 , font = tkFont .Font (weight = "bold" )
146
+ )
147
+ }
148
+ self ._labels ["Words" ].grid (row = 2 , column = 0 , columnspan = 2 )
132
149
for i , word in enumerate (sorted (self ._words )):
133
- self ._labels [word ] = tk .Label (self ._word_list , text = word , anchor = 'w' )
134
- self ._labels [word ].grid (row = (i // 2 ) + (i % 1 ) + 3 , column = i % 2 , sticky = 'W' )
150
+ self ._labels [word ] = tk .Label (self ._word_list , text = word , anchor = "w" )
151
+ self ._labels [word ].grid (
152
+ row = (i // 2 ) + (i % 1 ) + 3 , column = i % 2 , sticky = "W"
153
+ )
135
154
136
155
def _choose_random_words (self ):
137
156
"""
@@ -153,12 +172,12 @@ def _pressed(self, row, col):
153
172
row, col: The row and column index of the button in the self._buttons
154
173
list
155
174
"""
156
- if self ._buttons [row ][col ].cget ('bg' ) == self ._color :
157
- self ._buttons [row ][col ].configure (bg = ' SystemButtonFace' )
158
- self ._pushed .remove ((self ._buttons [row ][col ].cget (' text' ), col , row ))
175
+ if self ._buttons [row ][col ].cget ("bg" ) == self ._color :
176
+ self ._buttons [row ][col ].configure (bg = " SystemButtonFace" )
177
+ self ._pushed .remove ((self ._buttons [row ][col ].cget (" text" ), col , row ))
159
178
else :
160
179
self ._buttons [row ][col ].configure (bg = self ._color )
161
- self ._pushed .add ((self ._buttons [row ][col ].cget (' text' ), col , row ))
180
+ self ._pushed .add ((self ._buttons [row ][col ].cget (" text" ), col , row ))
162
181
for word , coords in self ._word_search .solutions .items ():
163
182
if coords & self ._pushed == coords :
164
183
for _ , col , row in coords :
@@ -172,7 +191,7 @@ def _solution(self):
172
191
the words in the board.
173
192
"""
174
193
if self ._solution_shown :
175
- bg = ' SystemButtonFace'
194
+ bg = " SystemButtonFace"
176
195
state = tk .NORMAL
177
196
self ._pushed .clear ()
178
197
else :
@@ -190,7 +209,7 @@ def _reshuffle(self):
190
209
Command for the "Reshuffle" button. Uses the existing words and
191
210
creates a new word search board with the words in new locations.
192
211
"""
193
- self ._export_button .configure (text = ' Export' , state = tk .NORMAL )
212
+ self ._export_button .configure (text = " Export" , state = tk .NORMAL )
194
213
195
214
if self ._solution_shown :
196
215
self ._solution_shown = not self ._solution_shown
@@ -200,12 +219,13 @@ def _reshuffle(self):
200
219
for i in range (self ._size ):
201
220
for j in range (self ._size ):
202
221
self ._buttons [i ][j ].configure (
203
- text = self ._word_search .board [i ][j ], bg = 'SystemButtonFace' ,
204
- state = tk .NORMAL
222
+ text = self ._word_search .board [i ][j ],
223
+ bg = "SystemButtonFace" ,
224
+ state = tk .NORMAL ,
205
225
)
206
226
207
227
for label in self ._labels .values ():
208
- label .configure (bg = ' SystemButtonFace' )
228
+ label .configure (bg = " SystemButtonFace" )
209
229
210
230
def _select_new (self ):
211
231
"""
@@ -227,54 +247,62 @@ def _export(self):
227
247
self ._export_button .configure (state = tk .DISABLED )
228
248
229
249
number = 0
230
- file_name = ' WordSearch.html'
250
+ file_name = " WordSearch.html"
231
251
while file_name in listdir (getcwd ()):
232
252
number += 1
233
- file_name = f' WordSearch{ number } .html'
234
-
235
- with open (file_name , mode = 'w' ) as f :
236
- f .write (' <!DOCTYPE html>\n ' )
237
- f .write (' <html>\n ' )
238
- f .write (' <head>\n ' )
239
- f .write (' \t <title>Word Search</title>\n ' )
253
+ file_name = f" WordSearch{ number } .html"
254
+
255
+ with open (file_name , mode = "w" ) as f :
256
+ f .write (" <!DOCTYPE html>\n " )
257
+ f .write (" <html>\n " )
258
+ f .write (" <head>\n " )
259
+ f .write (" \t <title>Word Search</title>\n " )
240
260
# Scripts required to display LaTeX
241
261
f .write (
242
- ''' \t <script type="text/x-mathjax-config">
262
+ """ \t <script type="text/x-mathjax-config">
243
263
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\ \\ (','\\ \\ )']]}});
244
264
</script>
245
265
<script type="text/javascript"
246
266
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
247
- </script>\n '''
267
+ </script>\n """
248
268
)
249
- f .write (' </head>\n ' )
269
+ f .write (" </head>\n " )
250
270
f .write ('<h2 align="center">HTML Table WordSearch Grid:</h2>\n <br><br>' )
251
271
252
272
# Create HTML Table Version of the Word Search Grid
253
273
f .write ('<table align="center">\n ' )
254
274
for i in range (self ._size ):
255
- f .write (' \t <tr>\n \t \t ' )
275
+ f .write (" \t <tr>\n \t \t " )
256
276
for j in range (self ._size ):
257
- f .write (f' <td padding=1em>{ self ._word_search .board [i ][j ]} </td>' )
258
- f .write (' \t </tr>\n ' )
259
- f .write (' </table>\n <br><br>' )
277
+ f .write (f" <td padding=1em>{ self ._word_search .board [i ][j ]} </td>" )
278
+ f .write (" \t </tr>\n " )
279
+ f .write (" </table>\n <br><br>" )
260
280
261
281
# Create LaTeX Matrix of the Word Search Grid
262
282
f .write ('<h2 align="center">Latex WordSearch Grid:</h2>\n <br><br>' )
263
- f .write (' \\ begin{matrix}' )
264
- f .write (' \\ \\ ' .join ([' & ' .join (row ) for row in self ._word_search .board ]))
265
- f .write (' \\ end{matrix}\n <br><br>' )
283
+ f .write (" \\ begin{matrix}" )
284
+ f .write (" \\ \\ " .join ([" & " .join (row ) for row in self ._word_search .board ]))
285
+ f .write (" \\ end{matrix}\n <br><br>" )
266
286
f .write ('<h2 align="center">WordSearch Grid as String:</h2>\n <br><br>' )
267
287
268
288
# Create String Version of Word Search Grid. Each Row separated by ':::::'
269
289
f .write ('<div align="center">\n ' )
270
- f .write (' ::: ' .join (['' .join (self ._word_search .board [i ]) for i in range (self ._size )]))
271
- f .write ('\n <br><br>\n ' )
272
- f .write ('\n ' .join (['' .join (self ._word_search .board [i ]) for i in range (self ._size )]))
273
- f .write ('</div>\n ' )
290
+ f .write (
291
+ " ::: " .join (
292
+ ["" .join (self ._word_search .board [i ]) for i in range (self ._size )]
293
+ )
294
+ )
295
+ f .write ("\n <br><br>\n " )
296
+ f .write (
297
+ "\n " .join (
298
+ ["" .join (self ._word_search .board [i ]) for i in range (self ._size )]
299
+ )
300
+ )
301
+ f .write ("</div>\n " )
274
302
275
303
# Add solution to the bottom of the file
276
304
f .write ('\n <br><br><h2 align="center">Solution</h2><br><br>\n ' )
277
- f .write (' \\ begin{matrix}' )
305
+ f .write (" \\ begin{matrix}" )
278
306
279
307
coordinates = set ()
280
308
for coords in self ._word_search .solutions .values ():
@@ -288,16 +316,20 @@ def _export(self):
288
316
if (self ._word_search .board [i ][j ], j , i ) in coordinates :
289
317
row .append (self ._word_search .board [i ][j ])
290
318
else :
291
- row .append ('' )
319
+ row .append ("" )
292
320
board .append (row )
293
-
294
- f .write (' \\ \\ ' .join ([' & ' .join (row ) for row in board ]))
295
- f .write (' \\ end{matrix}' )
321
+
322
+ f .write (" \\ \\ " .join ([" & " .join (row ) for row in board ]))
323
+ f .write (" \\ end{matrix}" )
296
324
297
325
# Add words used in the Word Search and the size of the board
298
326
f .write ('\n <br><br><h2 align="center">Words</h2><br><br>\n ' )
299
- f .write (f'''<ul align="center"><li>{ '</li><li>' .join (self ._words )} </li></ul>\n ''' )
300
- f .write (f'\n <br><br><h2 align="center">SIZE: { self ._size } x{ self ._size } </h2><br><br>\n ' )
301
- f .write ('</html>' )
327
+ f .write (
328
+ f"""<ul align="center"><li>{ '</li><li>' .join (self ._words )} </li></ul>\n """
329
+ )
330
+ f .write (
331
+ f'\n <br><br><h2 align="center">SIZE: { self ._size } x{ self ._size } </h2><br><br>\n '
332
+ )
333
+ f .write ("</html>" )
302
334
303
- self ._export_button .configure (text = ' Exported' )
335
+ self ._export_button .configure (text = " Exported" )
0 commit comments