11import json
22import re
33import tkinter as tk
4+ from tkinter import ttk
45
56class Root (tk .Tk ):
67 def __init__ (self ):
@@ -9,6 +10,8 @@ def __init__(self):
910 self .title ('Python Regex Practice' )
1011 self .geometry ('800x600' )
1112
13+ self .styling ()
14+ 1215 self .create_question_frame ()
1316 self .create_pattern_frame ()
1417 self .create_format_frame ()
@@ -18,71 +21,80 @@ def __init__(self):
1821
1922 self .initialize ()
2023
24+ def styling (self ):
25+ ttk .Style ().theme_use ('alt' )
26+ 27+ ttk .Style ().configure ('Q.TLabel' , foreground = '#a52a2a' )
28+ 29+ ttk .Style ().configure ('DF.TLabel' )
30+ ttk .Style ().configure ('SM.TLabel' , background = '#24ff24' )
31+ ttk .Style ().configure ('SNM.TLabel' , background = '#ff2424' )
32+ ttk .Style ().configure ('CS.TLabel' , background = '#a3ffa3' )
33+ ttk .Style ().configure ('WS.TLabel' , background = '#ffa3a3' )
34+ 2135 def create_question_frame (self ):
22- self .question_frame = tk .Frame ()
36+ self .question_frame = ttk .Frame ()
2337 self .question_frame .pack (side = tk .TOP , pady = 5 )
2438
25- self .l_question = tk .Label (self .question_frame , wraplength = 500 ,
26- justify = 'left' , fg = '#a52a2a ' ,
27- font = 'TkFixedFont ' )
39+ self .l_question = ttk .Label (self .question_frame , wraplength = 500 ,
40+ justify = 'left' , font = 'TkFixedFont ' ,
41+ style = 'Q.TLabel ' )
2842 self .l_question .pack ()
2943
3044 def create_pattern_frame (self ):
31- self .pattern_frame = tk .Frame ()
45+ self .pattern_frame = ttk .Frame ()
3246 self .pattern_frame .pack (side = tk .TOP , pady = 5 )
3347
34- label = tk .Label (self .pattern_frame , text = 'Search Pattern: ' ,
35- pady = 10 , font = 'TkFixedFont' )
36- label .pack (side = tk .LEFT )
37- self .l_default_bg = label ['bg' ]
48+ label = ttk .Label (self .pattern_frame , text = 'Search Pattern: ' ,
49+ font = 'TkFixedFont' )
50+ label .pack (side = tk .LEFT , pady = 10 )
3851
3952 self .user_input = tk .StringVar ()
4053 self .user_input .trace_add ('write' , self .update_display )
41- self .e_pattern = tk .Entry (self .pattern_frame ,
42- textvariable = self .user_input ,
43- width = 35 , font = 'TkFixedFont' )
54+ self .e_pattern = ttk .Entry (self .pattern_frame ,
55+ textvariable = self .user_input ,
56+ width = 35 , font = 'TkFixedFont' )
4457 self .e_pattern .pack (side = tk .RIGHT )
4558
4659 def create_format_frame (self ):
47- self .format_frame = tk .Frame ()
60+ self .format_frame = ttk .Frame ()
4861 self .format_frame .pack (side = tk .TOP , pady = 5 )
4962
5063 self .test_string_format = tk .IntVar ()
5164 self .test_string_format .set (0 )
52- tk .Radiobutton (self .format_frame , text = 'visual string' ,
53- variable = self .test_string_format , value = 0 ,
54- command = self .change_format
55- ).pack (side = tk .LEFT )
56- tk .Radiobutton (self .format_frame , text = 'representation' ,
57- variable = self .test_string_format , value = 1 ,
58- command = self .change_format
59- ).pack (side = tk .RIGHT )
65+ ttk .Radiobutton (self .format_frame , text = 'visual string' ,
66+ variable = self .test_string_format , value = 0 ,
67+ command = self .change_format
68+ ).pack (side = tk .LEFT )
69+ ttk .Radiobutton (self .format_frame , text = 'representation' ,
70+ variable = self .test_string_format , value = 1 ,
71+ command = self .change_format
72+ ).pack (side = tk .RIGHT )
6073
6174 def create_test_frame (self ):
62- self .test_frame = tk .Frame ()
75+ self .test_frame = ttk .Frame ()
6376 self .test_frame .pack ()
6477
6578 def create_solution_frame (self ):
66- self .solution_frame = tk .Frame ()
79+ self .solution_frame = ttk .Frame ()
6780 self .solution_frame .pack ()
6881
69- self .l_solution = tk .Label (self .solution_frame ,
70- pady = 10 , font = 'TkFixedFont' )
71- self .l_solution .pack ()
82+ self .l_solution = ttk .Label (self .solution_frame , font = 'TkFixedFont' )
83+ self .l_solution .pack (pady = 10 )
7284
7385 def create_button_frame (self ):
74- self .button_frame = tk .Frame ()
86+ self .button_frame = ttk .Frame ()
7587 self .button_frame .pack ()
7688
77- tk .Button (self .button_frame , text = 'Quit' , command = self .quit
78- ).pack (side = tk .LEFT )
89+ ttk .Button (self .button_frame , text = 'Quit' , command = self .quit
90+ ).pack (side = tk .LEFT )
7991
80- tk .Button (self .button_frame , text = 'Previous' ,
81- command = lambda : self .next (previous = True )
82- ).pack (side = tk .LEFT )
92+ ttk .Button (self .button_frame , text = 'Previous' ,
93+ command = lambda : self .next (previous = True )
94+ ).pack (side = tk .LEFT )
8395
84- tk .Button (self .button_frame , text = 'Next' , command = self .next
85- ).pack (side = tk .RIGHT )
96+ ttk .Button (self .button_frame , text = 'Next' , command = self .next
97+ ).pack (side = tk .RIGHT )
8698
8799 def initialize (self ):
88100 self .format = {0 : str , 1 : repr }
@@ -114,11 +126,11 @@ def initialize(self):
114126
115127 def display_question (self , question ):
116128 def create_label (s ):
117- return tk .Label (self .test_frame , text = s ,
118- bg = self . l_default_bg , highlightthickness = 1 ,
119- highlightbackground = 'black ' ,
120- width = 35 , justify = tk . LEFT , anchor = 'w ' ,
121- padx = 10 , pady = 10 , font = 'TkFixedFont ' )
129+ return ttk .Label (self .test_frame , text = s ,
130+ width = 35 , justify = tk . LEFT ,
131+ anchor = 'w' , font = 'TkFixedFont ' ,
132+ borderwidth = 2 , relief = 'raised ' ,
133+ padding = 10 , style = 'DF.TLabel ' )
122134
123135 self .l_question ['text' ] = f"Q{ self .question_idx + 1 } ) { question ['question' ]} "
124136 self .should_match = ['Should match' , * question ['Should match' ]]
@@ -132,15 +144,15 @@ def create_label(s):
132144 label = create_label (fmt_func (sm ))
133145 label .grid (row = row , column = 0 )
134146 if row == 0 :
135- label ['bg ' ] = '#24ff24 '
147+ label ['style ' ] = 'SM.TLabel '
136148 label ['justify' ] = tk .CENTER
137149 label ['anchor' ] = 'center'
138150 self .l_test_strings [row * 2 ] = label
139151
140152 label = create_label (fmt_func (snm ))
141153 label .grid (row = row , column = 1 )
142154 if row == 0 :
143- label ['bg ' ] = '#ff2424 '
155+ label ['style ' ] = 'SNM.TLabel '
144156 label ['justify' ] = tk .CENTER
145157 label ['anchor' ] = 'center'
146158 self .l_test_strings [row * 2 + 1 ] = label
@@ -185,25 +197,25 @@ def update_display(self, *_):
185197 except re .error :
186198 for label in self .l_test_strings [2 :]:
187199 label ['state' ] = 'disable'
188- label ['bg ' ] = self . l_default_bg
200+ label ['style ' ] = 'DF.TLabel'
189201 return
190202 else :
191203 for label in self .l_test_strings [2 :]:
192204 label ['state' ] = 'normal'
193- label ['bg ' ] = self . l_default_bg
205+ label ['style ' ] = 'DF.TLabel'
194206 if pat .pattern == '' :
195207 return
196208
197209 row = 1
198210 self .correct_solution = True
199211 for sm , snm in zip (self .should_match [1 :], self .should_not_match [1 :]):
200212 if pat .search (sm ):
201- self .l_test_strings [row * 2 ]['bg ' ] = '#a3ffa3 '
213+ self .l_test_strings [row * 2 ]['style ' ] = 'CS.TLabel '
202214 else :
203215 self .correct_solution = False
204216
205217 if not pat .search (snm ):
206- self .l_test_strings [row * 2 + 1 ]['bg ' ] = '#ffa3a3 '
218+ self .l_test_strings [row * 2 + 1 ]['style ' ] = 'WS.TLabel '
207219 else :
208220 self .correct_solution = False
209221
0 commit comments