summaryrefslogtreecommitdiff
path: root/Lib/tkinter
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-04-04 12:44:30 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2015-04-04 12:44:30 +0300
commit40f00375853af362198af8ab80fb69dec8b84e5f (patch)
tree9957d749cee1f183008ea45f617a917bc37f8a79 /Lib/tkinter
parentd6c289dad8f6c9979a3bec08cc003acab661bef2 (diff)
parent32e04f79e3b2be5d92f128bafa2354b9a45fd400 (diff)
downloadcpython-40f00375853af362198af8ab80fb69dec8b84e5f.tar.gz
Issue #15133: _tkinter.tkapp.getboolean() now supports Tcl_Obj and always
returns bool. tkinter.BooleanVar now validates input values (accepted bool, int, str, and Tcl_Obj). tkinter.BooleanVar.get() now always returns bool.
Diffstat (limited to 'Lib/tkinter')
-rw-r--r--Lib/tkinter/__init__.py5
-rw-r--r--Lib/tkinter/test/test_tkinter/test_variables.py34
-rw-r--r--Lib/tkinter/ttk.py6
3 files changed, 38 insertions, 7 deletions
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index beb0a73d00..c8d806137e 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -391,6 +391,11 @@ class BooleanVar(Variable):
"""
Variable.__init__(self, master, value, name)
+ def set(self, value):
+ """Set the variable to VALUE."""
+ return self._tk.globalsetvar(self._name, self._tk.getboolean(value))
+ initialize = set
+
def get(self):
"""Return the value of the variable as a bool."""
try:
diff --git a/Lib/tkinter/test/test_tkinter/test_variables.py b/Lib/tkinter/test/test_tkinter/test_variables.py
index 7b7e2999ba..4b72943178 100644
--- a/Lib/tkinter/test/test_tkinter/test_variables.py
+++ b/Lib/tkinter/test/test_tkinter/test_variables.py
@@ -1,6 +1,7 @@
import unittest
-from tkinter import Variable, StringVar, IntVar, DoubleVar, BooleanVar, Tcl
+from tkinter import (Variable, StringVar, IntVar, DoubleVar, BooleanVar, Tcl,
+ TclError)
class Var(Variable):
@@ -159,16 +160,41 @@ class TestBooleanVar(TestBase):
def test_default(self):
v = BooleanVar(self.root)
- self.assertEqual(False, v.get())
+ self.assertIs(v.get(), False)
def test_get(self):
v = BooleanVar(self.root, True, "name")
- self.assertAlmostEqual(True, v.get())
+ self.assertIs(v.get(), True)
self.root.globalsetvar("name", "0")
- self.assertAlmostEqual(False, v.get())
+ self.assertIs(v.get(), False)
+ self.root.globalsetvar("name", 42 if self.root.wantobjects() else 1)
+ self.assertIs(v.get(), True)
+ self.root.globalsetvar("name", 0)
+ self.assertIs(v.get(), False)
+ self.root.globalsetvar("name", "on")
+ self.assertIs(v.get(), True)
+
+ def test_set(self):
+ true = 1 if self.root.wantobjects() else "1"
+ false = 0 if self.root.wantobjects() else "0"
+ v = BooleanVar(self.root, name="name")
+ v.set(True)
+ self.assertEqual(self.root.globalgetvar("name"), true)
+ v.set("0")
+ self.assertEqual(self.root.globalgetvar("name"), false)
+ v.set(42)
+ self.assertEqual(self.root.globalgetvar("name"), true)
+ v.set(0)
+ self.assertEqual(self.root.globalgetvar("name"), false)
+ v.set("on")
+ self.assertEqual(self.root.globalgetvar("name"), true)
def test_invalid_value_domain(self):
+ false = 0 if self.root.wantobjects() else "0"
v = BooleanVar(self.root, name="name")
+ with self.assertRaises(TclError):
+ v.set("value")
+ self.assertEqual(self.root.globalgetvar("name"), false)
self.root.globalsetvar("name", "value")
with self.assertRaises(ValueError):
v.get()
diff --git a/Lib/tkinter/ttk.py b/Lib/tkinter/ttk.py
index 4327dbb05d..b9c57ad704 100644
--- a/Lib/tkinter/ttk.py
+++ b/Lib/tkinter/ttk.py
@@ -573,7 +573,7 @@ class Widget(tkinter.Widget):
if ret and callback:
return callback(*args, **kw)
- return bool(ret)
+ return ret
def state(self, statespec=None):
@@ -681,7 +681,7 @@ class Entry(Widget, tkinter.Entry):
"""Force revalidation, independent of the conditions specified
by the validate option. Returns False if validation fails, True
if it succeeds. Sets or clears the invalid state accordingly."""
- return bool(self.tk.getboolean(self.tk.call(self._w, "validate")))
+ return self.tk.getboolean(self.tk.call(self._w, "validate"))
class Combobox(Entry):
@@ -1231,7 +1231,7 @@ class Treeview(Widget, tkinter.XView, tkinter.YView):
def exists(self, item):
"""Returns True if the specified item is present in the tree,
False otherwise."""
- return bool(self.tk.getboolean(self.tk.call(self._w, "exists", item)))
+ return self.tk.getboolean(self.tk.call(self._w, "exists", item))
def focus(self, item=None):