summaryrefslogtreecommitdiffstats
path: root/Lib/tkinter
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/tkinter')
-rw-r--r--Lib/tkinter/__init__.py19
-rw-r--r--Lib/tkinter/test/test_tkinter/test_variables.py51
-rw-r--r--Lib/tkinter/test/test_ttk/test_widgets.py51
-rw-r--r--Lib/tkinter/ttk.py4
4 files changed, 116 insertions, 9 deletions
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index aa646dc..55bfb7f 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -271,7 +271,7 @@ class Variable:
Return the name of the callback.
"""
- f = CallWrapper(callback, None, self).__call__
+ f = CallWrapper(callback, None, self._root).__call__
cbname = repr(id(f))
try:
callback = callback.__func__
@@ -295,14 +295,19 @@ class Variable:
CBNAME is the name of the callback returned from trace_variable or trace.
"""
self._tk.call("trace", "vdelete", self._name, mode, cbname)
- self._tk.deletecommand(cbname)
- try:
- self._tclCommands.remove(cbname)
- except ValueError:
- pass
+ cbname = self._tk.splitlist(cbname)[0]
+ for m, ca in self.trace_vinfo():
+ if self._tk.splitlist(ca)[0] == cbname:
+ break
+ else:
+ self._tk.deletecommand(cbname)
+ try:
+ self._tclCommands.remove(cbname)
+ except ValueError:
+ pass
def trace_vinfo(self):
"""Return all trace callback information."""
- return [self._tk.split(x) for x in self._tk.splitlist(
+ return [self._tk.splitlist(x) for x in self._tk.splitlist(
self._tk.call("trace", "vinfo", self._name))]
def __eq__(self, other):
"""Comparison for equality (==).
diff --git a/Lib/tkinter/test/test_tkinter/test_variables.py b/Lib/tkinter/test/test_tkinter/test_variables.py
index abdce96..1f74453 100644
--- a/Lib/tkinter/test/test_tkinter/test_variables.py
+++ b/Lib/tkinter/test/test_tkinter/test_variables.py
@@ -1,5 +1,5 @@
import unittest
-
+import gc
from tkinter import (Variable, StringVar, IntVar, DoubleVar, BooleanVar, Tcl,
TclError)
@@ -87,6 +87,55 @@ class TestVariable(TestBase):
v.set("value")
self.assertTrue(v.side_effect)
+ def test_trace(self):
+ v = Variable(self.root)
+ vname = str(v)
+ trace = []
+ def read_tracer(*args):
+ trace.append(('read',) + args)
+ def write_tracer(*args):
+ trace.append(('write',) + args)
+ cb1 = v.trace_variable('r', read_tracer)
+ cb2 = v.trace_variable('wu', write_tracer)
+ self.assertEqual(sorted(v.trace_vinfo()), [('r', cb1), ('wu', cb2)])
+ self.assertEqual(trace, [])
+
+ v.set('spam')
+ self.assertEqual(trace, [('write', vname, '', 'w')])
+
+ trace = []
+ v.get()
+ self.assertEqual(trace, [('read', vname, '', 'r')])
+
+ trace = []
+ info = sorted(v.trace_vinfo())
+ v.trace_vdelete('w', cb1) # Wrong mode
+ self.assertEqual(sorted(v.trace_vinfo()), info)
+ with self.assertRaises(TclError):
+ v.trace_vdelete('r', 'spam') # Wrong command name
+ self.assertEqual(sorted(v.trace_vinfo()), info)
+ v.trace_vdelete('r', (cb1, 43)) # Wrong arguments
+ self.assertEqual(sorted(v.trace_vinfo()), info)
+ v.get()
+ self.assertEqual(trace, [('read', vname, '', 'r')])
+
+ trace = []
+ v.trace_vdelete('r', cb1)
+ self.assertEqual(v.trace_vinfo(), [('wu', cb2)])
+ v.get()
+ self.assertEqual(trace, [])
+
+ trace = []
+ del write_tracer
+ gc.collect()
+ v.set('eggs')
+ self.assertEqual(trace, [('write', vname, '', 'w')])
+
+ trace = []
+ del v
+ gc.collect()
+ self.assertEqual(trace, [('write', vname, '', 'u')])
+
class TestStringVar(TestBase):
diff --git a/Lib/tkinter/test/test_ttk/test_widgets.py b/Lib/tkinter/test/test_ttk/test_widgets.py
index c031351..8bd22d0 100644
--- a/Lib/tkinter/test/test_ttk/test_widgets.py
+++ b/Lib/tkinter/test/test_ttk/test_widgets.py
@@ -1486,6 +1486,57 @@ class TreeviewTest(AbstractWidgetTest, unittest.TestCase):
value)
+ def test_selection(self):
+ # item 'none' doesn't exist
+ self.assertRaises(tkinter.TclError, self.tv.selection_set, 'none')
+ self.assertRaises(tkinter.TclError, self.tv.selection_add, 'none')
+ self.assertRaises(tkinter.TclError, self.tv.selection_remove, 'none')
+ self.assertRaises(tkinter.TclError, self.tv.selection_toggle, 'none')
+
+ item1 = self.tv.insert('', 'end')
+ item2 = self.tv.insert('', 'end')
+ c1 = self.tv.insert(item1, 'end')
+ c2 = self.tv.insert(item1, 'end')
+ c3 = self.tv.insert(item1, 'end')
+ self.assertEqual(self.tv.selection(), ())
+
+ self.tv.selection_set((c1, item2))
+ self.assertEqual(self.tv.selection(), (c1, item2))
+ self.tv.selection_set(c2)
+ self.assertEqual(self.tv.selection(), (c2,))
+
+ self.tv.selection_add((c1, item2))
+ self.assertEqual(self.tv.selection(), (c1, c2, item2))
+ self.tv.selection_add(item1)
+ self.assertEqual(self.tv.selection(), (item1, c1, c2, item2))
+
+ self.tv.selection_remove((item1, c3))
+ self.assertEqual(self.tv.selection(), (c1, c2, item2))
+ self.tv.selection_remove(c2)
+ self.assertEqual(self.tv.selection(), (c1, item2))
+
+ self.tv.selection_toggle((c1, c3))
+ self.assertEqual(self.tv.selection(), (c3, item2))
+ self.tv.selection_toggle(item2)
+ self.assertEqual(self.tv.selection(), (c3,))
+
+ self.tv.insert('', 'end', id='with spaces')
+ self.tv.selection_set('with spaces')
+ self.assertEqual(self.tv.selection(), ('with spaces',))
+
+ self.tv.insert('', 'end', id='{brace')
+ self.tv.selection_set('{brace')
+ self.assertEqual(self.tv.selection(), ('{brace',))
+
+ self.tv.insert('', 'end', id='unicode\u20ac')
+ self.tv.selection_set('unicode\u20ac')
+ self.assertEqual(self.tv.selection(), ('unicode\u20ac',))
+
+ self.tv.insert('', 'end', id=b'bytes\xe2\x82\xac')
+ self.tv.selection_set(b'bytes\xe2\x82\xac')
+ self.assertEqual(self.tv.selection(), ('bytes\xe2\x82\xac',))
+
+
def test_set(self):
self.tv['columns'] = ['A', 'B']
item = self.tv.insert('', 'end', values=['a', 'b'])
diff --git a/Lib/tkinter/ttk.py b/Lib/tkinter/ttk.py
index 8f9369b..0e5759d 100644
--- a/Lib/tkinter/ttk.py
+++ b/Lib/tkinter/ttk.py
@@ -1392,7 +1392,9 @@ class Treeview(Widget, tkinter.XView, tkinter.YView):
def selection(self, selop=None, items=None):
"""If selop is not specified, returns selected items."""
- return self.tk.call(self._w, "selection", selop, items)
+ if isinstance(items, (str, bytes)):
+ items = (items,)
+ return self.tk.splitlist(self.tk.call(self._w, "selection", selop, items))
def selection_set(self, items):