Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb70b2a

Browse files
bpo-15303: Support widgets with boolean value False in Tkinter (GH-23904)
Use `widget is None` instead of checking the boolean value of a widget.
1 parent 954a742 commit bb70b2a

8 files changed

Lines changed: 30 additions & 29 deletions

File tree

Lib/tkinter/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def _get_default_root(what=None):
292292
if not _support_default_root:
293293
raise RuntimeError("No master specified and tkinter is "
294294
"configured to not support default root")
295-
if not _default_root:
295+
if _default_root is None:
296296
if what:
297297
raise RuntimeError(f"Too early to {what}: no default root window")
298298
root = Tk()
@@ -342,7 +342,7 @@ def __init__(self, master=None, value=None, name=None):
342342
if name is not None and not isinstance(name, str):
343343
raise TypeError("name must be a string")
344344
global _varnum
345-
if not master:
345+
if master is None:
346346
master = _get_default_root('create variable')
347347
self._root = master._root()
348348
self._tk = master.tk
@@ -808,7 +808,7 @@ def after(self, ms, func=None, *args):
808808
function which shall be called. Additional parameters
809809
are given as parameters to the function call. Return
810810
identifier to cancel scheduling with after_cancel."""
811-
if not func:
811+
if func is None:
812812
# I'd rather use time.sleep(ms*0.001)
813813
self.tk.call('after', ms)
814814
return None
@@ -1542,7 +1542,7 @@ def _register(self, func, subst=None, needcleanup=1):
15421542
def _root(self):
15431543
"""Internal function."""
15441544
w = self
1545-
while w.master: w = w.master
1545+
while w.master is not None: w = w.master
15461546
return w
15471547
_subst_format = ('%#', '%b', '%f', '%h', '%k',
15481548
'%s', '%t', '%w', '%x', '%y',
@@ -2306,7 +2306,7 @@ def _loadtk(self):
23062306
self.tk.createcommand('exit', _exit)
23072307
self._tclCommands.append('tkerror')
23082308
self._tclCommands.append('exit')
2309-
if _support_default_root and not _default_root:
2309+
if _support_default_root and _default_root is None:
23102310
_default_root = self
23112311
self.protocol("WM_DELETE_WINDOW", self.destroy)
23122312

@@ -2534,7 +2534,7 @@ class BaseWidget(Misc):
25342534

25352535
def _setup(self, master, cnf):
25362536
"""Internal function. Sets up information about children."""
2537-
if not master:
2537+
if master is None:
25382538
master = _get_default_root()
25392539
self.master = master
25402540
self.tk = master.tk
@@ -3949,7 +3949,7 @@ def __init__(self, var, value, callback=None):
39493949

39503950
def __call__(self, *args):
39513951
self.__var.set(self.__value)
3952-
if self.__callback:
3952+
if self.__callback is not None:
39533953
self.__callback(self.__value, *args)
39543954

39553955

@@ -3998,7 +3998,7 @@ class Image:
39983998

39993999
def __init__(self, imgtype, name=None, cnf={}, master=None, **kw):
40004000
self.name = None
4001-
if not master:
4001+
if master is None:
40024002
master = _get_default_root('create image')
40034003
self.tk = getattr(master, 'tk', master)
40044004
if not name:

Lib/tkinter/commondialog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Dialog:
1818
command = None
1919

2020
def __init__(self, master=None, **options):
21-
if not master:
21+
if master is None:
2222
master = options.get('parent')
2323
self.master = master
2424
self.options = options

Lib/tkinter/dnd.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108

109109
def dnd_start(source, event):
110110
h = DndHandler(source, event)
111-
if h.root:
111+
if h.root is not None:
112112
return h
113113
else:
114114
return None
@@ -143,7 +143,7 @@ def __init__(self, source, event):
143143
def __del__(self):
144144
root = self.root
145145
self.root = None
146-
if root:
146+
if root is not None:
147147
try:
148148
del root.__dnd
149149
except AttributeError:
@@ -154,25 +154,25 @@ def on_motion(self, event):
154154
target_widget = self.initial_widget.winfo_containing(x, y)
155155
source = self.source
156156
new_target = None
157-
while target_widget:
157+
while target_widget is not None:
158158
try:
159159
attr = target_widget.dnd_accept
160160
except AttributeError:
161161
pass
162162
else:
163163
new_target = attr(source, event)
164-
if new_target:
164+
if new_target is not None:
165165
break
166166
target_widget = target_widget.master
167167
old_target = self.target
168168
if old_target is new_target:
169-
if old_target:
169+
if old_target is not None:
170170
old_target.dnd_motion(source, event)
171171
else:
172-
if old_target:
172+
if old_target is not None:
173173
self.target = None
174174
old_target.dnd_leave(source, event)
175-
if new_target:
175+
if new_target is not None:
176176
new_target.dnd_enter(source, event)
177177
self.target = new_target
178178

@@ -193,7 +193,7 @@ def finish(self, event, commit=0):
193193
self.initial_widget.unbind("<Motion>")
194194
widget['cursor'] = self.save_cursor
195195
self.target = self.source = self.initial_widget = self.root = None
196-
if target:
196+
if target is not None:
197197
if commit:
198198
target.dnd_commit(source, event)
199199
else:
@@ -215,9 +215,9 @@ def attach(self, canvas, x=10, y=10):
215215
if canvas is self.canvas:
216216
self.canvas.coords(self.id, x, y)
217217
return
218-
if self.canvas:
218+
if self.canvas is not None:
219219
self.detach()
220-
if not canvas:
220+
if canvas is None:
221221
return
222222
label = tkinter.Label(canvas, text=self.name,
223223
borderwidth=2, relief="raised")
@@ -229,7 +229,7 @@ def attach(self, canvas, x=10, y=10):
229229

230230
def detach(self):
231231
canvas = self.canvas
232-
if not canvas:
232+
if canvas is None:
233233
return
234234
id = self.id
235235
label = self.label

Lib/tkinter/font.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def _mkdict(self, args):
6868

6969
def __init__(self, root=None, font=None, name=None, exists=False,
7070
**options):
71-
if not root:
71+
if root is None:
7272
root = tkinter._get_default_root('use font')
7373
tk = getattr(root, 'tk', root)
7474
if font:
@@ -183,7 +183,7 @@ def metrics(self, *options, **kw):
183183

184184
def families(root=None, displayof=None):
185185
"Get font families (as a tuple)"
186-
if not root:
186+
if root is None:
187187
root = tkinter._get_default_root('use font.families()')
188188
args = ()
189189
if displayof:
@@ -193,7 +193,7 @@ def families(root=None, displayof=None):
193193

194194
def names(root=None):
195195
"Get names of defined fonts (as a tuple)"
196-
if not root:
196+
if root is None:
197197
root = tkinter._get_default_root('use font.names()')
198198
return root.tk.splitlist(root.tk.call("font", "names"))
199199

Lib/tkinter/simpledialog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def __init__(self, parent, title = None):
9999
title -- the dialog title
100100
'''
101101
master = parent
102-
if not master:
102+
if master is None:
103103
master = _get_default_root('create dialog window')
104104

105105
Toplevel.__init__(self, master)
@@ -124,7 +124,7 @@ def __init__(self, parent, title = None):
124124

125125
self.buttonbox()
126126

127-
if not self.initial_focus:
127+
if self.initial_focus is None:
128128
self.initial_focus = self
129129

130130
self.protocol("WM_DELETE_WINDOW", self.cancel)

Lib/tkinter/tix.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def config_all(self, option, value):
386386
self.tk.call(name, 'configure', '-' + option, value)
387387
# These are missing from Tkinter
388388
def image_create(self, imgtype, cnf={}, master=None, **kw):
389-
if not master:
389+
if master is None:
390390
master = self
391391
if kw and cnf: cnf = _cnfmerge((cnf, kw))
392392
elif kw: cnf = kw
@@ -467,7 +467,7 @@ class DisplayStyle:
467467
(multiple) Display Items"""
468468

469469
def __init__(self, itemtype, cnf={}, *, master=None, **kw):
470-
if not master:
470+
if master is None:
471471
if 'refwindow' in kw:
472472
master = kw['refwindow']
473473
elif 'refwindow' in cnf:
@@ -862,7 +862,7 @@ def add(self, entry, cnf={}, **kw):
862862
return self.tk.call(self._w, 'add', entry, *self._options(cnf, kw))
863863

864864
def add_child(self, parent=None, cnf={}, **kw):
865-
if not parent:
865+
if parent is None:
866866
parent = ''
867867
return self.tk.call(
868868
self._w, 'addchild', parent, *self._options(cnf, kw))

Lib/tkinter/ttk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ def instate(self, statespec, callback=None, *args, **kw):
569569
matches statespec. statespec is expected to be a sequence."""
570570
ret = self.tk.getboolean(
571571
self.tk.call(self._w, "instate", ' '.join(statespec)))
572-
if ret and callback:
572+
if ret and callback is not None:
573573
return callback(*args, **kw)
574574

575575
return ret
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:mod:`tkinter` supports now widgets with boolean value False.

0 commit comments

Comments
 (0)