From 007ee17e21eeb3dcbb515efa1436f3f669aad2fc Mon Sep 17 00:00:00 2001
From: Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Date: Mon, 3 Nov 2008 18:18:08 +0000
Subject: [PATCH] Issue #3774: Fixed an error when create a Tkinter menu item
 without command and then remove it. Written by Guilherme Polo (gpolo).
 Backport of r67082.

---
 Lib/lib-tk/Tkinter.py | 24 +++++++++++++-----------
 Misc/NEWS             |  3 +++
 2 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/Lib/lib-tk/Tkinter.py b/Lib/lib-tk/Tkinter.py
index d714a908a3..de2873fc3c 100644
--- a/Lib/lib-tk/Tkinter.py
+++ b/Lib/lib-tk/Tkinter.py
@@ -1931,6 +1931,8 @@ class BaseWidget(Misc):
             cnf = _cnfmerge((cnf, kw))
         self.widgetName = widgetName
         BaseWidget._setup(self, master, cnf)
+        if self._tclCommands is None:
+            self._tclCommands = []
         classes = []
         for k in cnf.keys():
             if type(k) is ClassType:
@@ -2668,20 +2670,20 @@ class Menu(Widget):
         """Add separator at INDEX."""
         self.insert(index, 'separator', cnf or kw)
     def delete(self, index1, index2=None):
-        """Delete menu items between INDEX1 and INDEX2 (not included)."""
+        """Delete menu items between INDEX1 and INDEX2 (included)."""
         if index2 is None:
             index2 = index1
-        cmds = []
-        (num_index1, num_index2) = (self.index(index1), self.index(index2))
-        if (num_index1 is not None) and (num_index2 is not None):
-            for i in range(num_index1, num_index2 + 1):
-                if 'command' in self.entryconfig(i):
-                    c = str(self.entrycget(i, 'command'))
-                    if c in self._tclCommands:
-                        cmds.append(c)
+
+        num_index1, num_index2 = self.index(index1), self.index(index2)
+        if (num_index1 is None) or (num_index2 is None):
+            num_index1, num_index2 = 0, -1
+
+        for i in range(num_index1, num_index2 + 1):
+            if 'command' in self.entryconfig(i):
+                c = str(self.entrycget(i, 'command'))
+                if c:
+                    self.deletecommand(c)
         self.tk.call(self._w, 'delete', index1, index2)
-        for c in cmds:
-            self.deletecommand(c)
     def entrycget(self, index, option):
         """Return the resource value of an menu item for OPTION at INDEX."""
         return self.tk.call(self._w, 'entrycget', index, '-' + option)
diff --git a/Misc/NEWS b/Misc/NEWS
index c8651c1820..4c45e4fc50 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -97,6 +97,9 @@ Core and builtins
 Library
 -------
 
+- Issue #3774: Fixed an error when create a Tkinter menu item without command
+  and then remove it.
+
 - Assigning methods to ctypes.Structure and ctypes.Union subclasses
   after creation of the class does now work correctly.  See Issue #1700288.
 
-- 
2.40.0