mirror of
https://github.com/python/cpython.git
synced 2025-09-18 14:40:43 +00:00
move constants to Tkconstants; added some; overridable error reporting; fix typo in propagate
This commit is contained in:
parent
1dc366f109
commit
a5773ddb23
2 changed files with 52 additions and 148 deletions
|
@ -3,6 +3,7 @@
|
||||||
import tkinter
|
import tkinter
|
||||||
from tkinter import TclError
|
from tkinter import TclError
|
||||||
from types import *
|
from types import *
|
||||||
|
from Tkconstants import *
|
||||||
|
|
||||||
CallableTypes = (FunctionType, MethodType,
|
CallableTypes = (FunctionType, MethodType,
|
||||||
BuiltinFunctionType, BuiltinMethodType)
|
BuiltinFunctionType, BuiltinMethodType)
|
||||||
|
@ -12,64 +13,6 @@ TclVersion = eval(tkinter.TCL_VERSION)
|
||||||
if TkVersion < 4.0:
|
if TkVersion < 4.0:
|
||||||
raise ImportError, "This version of Tkinter.py requires Tk 4.0 or higher"
|
raise ImportError, "This version of Tkinter.py requires Tk 4.0 or higher"
|
||||||
|
|
||||||
# Symbolic constants
|
|
||||||
|
|
||||||
# Booleans
|
|
||||||
NO=FALSE=OFF=0
|
|
||||||
YES=TRUE=ON=1
|
|
||||||
|
|
||||||
# -anchor
|
|
||||||
N='n'
|
|
||||||
S='s'
|
|
||||||
W='w'
|
|
||||||
E='e'
|
|
||||||
NW='nw'
|
|
||||||
SW='sw'
|
|
||||||
NE='ne'
|
|
||||||
SE='se'
|
|
||||||
CENTER='center'
|
|
||||||
|
|
||||||
# -fill
|
|
||||||
NONE='none'
|
|
||||||
X='x'
|
|
||||||
Y='y'
|
|
||||||
BOTH='both'
|
|
||||||
|
|
||||||
# -side
|
|
||||||
LEFT='left'
|
|
||||||
TOP='top'
|
|
||||||
RIGHT='right'
|
|
||||||
BOTTOM='bottom'
|
|
||||||
|
|
||||||
# -relief
|
|
||||||
RAISED='raised'
|
|
||||||
SUNKEN='sunken'
|
|
||||||
FLAT='flat'
|
|
||||||
RIDGE='ridge'
|
|
||||||
GROOVE='groove'
|
|
||||||
|
|
||||||
# -orient
|
|
||||||
HORIZONTAL='horizontal'
|
|
||||||
VERTICAL='vertical'
|
|
||||||
|
|
||||||
# -tabs
|
|
||||||
NUMERIC='numeric'
|
|
||||||
|
|
||||||
# -wrap
|
|
||||||
CHAR='char'
|
|
||||||
WORD='word'
|
|
||||||
|
|
||||||
# -align
|
|
||||||
BASELINE='baseline'
|
|
||||||
|
|
||||||
# Special tags, marks and insert positions
|
|
||||||
SEL='sel'
|
|
||||||
SEL_FIRST='sel.first'
|
|
||||||
SEL_LAST='sel.last'
|
|
||||||
END='end'
|
|
||||||
INSERT='insert'
|
|
||||||
CURRENT='current'
|
|
||||||
ANCHOR='anchor'
|
|
||||||
|
|
||||||
def _flatten(tuple):
|
def _flatten(tuple):
|
||||||
res = ()
|
res = ()
|
||||||
|
@ -461,6 +404,7 @@ class Misc:
|
||||||
cnf = _cnfmerge(cnf)
|
cnf = _cnfmerge(cnf)
|
||||||
res = ()
|
res = ()
|
||||||
for k, v in cnf.items():
|
for k, v in cnf.items():
|
||||||
|
if k[-1] == '_': k = k[:-1]
|
||||||
if type(v) in CallableTypes:
|
if type(v) in CallableTypes:
|
||||||
v = self._register(v)
|
v = self._register(v)
|
||||||
res = res + ('-'+k, v)
|
res = res + ('-'+k, v)
|
||||||
|
@ -481,13 +425,13 @@ class Misc:
|
||||||
name = tail
|
name = tail
|
||||||
return w
|
return w
|
||||||
def _register(self, func, subst=None):
|
def _register(self, func, subst=None):
|
||||||
f = _CallSafely(func, subst).__call__
|
f = CallWrapper(func, subst, self).__call__
|
||||||
name = `id(f)`
|
name = `id(f)`
|
||||||
if hasattr(func, 'im_func'):
|
if hasattr(func, 'im_func'):
|
||||||
func = func.im_func
|
func = func.im_func
|
||||||
if hasattr(func, 'func_name') and \
|
if hasattr(func, '__name__') and \
|
||||||
type(func.func_name) == type(''):
|
type(func.__name__) == type(''):
|
||||||
name = name + func.func_name
|
name = name + func.__name__
|
||||||
self.tk.createcommand(name, f)
|
self.tk.createcommand(name, f)
|
||||||
return name
|
return name
|
||||||
register = _register
|
register = _register
|
||||||
|
@ -525,25 +469,26 @@ class Misc:
|
||||||
e.x_root = tk.getint(X)
|
e.x_root = tk.getint(X)
|
||||||
e.y_root = tk.getint(Y)
|
e.y_root = tk.getint(Y)
|
||||||
return (e,)
|
return (e,)
|
||||||
|
def _report_exception(self):
|
||||||
|
import sys
|
||||||
|
exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
|
||||||
|
root = self._root()
|
||||||
|
root.report_callback_exception(exc, val, tb)
|
||||||
|
|
||||||
class _CallSafely:
|
class CallWrapper:
|
||||||
def __init__(self, func, subst=None):
|
def __init__(self, func, subst, widget):
|
||||||
self.func = func
|
self.func = func
|
||||||
self.subst = subst
|
self.subst = subst
|
||||||
|
self.widget = widget
|
||||||
def __call__(self, *args):
|
def __call__(self, *args):
|
||||||
if self.subst:
|
|
||||||
args = self.apply_func(self.subst, args)
|
|
||||||
args = self.apply_func(self.func, args)
|
|
||||||
def apply_func(self, func, args):
|
|
||||||
import sys
|
|
||||||
try:
|
try:
|
||||||
return apply(func, args)
|
if self.subst:
|
||||||
|
args = apply(self.subst, args)
|
||||||
|
return apply(self.func, args)
|
||||||
except SystemExit, msg:
|
except SystemExit, msg:
|
||||||
raise SystemExit, msg
|
raise SystemExit, msg
|
||||||
except:
|
except:
|
||||||
import traceback
|
self.widget._report_exception()
|
||||||
print "Exception in Tkinter callback"
|
|
||||||
traceback.print_exc()
|
|
||||||
|
|
||||||
class Wm:
|
class Wm:
|
||||||
def aspect(self,
|
def aspect(self,
|
||||||
|
@ -618,6 +563,7 @@ class Wm:
|
||||||
class Tk(Misc, Wm):
|
class Tk(Misc, Wm):
|
||||||
_w = '.'
|
_w = '.'
|
||||||
def __init__(self, screenName=None, baseName=None, className='Tk'):
|
def __init__(self, screenName=None, baseName=None, className='Tk'):
|
||||||
|
global _default_root
|
||||||
self.master = None
|
self.master = None
|
||||||
self.children = {}
|
self.children = {}
|
||||||
if baseName is None:
|
if baseName is None:
|
||||||
|
@ -628,6 +574,8 @@ class Tk(Misc, Wm):
|
||||||
self.tk.createcommand('tkerror', _tkerror)
|
self.tk.createcommand('tkerror', _tkerror)
|
||||||
self.tk.createcommand('exit', _exit)
|
self.tk.createcommand('exit', _exit)
|
||||||
self.readprofile(baseName, className)
|
self.readprofile(baseName, className)
|
||||||
|
if not _default_root:
|
||||||
|
_default_root = self
|
||||||
def destroy(self):
|
def destroy(self):
|
||||||
for c in self.children.values(): c.destroy()
|
for c in self.children.values(): c.destroy()
|
||||||
self.tk.call('destroy', self._w)
|
self.tk.call('destroy', self._w)
|
||||||
|
@ -657,6 +605,10 @@ class Tk(Misc, Wm):
|
||||||
if os.path.isfile(base_py):
|
if os.path.isfile(base_py):
|
||||||
print 'execfile', `base_py`
|
print 'execfile', `base_py`
|
||||||
execfile(base_py, dir)
|
execfile(base_py, dir)
|
||||||
|
def report_callback_exception(self, exc, val, tb):
|
||||||
|
import traceback
|
||||||
|
print "Exception in Tkinter callback"
|
||||||
|
traceback.print_exception(exc, val, tb)
|
||||||
|
|
||||||
class Pack:
|
class Pack:
|
||||||
def config(self, cnf={}, **kw):
|
def config(self, cnf={}, **kw):
|
||||||
|
@ -682,7 +634,7 @@ class Pack:
|
||||||
info = newinfo
|
info = newinfo
|
||||||
_noarg_ = ['_noarg_']
|
_noarg_ = ['_noarg_']
|
||||||
def propagate(self, flag=_noarg_):
|
def propagate(self, flag=_noarg_):
|
||||||
if boolean is Pack._noarg_:
|
if flag is Pack._noarg_:
|
||||||
return self._getboolean(self.tk.call(
|
return self._getboolean(self.tk.call(
|
||||||
'pack', 'propagate', self._w))
|
'pack', 'propagate', self._w))
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
import tkinter
|
import tkinter
|
||||||
from tkinter import TclError
|
from tkinter import TclError
|
||||||
from types import *
|
from types import *
|
||||||
|
from Tkconstants import *
|
||||||
|
|
||||||
CallableTypes = (FunctionType, MethodType,
|
CallableTypes = (FunctionType, MethodType,
|
||||||
BuiltinFunctionType, BuiltinMethodType)
|
BuiltinFunctionType, BuiltinMethodType)
|
||||||
|
@ -12,64 +13,6 @@ TclVersion = eval(tkinter.TCL_VERSION)
|
||||||
if TkVersion < 4.0:
|
if TkVersion < 4.0:
|
||||||
raise ImportError, "This version of Tkinter.py requires Tk 4.0 or higher"
|
raise ImportError, "This version of Tkinter.py requires Tk 4.0 or higher"
|
||||||
|
|
||||||
# Symbolic constants
|
|
||||||
|
|
||||||
# Booleans
|
|
||||||
NO=FALSE=OFF=0
|
|
||||||
YES=TRUE=ON=1
|
|
||||||
|
|
||||||
# -anchor
|
|
||||||
N='n'
|
|
||||||
S='s'
|
|
||||||
W='w'
|
|
||||||
E='e'
|
|
||||||
NW='nw'
|
|
||||||
SW='sw'
|
|
||||||
NE='ne'
|
|
||||||
SE='se'
|
|
||||||
CENTER='center'
|
|
||||||
|
|
||||||
# -fill
|
|
||||||
NONE='none'
|
|
||||||
X='x'
|
|
||||||
Y='y'
|
|
||||||
BOTH='both'
|
|
||||||
|
|
||||||
# -side
|
|
||||||
LEFT='left'
|
|
||||||
TOP='top'
|
|
||||||
RIGHT='right'
|
|
||||||
BOTTOM='bottom'
|
|
||||||
|
|
||||||
# -relief
|
|
||||||
RAISED='raised'
|
|
||||||
SUNKEN='sunken'
|
|
||||||
FLAT='flat'
|
|
||||||
RIDGE='ridge'
|
|
||||||
GROOVE='groove'
|
|
||||||
|
|
||||||
# -orient
|
|
||||||
HORIZONTAL='horizontal'
|
|
||||||
VERTICAL='vertical'
|
|
||||||
|
|
||||||
# -tabs
|
|
||||||
NUMERIC='numeric'
|
|
||||||
|
|
||||||
# -wrap
|
|
||||||
CHAR='char'
|
|
||||||
WORD='word'
|
|
||||||
|
|
||||||
# -align
|
|
||||||
BASELINE='baseline'
|
|
||||||
|
|
||||||
# Special tags, marks and insert positions
|
|
||||||
SEL='sel'
|
|
||||||
SEL_FIRST='sel.first'
|
|
||||||
SEL_LAST='sel.last'
|
|
||||||
END='end'
|
|
||||||
INSERT='insert'
|
|
||||||
CURRENT='current'
|
|
||||||
ANCHOR='anchor'
|
|
||||||
|
|
||||||
def _flatten(tuple):
|
def _flatten(tuple):
|
||||||
res = ()
|
res = ()
|
||||||
|
@ -461,6 +404,7 @@ class Misc:
|
||||||
cnf = _cnfmerge(cnf)
|
cnf = _cnfmerge(cnf)
|
||||||
res = ()
|
res = ()
|
||||||
for k, v in cnf.items():
|
for k, v in cnf.items():
|
||||||
|
if k[-1] == '_': k = k[:-1]
|
||||||
if type(v) in CallableTypes:
|
if type(v) in CallableTypes:
|
||||||
v = self._register(v)
|
v = self._register(v)
|
||||||
res = res + ('-'+k, v)
|
res = res + ('-'+k, v)
|
||||||
|
@ -481,13 +425,13 @@ class Misc:
|
||||||
name = tail
|
name = tail
|
||||||
return w
|
return w
|
||||||
def _register(self, func, subst=None):
|
def _register(self, func, subst=None):
|
||||||
f = _CallSafely(func, subst).__call__
|
f = CallWrapper(func, subst, self).__call__
|
||||||
name = `id(f)`
|
name = `id(f)`
|
||||||
if hasattr(func, 'im_func'):
|
if hasattr(func, 'im_func'):
|
||||||
func = func.im_func
|
func = func.im_func
|
||||||
if hasattr(func, 'func_name') and \
|
if hasattr(func, '__name__') and \
|
||||||
type(func.func_name) == type(''):
|
type(func.__name__) == type(''):
|
||||||
name = name + func.func_name
|
name = name + func.__name__
|
||||||
self.tk.createcommand(name, f)
|
self.tk.createcommand(name, f)
|
||||||
return name
|
return name
|
||||||
register = _register
|
register = _register
|
||||||
|
@ -525,25 +469,26 @@ class Misc:
|
||||||
e.x_root = tk.getint(X)
|
e.x_root = tk.getint(X)
|
||||||
e.y_root = tk.getint(Y)
|
e.y_root = tk.getint(Y)
|
||||||
return (e,)
|
return (e,)
|
||||||
|
def _report_exception(self):
|
||||||
|
import sys
|
||||||
|
exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
|
||||||
|
root = self._root()
|
||||||
|
root.report_callback_exception(exc, val, tb)
|
||||||
|
|
||||||
class _CallSafely:
|
class CallWrapper:
|
||||||
def __init__(self, func, subst=None):
|
def __init__(self, func, subst, widget):
|
||||||
self.func = func
|
self.func = func
|
||||||
self.subst = subst
|
self.subst = subst
|
||||||
|
self.widget = widget
|
||||||
def __call__(self, *args):
|
def __call__(self, *args):
|
||||||
if self.subst:
|
|
||||||
args = self.apply_func(self.subst, args)
|
|
||||||
args = self.apply_func(self.func, args)
|
|
||||||
def apply_func(self, func, args):
|
|
||||||
import sys
|
|
||||||
try:
|
try:
|
||||||
return apply(func, args)
|
if self.subst:
|
||||||
|
args = apply(self.subst, args)
|
||||||
|
return apply(self.func, args)
|
||||||
except SystemExit, msg:
|
except SystemExit, msg:
|
||||||
raise SystemExit, msg
|
raise SystemExit, msg
|
||||||
except:
|
except:
|
||||||
import traceback
|
self.widget._report_exception()
|
||||||
print "Exception in Tkinter callback"
|
|
||||||
traceback.print_exc()
|
|
||||||
|
|
||||||
class Wm:
|
class Wm:
|
||||||
def aspect(self,
|
def aspect(self,
|
||||||
|
@ -618,6 +563,7 @@ class Wm:
|
||||||
class Tk(Misc, Wm):
|
class Tk(Misc, Wm):
|
||||||
_w = '.'
|
_w = '.'
|
||||||
def __init__(self, screenName=None, baseName=None, className='Tk'):
|
def __init__(self, screenName=None, baseName=None, className='Tk'):
|
||||||
|
global _default_root
|
||||||
self.master = None
|
self.master = None
|
||||||
self.children = {}
|
self.children = {}
|
||||||
if baseName is None:
|
if baseName is None:
|
||||||
|
@ -628,6 +574,8 @@ class Tk(Misc, Wm):
|
||||||
self.tk.createcommand('tkerror', _tkerror)
|
self.tk.createcommand('tkerror', _tkerror)
|
||||||
self.tk.createcommand('exit', _exit)
|
self.tk.createcommand('exit', _exit)
|
||||||
self.readprofile(baseName, className)
|
self.readprofile(baseName, className)
|
||||||
|
if not _default_root:
|
||||||
|
_default_root = self
|
||||||
def destroy(self):
|
def destroy(self):
|
||||||
for c in self.children.values(): c.destroy()
|
for c in self.children.values(): c.destroy()
|
||||||
self.tk.call('destroy', self._w)
|
self.tk.call('destroy', self._w)
|
||||||
|
@ -657,6 +605,10 @@ class Tk(Misc, Wm):
|
||||||
if os.path.isfile(base_py):
|
if os.path.isfile(base_py):
|
||||||
print 'execfile', `base_py`
|
print 'execfile', `base_py`
|
||||||
execfile(base_py, dir)
|
execfile(base_py, dir)
|
||||||
|
def report_callback_exception(self, exc, val, tb):
|
||||||
|
import traceback
|
||||||
|
print "Exception in Tkinter callback"
|
||||||
|
traceback.print_exception(exc, val, tb)
|
||||||
|
|
||||||
class Pack:
|
class Pack:
|
||||||
def config(self, cnf={}, **kw):
|
def config(self, cnf={}, **kw):
|
||||||
|
@ -682,7 +634,7 @@ class Pack:
|
||||||
info = newinfo
|
info = newinfo
|
||||||
_noarg_ = ['_noarg_']
|
_noarg_ = ['_noarg_']
|
||||||
def propagate(self, flag=_noarg_):
|
def propagate(self, flag=_noarg_):
|
||||||
if boolean is Pack._noarg_:
|
if flag is Pack._noarg_:
|
||||||
return self._getboolean(self.tk.call(
|
return self._getboolean(self.tk.call(
|
||||||
'pack', 'propagate', self._w))
|
'pack', 'propagate', self._w))
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue