Skip to content

Commit d0cf6f3

Browse files
committed
Changes per chapter 5 revision 2
1 parent 1efa647 commit d0cf6f3

File tree

3 files changed

+40
-50
lines changed

3 files changed

+40
-50
lines changed

Chapter05/DateEntry.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ def __init__(self, parent, *args, **kwargs):
99
super().__init__(parent, *args, **kwargs)
1010
self.configure(
1111
validate='all',
12-
validatecommand=(self.register(self._validate), '%S', '%i', '%V', '%d'),
12+
validatecommand=(
13+
self.register(self._validate),
14+
'%S', '%i', '%V', '%d'
15+
),
1316
invalidcommand=(self.register(self._on_invalid), '%V')
1417
)
1518
self.error = tk.StringVar()
1619

1720
def _toggle_error(self, error=''):
1821
self.error.set(error)
19-
if error:
20-
self.config(foreground='red')
21-
else:
22-
self.config(foreground='black')
22+
self.config(foreground='red' if error else 'black')
2323

2424
def _validate(self, char, index, event, action):
2525

@@ -53,7 +53,7 @@ def _on_invalid(self, event):
5353
entry = DateEntry(root)
5454
entry.pack()
5555
ttk.Label(
56-
textvariable=entry.error, foreground='red'
56+
textvariable=entry.error, foreground='red'
5757
).pack()
5858

5959
# add this so we can unfocus the DateEntry

Chapter05/data_entry_app.py

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def _key_validate(self, proposed, action, **kwargs):
148148

149149
# get our values list
150150
values = self.cget('values')
151-
# Do a case-insensitve match against the entered text
151+
# Do a case-insensitive match against the entered text
152152
matching = [
153153
x for x in values
154154
if x.lower().startswith(proposed.lower())
@@ -199,7 +199,7 @@ def _set_focus_update_var(self, event):
199199
if self.focus_update_var and not self.error.get():
200200
self.focus_update_var.set(value)
201201

202-
def _set_minimum(self, *args):
202+
def _set_minimum(self, *_):
203203
current = self.get()
204204
try:
205205
new_min = self.min_var.get()
@@ -212,7 +212,7 @@ def _set_minimum(self, *args):
212212
self.variable.set(current)
213213
self.trigger_focusout_validation()
214214

215-
def _set_maximum(self, *args):
215+
def _set_maximum(self, *_):
216216
current = self.get()
217217
try:
218218
new_max = self.max_var.get()
@@ -228,13 +228,13 @@ def _set_maximum(self, *args):
228228
def _key_validate(
229229
self, char, index, current, proposed, action, **kwargs
230230
):
231+
if action == '0':
232+
return True
231233
valid = True
232234
min_val = self.cget('from')
233235
max_val = self.cget('to')
234236
no_negative = min_val >= 0
235237
no_decimal = self.precision >= 0
236-
if action == '0':
237-
return True
238238

239239
# First, filter out obviously invalid keystrokes
240240
if any([
@@ -269,16 +269,17 @@ def _focusout_validate(self, **kwargs):
269269
max_val = self.cget('to')
270270

271271
try:
272-
value = Decimal(value)
272+
d_value = Decimal(value)
273273
except InvalidOperation:
274-
self.error.set('Invalid number string: {}'.format(value))
274+
self.error.set(f'Invalid number string: {value}')
275275
return False
276276

277-
if value < min_val:
278-
self.error.set('Value is too low (min {})'.format(min_val))
277+
if d_value < min_val:
278+
self.error.set(f'Value is too low (min {min_val})')
279+
valid = False
280+
if d_value > max_val:
281+
self.error.set(f'Value is too high (max {max_val})')
279282
valid = False
280-
if value > max_val:
281-
self.error.set('Value is too high (max {})'.format(max_val))
282283

283284
return valid
284285

@@ -288,36 +289,23 @@ class BoundText(tk.Text):
288289
def __init__(self, *args, textvariable=None, **kwargs):
289290
super().__init__(*args, **kwargs)
290291
self._variable = textvariable
291-
self._modifying = False
292292
if self._variable:
293293
# insert any default value
294294
self.insert('1.0', self._variable.get())
295295
self._variable.trace_add('write', self._set_content)
296296
self.bind('<<Modified>>', self._set_var)
297297

298-
def _clear_modified_flag(self):
299-
# This also triggers a '<<Modified>>' Event
300-
self.tk.call(self._w, 'edit', 'modified', 0)
301-
302298
def _set_var(self, *_):
303299
"""Set the variable to the text contents"""
304-
if self._modifying:
305-
return
306-
self._modifying = True
307-
# remove trailing newline from content
308-
content = self.get('1.0', tk.END)[:-1]
309-
self._variable.set(content)
310-
self._clear_modified_flag()
311-
self._modifying = False
300+
if self.edit_modified():
301+
content = self.get('1.0', 'end-1chars')
302+
self._variable.set(content)
303+
self.edit_modified(False)
312304

313305
def _set_content(self, *_):
314306
"""Set the text contents to the variable"""
315-
if self._modifying:
316-
return
317-
self._modifying = True
318307
self.delete('1.0', tk.END)
319308
self.insert('1.0', self._variable.get())
320-
self._modifying = False
321309

322310
##################
323311
# Module Classes #
@@ -595,7 +583,9 @@ def reset(self):
595583
plot = self._vars['Plot'].get()
596584
except tk.TclError:
597585
plot = ''
598-
plot_values = self._vars['Plot'].label_widget.input.cget('values')
586+
plot_values = (
587+
self._vars['Plot'].label_widget.input.cget('values')
588+
)
599589

600590
# clear all values
601591
for var in self._vars.values():

Chapter05/validate_demo.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,51 @@
66

77
# create a pointless validation command
88
def always_good():
9-
return True
9+
return True
1010

1111
validate_ref = root.register(always_good)
1212

1313
entry.configure(
14-
validate='all',
15-
validatecommand=(validate_ref,)
14+
validate='all',
15+
validatecommand=(validate_ref,)
1616
)
1717

1818
# a more useful validation command
1919

2020

2121
entry2 = tk.Entry(root)
22-
entry2.grid()
22+
entry2.grid(pady=10)
2323

2424
def no_t_for_me(proposed):
25-
return 't' not in proposed
25+
return 't' not in proposed
2626

2727
validate2_ref = root.register(no_t_for_me)
2828
entry2.configure(
29-
validate='all',
30-
validatecommand=(validate2_ref, '%P')
29+
validate='all',
30+
validatecommand=(validate2_ref, '%P')
3131
)
3232

3333
# An Entry that displays an error
3434

3535
entry3 = tk.Entry(root)
3636
entry3.grid()
3737
entry3_error = tk.Label(root, fg='red')
38-
entry3_error.grid()
38+
entry3_error.grid(pady=10)
3939

4040
def only_five_chars(proposed):
41-
return len(proposed) < 6
41+
return len(proposed) < 6
4242

4343
def only_five_chars_error(proposed):
44-
entry3_error.configure(
45-
text=f'{proposed} is too long, only 5 chars allowed.'
46-
)
44+
entry3_error.configure(
45+
text=f'{proposed} is too long, only 5 chars allowed.'
46+
)
4747
validate3_ref = root.register(only_five_chars)
4848
invalid3_ref = root.register(only_five_chars_error)
4949

5050
entry3.configure(
51-
validate='all',
52-
validatecommand=(validate3_ref, '%P'),
53-
invalidcommand=(invalid3_ref, '%P')
51+
validate='all',
52+
validatecommand=(validate3_ref, '%P'),
53+
invalidcommand=(invalid3_ref, '%P')
5454
)
5555

5656
root.mainloop()

0 commit comments

Comments
 (0)