@@ -8,7 +8,8 @@ def __init__(self):
88 super ().__init__ ()
99
1010 self .title ('Python Regex Practice' )
11- self .geometry ('800x600' )
11+ self .dimensions ()
12+ self .geometry (self .xy )
1213
1314 self .styling ()
1415
@@ -23,24 +24,34 @@ def __init__(self):
2324
2425 self .initialize ()
2526
27+ def dimensions (self ):
28+ self .xy = '800x650'
29+ self .question_width = 82
30+ self .question_wraplength = 650
31+ self .entry_width = 53
32+ self .test_width = 40
33+ 2634 def styling (self ):
2735 ttk .Style ().theme_use ('alt' )
2836
29- ttk .Style ().configure ('Q.TLabel' , foreground = '#a52a2a' )
37+ ttk .Style ().configure ('QA.TFrame' , borderwidth = 5 , relief = 'groove' )
38+ ttk .Style ().configure ('DF.TFrame' )
3039
40+ ttk .Style ().configure ('QA.TLabel' , foreground = '#a52a2a' )
3141 ttk .Style ().configure ('DF.TLabel' )
3242 ttk .Style ().configure ('LC.TLabel' , background = '#24ff24' )
3343 ttk .Style ().configure ('RC.TLabel' , background = '#ff2424' )
3444 ttk .Style ().configure ('CS.TLabel' , background = '#a3ffa3' )
3545 ttk .Style ().configure ('WS.TLabel' , background = '#ffa3a3' )
3646
3747 def create_question_frame (self ):
38- self .question_frame = ttk .Frame ()
39- self .question_frame .pack (side = tk .TOP , pady = 5 )
48+ self .question_frame = ttk .Frame (style = 'QA.TFrame' , padding = 15 )
49+ self .question_frame .pack (side = tk .TOP , pady = 15 )
4050
41- self .l_question = ttk .Label (self .question_frame , wraplength = 500 ,
51+ self .l_question = ttk .Label (self .question_frame , style = 'QA.TLabel' ,
4252 justify = 'left' , font = 'TkFixedFont' ,
43- style = 'Q.TLabel' )
53+ width = self .question_width ,
54+ wraplength = self .question_wraplength )
4455 self .l_question .pack ()
4556
4657 def create_pattern_frame (self ):
@@ -53,9 +64,9 @@ def create_pattern_frame(self):
5364
5465 self .user_pattern = tk .StringVar ()
5566 self .user_pattern .trace_add ('write' , self .update_display )
56- self .e_pattern = ttk .Entry (self .pattern_frame ,
67+ self .e_pattern = ttk .Entry (self .pattern_frame ,font = 'TkFixedFont' ,
5768 textvariable = self .user_pattern ,
58- width = 53 , font = 'TkFixedFont' )
69+ width = self . entry_width )
5970 self .e_pattern .grid (row = 0 , column = 1 )
6071
6172 def create_replace_frame (self ):
@@ -67,9 +78,9 @@ def create_replace_frame(self):
6778
6879 self .user_replace = tk .StringVar ()
6980 self .user_replace .trace_add ('write' , self .update_display )
70- self .e_replace = ttk .Entry (self .replace_frame ,
81+ self .e_replace = ttk .Entry (self .replace_frame ,font = 'TkFixedFont' ,
7182 textvariable = self .user_replace ,
72- width = 53 , font = 'TkFixedFont' )
83+ width = self . entry_width )
7384 self .e_replace .grid (row = 0 , column = 1 )
7485
7586 def create_flags_frame (self ):
@@ -106,28 +117,28 @@ def create_format_frame(self):
106117
107118 def create_test_frame (self ):
108119 self .test_frame = ttk .Frame ()
109- self .test_frame .pack ()
120+ self .test_frame .pack (pady = 5 )
110121
111122 def create_solution_frame (self ):
112- self .solution_frame = ttk .Frame ()
113- self .solution_frame .pack ()
123+ self .solution_frame = ttk .Frame (padding = 5 )
124+ self .solution_frame .pack (pady = 5 )
114125
115- self .l_solution = ttk .Label (self .solution_frame , font = 'TkFixedFont' )
116- self .l_solution .pack (pady = 10 )
126+ self .l_solution = ttk .Label (self .solution_frame , font = 'TkFixedFont' ,
127+ style = 'QA.TLabel' )
128+ self .l_solution .pack (padx = 10 , pady = 10 )
117129
118130 def create_button_frame (self ):
119131 self .button_frame = ttk .Frame ()
120- self .button_frame .pack ()
121- 122- ttk .Button (self .button_frame , text = 'Quit' , command = self .quit
123- ).pack (side = tk .LEFT )
132+ self .button_frame .pack (pady = 5 )
124133
125- ttk .Button (self .button_frame , text = 'Previous' ,
126- command = lambda : self .next (previous = True )
127- ).pack (side = tk .LEFT )
134+ buttons = {'Quit' : self .quit ,
135+ 'Solution' : self .solution ,
136+ 'Previous' : lambda : self .next (previous = True ),
137+ 'Next' : self .next }
128138
129- ttk .Button (self .button_frame , text = 'Next' , command = self .next
130- ).pack (side = tk .RIGHT )
139+ for t , c in buttons .items ():
140+ ttk .Button (self .button_frame , text = t , command = c
141+ ).pack (side = tk .LEFT , padx = 2 )
131142
132143 def initialize (self ):
133144 self .format = {0 : str , 1 : repr }
@@ -160,7 +171,7 @@ def initialize(self):
160171 def display_question (self , question ):
161172 def create_label (s ):
162173 return ttk .Label (self .test_frame , text = s ,
163- width = 40 , justify = tk .LEFT ,
174+ width = self . test_width , justify = tk .LEFT ,
164175 anchor = 'w' , font = 'TkFixedFont' ,
165176 borderwidth = 2 , relief = 'raised' ,
166177 padding = 10 , style = 'DF.TLabel' )
@@ -244,6 +255,7 @@ def next(self, previous=False):
244255
245256 self .user_pattern .set ('' )
246257 self .user_replace .set ('' )
258+ self .solution_frame ['style' ] = 'DF.TFrame'
247259 for label in self .l_test_strings :
248260 label .destroy ()
249261 self .l_solution ['text' ] = ''
@@ -301,8 +313,7 @@ def update_display(self, *_):
301313 row += 1
302314
303315 if self .correct_solution :
304- self .l_solution ['text' ] = f'Reference solution(s):\n { self .ref_solution } '
305- self .save_progress ()
316+ self .solution ()
306317
307318 def save_progress (self ):
308319 progress_key = str (self .question_idx )
@@ -322,6 +333,11 @@ def save_progress(self):
322333 with open (self .progress_file , 'w' ) as f :
323334 f .write (json .dumps (self .user_progress , indent = 4 ))
324335
336+ def solution (self ):
337+ self .l_solution ['text' ] = f'Reference solution(s):\n { self .ref_solution } '
338+ self .solution_frame ['style' ] = 'QA.TFrame'
339+ self .save_progress ()
340+ 325341 def quit (self ):
326342 self .save_progress ()
327343 self .destroy ()
0 commit comments