Commit f74f4cc8 authored by Martin v. Löwis's avatar Martin v. Löwis

Patch #763580: Add name and value arguments to

Tkinter variable classes.
parent ab80daa5
...@@ -168,18 +168,30 @@ class Variable: ...@@ -168,18 +168,30 @@ class Variable:
Subclasses StringVar, IntVar, DoubleVar, BooleanVar are specializations Subclasses StringVar, IntVar, DoubleVar, BooleanVar are specializations
that constrain the type of the value returned from get().""" that constrain the type of the value returned from get()."""
_default = "" _default = ""
def __init__(self, master=None): def __init__(self, master=None, value=None, name=None):
"""Construct a variable with an optional MASTER as master widget. """Construct a variable
The variable is named PY_VAR_number in Tcl.
MASTER can be given as master widget.
VALUE is an optional value (defaults to "")
NAME is an optional Tcl name (defaults to PY_VARnum).
If NAME matches an existing variable and VALUE is omitted
then the existing value is retained.
""" """
global _varnum global _varnum
if not master: if not master:
master = _default_root master = _default_root
self._master = master self._master = master
self._tk = master.tk self._tk = master.tk
self._name = 'PY_VAR' + repr(_varnum) if name:
_varnum = _varnum + 1 self._name = name
self.set(self._default) else:
self._name = 'PY_VAR' + `_varnum`
_varnum += 1
if value != None:
self.set(value)
elif not self._tk.call("info", "exists", self._name):
self.set(self._default)
def __del__(self): def __del__(self):
"""Unset the variable in Tcl.""" """Unset the variable in Tcl."""
self._tk.globalunsetvar(self._name) self._tk.globalunsetvar(self._name)
...@@ -217,15 +229,29 @@ class Variable: ...@@ -217,15 +229,29 @@ class Variable:
"""Return all trace callback information.""" """Return all trace callback information."""
return map(self._tk.split, self._tk.splitlist( return map(self._tk.split, self._tk.splitlist(
self._tk.call("trace", "vinfo", self._name))) self._tk.call("trace", "vinfo", self._name)))
def __eq__(self, other):
"""Comparison for equality (==).
Note: if the Variable's master matters to behavior
also compare self._master == other._master
"""
return self.__class__.__name__ == other.__class__.__name__ \
and self._name == other._name
class StringVar(Variable): class StringVar(Variable):
"""Value holder for strings variables.""" """Value holder for strings variables."""
_default = "" _default = ""
def __init__(self, master=None): def __init__(self, master=None, value=None, name=None):
"""Construct a string variable. """Construct a string variable.
MASTER can be given as master widget.""" MASTER can be given as master widget.
Variable.__init__(self, master) VALUE is an optional value (defaults to "")
NAME is an optional Tcl name (defaults to PY_VARnum).
If NAME matches an existing variable and VALUE is omitted
then the existing value is retained.
"""
Variable.__init__(self, master, value, name)
def get(self): def get(self):
"""Return value of variable as string.""" """Return value of variable as string."""
...@@ -237,11 +263,17 @@ class StringVar(Variable): ...@@ -237,11 +263,17 @@ class StringVar(Variable):
class IntVar(Variable): class IntVar(Variable):
"""Value holder for integer variables.""" """Value holder for integer variables."""
_default = 0 _default = 0
def __init__(self, master=None): def __init__(self, master=None, value=None, name=None):
"""Construct an integer variable. """Construct an integer variable.
MASTER can be given as master widget.""" MASTER can be given as master widget.
Variable.__init__(self, master) VALUE is an optional value (defaults to 0)
NAME is an optional Tcl name (defaults to PY_VARnum).
If NAME matches an existing variable and VALUE is omitted
then the existing value is retained.
"""
Variable.__init__(self, master, value, name)
def set(self, value): def set(self, value):
"""Set the variable to value, converting booleans to integers.""" """Set the variable to value, converting booleans to integers."""
...@@ -256,11 +288,17 @@ class IntVar(Variable): ...@@ -256,11 +288,17 @@ class IntVar(Variable):
class DoubleVar(Variable): class DoubleVar(Variable):
"""Value holder for float variables.""" """Value holder for float variables."""
_default = 0.0 _default = 0.0
def __init__(self, master=None): def __init__(self, master=None, value=None, name=None):
"""Construct a float variable. """Construct a float variable.
MASTER can be given as a master widget.""" MASTER can be given as master widget.
Variable.__init__(self, master) VALUE is an optional value (defaults to 0.0)
NAME is an optional Tcl name (defaults to PY_VARnum).
If NAME matches an existing variable and VALUE is omitted
then the existing value is retained.
"""
Variable.__init__(self, master, value, name)
def get(self): def get(self):
"""Return the value of the variable as a float.""" """Return the value of the variable as a float."""
...@@ -268,12 +306,18 @@ class DoubleVar(Variable): ...@@ -268,12 +306,18 @@ class DoubleVar(Variable):
class BooleanVar(Variable): class BooleanVar(Variable):
"""Value holder for boolean variables.""" """Value holder for boolean variables."""
_default = "false" _default = False
def __init__(self, master=None): def __init__(self, master=None, value=None, name=None):
"""Construct a boolean variable. """Construct a boolean variable.
MASTER can be given as a master widget.""" MASTER can be given as master widget.
Variable.__init__(self, master) VALUE is an optional value (defaults to False)
NAME is an optional Tcl name (defaults to PY_VARnum).
If NAME matches an existing variable and VALUE is omitted
then the existing value is retained.
"""
Variable.__init__(self, master, value, name)
def get(self): def get(self):
"""Return the value of the variable as a bool.""" """Return the value of the variable as a bool."""
......
...@@ -163,6 +163,9 @@ Extension Modules ...@@ -163,6 +163,9 @@ Extension Modules
Library Library
------- -------
- Patch #763580: Add name and value arguments to Tkinter variable
classes.
- Bug #1117556: SimpleHTTPServer now tries to find and use the system's - Bug #1117556: SimpleHTTPServer now tries to find and use the system's
mime.types file for determining MIME types. mime.types file for determining MIME types.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment