Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/lib-tk/Tkinter.py

    r2 r391  
    3131"""
    3232
    33 __version__ = "$Revision: 73770 $"
     33__version__ = "$Revision: 81008 $"
    3434
    3535import sys
     
    4242from types import *
    4343from Tkconstants import *
     44import re
    4445
    4546wantobjects = 1
     
    5859except AttributeError: _tkinter.deletefilehandler = None
    5960
     61
     62_magic_re = re.compile(r'([\\{}])')
     63_space_re = re.compile(r'([\s])')
     64
     65def _join(value):
     66    """Internal function."""
     67    return ' '.join(map(_stringify, value))
     68
     69def _stringify(value):
     70    """Internal function."""
     71    if isinstance(value, (list, tuple)):
     72        if len(value) == 1:
     73            value = _stringify(value[0])
     74            if value[0] == '{':
     75                value = '{%s}' % value
     76        else:
     77            value = '{%s}' % _join(value)
     78    else:
     79        if isinstance(value, basestring):
     80            value = unicode(value)
     81        else:
     82            value = str(value)
     83        if not value:
     84            value = '{}'
     85        elif _magic_re.search(value):
     86            # add '\' before special characters and spaces
     87            value = _magic_re.sub(r'\\\1', value)
     88            value = _space_re.sub(r'\\\1', value)
     89        elif value[0] == '"' or _space_re.search(value):
     90            value = '{%s}' % value
     91    return value
    6092
    6193def _flatten(tuple):
     
    155187    pass
    156188
    157 def _exit(code='0'):
    158     """Internal function. Calling it will throw the exception SystemExit."""
     189def _exit(code=0):
     190    """Internal function. Calling it will raise the exception SystemExit."""
     191    try:
     192        code = int(code)
     193    except ValueError:
     194        pass
    159195    raise SystemExit, code
    160196
     
    535571        The type keyword specifies the form in which the data is
    536572        to be returned and should be an atom name such as STRING
    537         or FILE_NAME.  Type defaults to STRING.
     573        or FILE_NAME.  Type defaults to STRING, except on X11, where the default
     574        is to try UTF8_STRING and fall back to STRING.
    538575
    539576        This command is equivalent to:
     
    541578        selection_get(CLIPBOARD)
    542579        """
     580        if 'type' not in kw and self._windowingsystem == 'x11':
     581            try:
     582                kw['type'] = 'UTF8_STRING'
     583                return self.tk.call(('clipboard', 'get') + self._options(kw))
     584            except TclError:
     585                del kw['type']
    543586        return self.tk.call(('clipboard', 'get') + self._options(kw))
    544587
     
    548591        A widget specified for the optional displayof keyword
    549592        argument specifies the target display."""
    550         if not kw.has_key('displayof'): kw['displayof'] = self._w
     593        if 'displayof' not in kw: kw['displayof'] = self._w
    551594        self.tk.call(('clipboard', 'clear') + self._options(kw))
    552595    def clipboard_append(self, string, **kw):
     
    556599        argument specifies the target display. The clipboard
    557600        can be retrieved with selection_get."""
    558         if not kw.has_key('displayof'): kw['displayof'] = self._w
     601        if 'displayof' not in kw: kw['displayof'] = self._w
    559602        self.tk.call(('clipboard', 'append') + self._options(kw)
    560603              + ('--', string))
     
    614657    def selection_clear(self, **kw):
    615658        """Clear the current X selection."""
    616         if not kw.has_key('displayof'): kw['displayof'] = self._w
     659        if 'displayof' not in kw: kw['displayof'] = self._w
    617660        self.tk.call(('selection', 'clear') + self._options(kw))
    618661    def selection_get(self, **kw):
     
    622665        the selection and defaults to PRIMARY.  A keyword
    623666        parameter displayof specifies a widget on the display
    624         to use."""
    625         if not kw.has_key('displayof'): kw['displayof'] = self._w
     667        to use. A keyword parameter type specifies the form of data to be
     668        fetched, defaulting to STRING except on X11, where UTF8_STRING is tried
     669        before STRING."""
     670        if 'displayof' not in kw: kw['displayof'] = self._w
     671        if 'type' not in kw and self._windowingsystem == 'x11':
     672            try:
     673                kw['type'] = 'UTF8_STRING'
     674                return self.tk.call(('selection', 'get') + self._options(kw))
     675            except TclError:
     676                del kw['type']
    626677        return self.tk.call(('selection', 'get') + self._options(kw))
    627678    def selection_handle(self, command, **kw):
     
    654705        selection - name of the selection (default PRIMARY),
    655706        type - type of the selection (e.g. STRING, FILE_NAME)."""
    656         if not kw.has_key('displayof'): kw['displayof'] = self._w
     707        if 'displayof' not in kw: kw['displayof'] = self._w
    657708        name = self.tk.call(('selection', 'own') + self._options(kw))
    658709        if not name: return None
     
    10381089            return ('-displayof', self._w)
    10391090        return ()
     1091    @property
     1092    def _windowingsystem(self):
     1093        """Internal function."""
     1094        try:
     1095            return self._root()._windowingsystem_cached
     1096        except AttributeError:
     1097            ws = self._root()._windowingsystem_cached = \
     1098                        self.tk.call('tk', 'windowingsystem')
     1099            return ws
    10401100    def _options(self, cnf, kw = None):
    10411101        """Internal function."""
     
    10481108            if v is not None:
    10491109                if k[-1] == '_': k = k[:-1]
    1050                 if callable(v):
     1110                if hasattr(v, '__call__'):
    10511111                    v = self._register(v)
    10521112                elif isinstance(v, (tuple, list)):
     
    10591119                        else:
    10601120                            # format it to proper Tcl code if it contains space
    1061                             nv.append(('{%s}' if ' ' in item else '%s') % item)
     1121                            nv.append(_stringify(item))
    10621122                    else:
    10631123                        v = ' '.join(nv)
     
    12881348                if not value:
    12891349                    value = None
    1290                 elif '.' in value:
     1350                elif '.' in str(value):
    12911351                    value = getdouble(value)
    12921352                else:
     
    14131473        except:
    14141474            self.widget._report_exception()
     1475
     1476
     1477class XView:
     1478    """Mix-in class for querying and changing the horizontal position
     1479    of a widget's window."""
     1480
     1481    def xview(self, *args):
     1482        """Query and change the horizontal position of the view."""
     1483        res = self.tk.call(self._w, 'xview', *args)
     1484        if not args:
     1485            return self._getdoubles(res)
     1486
     1487    def xview_moveto(self, fraction):
     1488        """Adjusts the view in the window so that FRACTION of the
     1489        total width of the canvas is off-screen to the left."""
     1490        self.tk.call(self._w, 'xview', 'moveto', fraction)
     1491
     1492    def xview_scroll(self, number, what):
     1493        """Shift the x-view according to NUMBER which is measured in "units"
     1494        or "pages" (WHAT)."""
     1495        self.tk.call(self._w, 'xview', 'scroll', number, what)
     1496
     1497
     1498class YView:
     1499    """Mix-in class for querying and changing the vertical position
     1500    of a widget's window."""
     1501
     1502    def yview(self, *args):
     1503        """Query and change the vertical position of the view."""
     1504        res = self.tk.call(self._w, 'yview', *args)
     1505        if not args:
     1506            return self._getdoubles(res)
     1507
     1508    def yview_moveto(self, fraction):
     1509        """Adjusts the view in the window so that FRACTION of the
     1510        total height of the canvas is off-screen to the top."""
     1511        self.tk.call(self._w, 'yview', 'moveto', fraction)
     1512
     1513    def yview_scroll(self, number, what):
     1514        """Shift the y-view according to NUMBER which is measured in
     1515        "units" or "pages" (WHAT)."""
     1516        self.tk.call(self._w, 'yview', 'scroll', number, what)
    14151517
    14161518
     
    16191721class Tk(Misc, Wm):
    16201722    """Toplevel widget of Tk which represents mostly the main window
    1621     of an appliation. It has an associated Tcl interpreter."""
     1723    of an application. It has an associated Tcl interpreter."""
    16221724    _w = '.'
    16231725    def __init__(self, screenName=None, baseName=None, className='Tk',
     
    16351737        self.tk = None
    16361738        if baseName is None:
    1637             import sys, os
     1739            import os
    16381740            baseName = os.path.basename(sys.argv[0])
    16391741            baseName, ext = os.path.splitext(baseName)
     
    16441746        if useTk:
    16451747            self._loadtk()
    1646         self.readprofile(baseName, className)
     1748        if not sys.flags.ignore_environment:
     1749            # Issue #16248: Honor the -E flag to avoid code injection.
     1750            self.readprofile(baseName, className)
    16471751    def loadtk(self):
    16481752        if not self._tkloaded:
     
    16941798        such a file exists in the home directory."""
    16951799        import os
    1696         if os.environ.has_key('HOME'): home = os.environ['HOME']
     1800        if 'HOME' in os.environ: home = os.environ['HOME']
    16971801        else: home = os.curdir
    16981802        class_tcl = os.path.join(home, '.%s.tcl' % className)
     
    17771881            key = words[i][1:]
    17781882            value = words[i+1]
    1779             if value[:1] == '.':
     1883            if str(value)[:1] == '.':
    17801884                value = self._nametowidget(value)
    17811885            dict[key] = value
     
    18281932            key = words[i][1:]
    18291933            value = words[i+1]
    1830             if value[:1] == '.':
     1934            if str(value)[:1] == '.':
    18311935                value = self._nametowidget(value)
    18321936            dict[key] = value
     
    18771981            key = words[i][1:]
    18781982            value = words[i+1]
    1879             if value[:1] == '.':
     1983            if str(value)[:1] == '.':
    18801984                value = self._nametowidget(value)
    18811985            dict[key] = value
     
    19012005        self.tk = master.tk
    19022006        name = None
    1903         if cnf.has_key('name'):
     2007        if 'name' in cnf:
    19042008            name = cnf['name']
    19052009            del cnf['name']
     
    19122016            self._w = master._w + '.' + name
    19132017        self.children = {}
    1914         if self.master.children.has_key(self._name):
     2018        if self._name in self.master.children:
    19152019            self.master.children[self._name].destroy()
    19162020        self.master.children[self._name] = self
     
    19372041        for c in self.children.values(): c.destroy()
    19382042        self.tk.call('destroy', self._w)
    1939         if self.master.children.has_key(self._name):
     2043        if self._name in self.master.children:
    19402044            del self.master.children[self._name]
    19412045        Misc.destroy(self)
     
    19652069        for wmkey in ['screen', 'class_', 'class', 'visual',
    19662070                  'colormap']:
    1967             if cnf.has_key(wmkey):
     2071            if wmkey in cnf:
    19682072                val = cnf[wmkey]
    19692073                # TBD: a hack needed because some keys
     
    20582162        return '@%r,%r' % (x, y)
    20592163
    2060 class Canvas(Widget):
     2164class Canvas(Widget, XView, YView):
    20612165    """Canvas widget to display graphical elements like lines or text."""
    20622166    def __init__(self, master=None, cnf={}, **kw):
     
    22982402        """Return the type of the item TAGORID."""
    22992403        return self.tk.call(self._w, 'type', tagOrId) or None
    2300     def xview(self, *args):
    2301         """Query and change horizontal position of the view."""
    2302         if not args:
    2303             return self._getdoubles(self.tk.call(self._w, 'xview'))
    2304         self.tk.call((self._w, 'xview') + args)
    2305     def xview_moveto(self, fraction):
    2306         """Adjusts the view in the window so that FRACTION of the
    2307         total width of the canvas is off-screen to the left."""
    2308         self.tk.call(self._w, 'xview', 'moveto', fraction)
    2309     def xview_scroll(self, number, what):
    2310         """Shift the x-view according to NUMBER which is measured in "units" or "pages" (WHAT)."""
    2311         self.tk.call(self._w, 'xview', 'scroll', number, what)
    2312     def yview(self, *args):
    2313         """Query and change vertical position of the view."""
    2314         if not args:
    2315             return self._getdoubles(self.tk.call(self._w, 'yview'))
    2316         self.tk.call((self._w, 'yview') + args)
    2317     def yview_moveto(self, fraction):
    2318         """Adjusts the view in the window so that FRACTION of the
    2319         total height of the canvas is off-screen to the top."""
    2320         self.tk.call(self._w, 'yview', 'moveto', fraction)
    2321     def yview_scroll(self, number, what):
    2322         """Shift the y-view according to NUMBER which is measured in "units" or "pages" (WHAT)."""
    2323         self.tk.call(self._w, 'yview', 'scroll', number, what)
    23242404
    23252405class Checkbutton(Widget):
     
    23522432        self.tk.call(self._w, 'toggle')
    23532433
    2354 class Entry(Widget):
     2434class Entry(Widget, XView):
    23552435    """Entry widget which allows to display simple text."""
    23562436    def __init__(self, master=None, cnf={}, **kw):
     
    24032483    select_from = selection_from
    24042484    def selection_present(self):
    2405         """Return whether the widget has the selection."""
     2485        """Return True if there are characters selected in the entry, False
     2486        otherwise."""
    24062487        return self.tk.getboolean(
    24072488            self.tk.call(self._w, 'selection', 'present'))
     
    24152496        self.tk.call(self._w, 'selection', 'to', index)
    24162497    select_to = selection_to
    2417     def xview(self, index):
    2418         """Query and change horizontal position of the view."""
    2419         self.tk.call(self._w, 'xview', index)
    2420     def xview_moveto(self, fraction):
    2421         """Adjust the view in the window so that FRACTION of the
    2422         total width of the entry is off-screen to the left."""
    2423         self.tk.call(self._w, 'xview', 'moveto', fraction)
    2424     def xview_scroll(self, number, what):
    2425         """Shift the x-view according to NUMBER which is measured in "units" or "pages" (WHAT)."""
    2426         self.tk.call(self._w, 'xview', 'scroll', number, what)
    24272498
    24282499class Frame(Widget):
     
    24362507        cnf = _cnfmerge((cnf, kw))
    24372508        extra = ()
    2438         if cnf.has_key('class_'):
     2509        if 'class_' in cnf:
    24392510            extra = ('-class', cnf['class_'])
    24402511            del cnf['class_']
    2441         elif cnf.has_key('class'):
     2512        elif 'class' in cnf:
    24422513            extra = ('-class', cnf['class'])
    24432514            del cnf['class']
     
    24662537        Widget.__init__(self, master, 'label', cnf, kw)
    24672538
    2468 class Listbox(Widget):
     2539class Listbox(Widget, XView, YView):
    24692540    """Listbox widget which can display a list of strings."""
    24702541    def __init__(self, master=None, cnf={}, **kw):
     
    25452616        """Return the number of elements in the listbox."""
    25462617        return getint(self.tk.call(self._w, 'size'))
    2547     def xview(self, *what):
    2548         """Query and change horizontal position of the view."""
    2549         if not what:
    2550             return self._getdoubles(self.tk.call(self._w, 'xview'))
    2551         self.tk.call((self._w, 'xview') + what)
    2552     def xview_moveto(self, fraction):
    2553         """Adjust the view in the window so that FRACTION of the
    2554         total width of the entry is off-screen to the left."""
    2555         self.tk.call(self._w, 'xview', 'moveto', fraction)
    2556     def xview_scroll(self, number, what):
    2557         """Shift the x-view according to NUMBER which is measured in "units" or "pages" (WHAT)."""
    2558         self.tk.call(self._w, 'xview', 'scroll', number, what)
    2559     def yview(self, *what):
    2560         """Query and change vertical position of the view."""
    2561         if not what:
    2562             return self._getdoubles(self.tk.call(self._w, 'yview'))
    2563         self.tk.call((self._w, 'yview') + what)
    2564     def yview_moveto(self, fraction):
    2565         """Adjust the view in the window so that FRACTION of the
    2566         total width of the entry is off-screen to the top."""
    2567         self.tk.call(self._w, 'yview', 'moveto', fraction)
    2568     def yview_scroll(self, number, what):
    2569         """Shift the y-view according to NUMBER which is measured in "units" or "pages" (WHAT)."""
    2570         self.tk.call(self._w, 'yview', 'scroll', number, what)
    25712618    def itemcget(self, index, option):
    25722619        """Return the resource value for an ITEM and an OPTION."""
     
    28152862
    28162863
    2817 class Text(Widget):
     2864class Text(Widget, XView, YView):
    28182865    """Text widget which can display text in various forms."""
    28192866    def __init__(self, master=None, cnf={}, **kw):
     
    31373184        return self.tk.splitlist(
    31383185            self.tk.call(self._w, 'window', 'names'))
    3139     def xview(self, *what):
    3140         """Query and change horizontal position of the view."""
    3141         if not what:
    3142             return self._getdoubles(self.tk.call(self._w, 'xview'))
    3143         self.tk.call((self._w, 'xview') + what)
    3144     def xview_moveto(self, fraction):
    3145         """Adjusts the view in the window so that FRACTION of the
    3146         total width of the canvas is off-screen to the left."""
    3147         self.tk.call(self._w, 'xview', 'moveto', fraction)
    3148     def xview_scroll(self, number, what):
    3149         """Shift the x-view according to NUMBER which is measured
    3150         in "units" or "pages" (WHAT)."""
    3151         self.tk.call(self._w, 'xview', 'scroll', number, what)
    3152     def yview(self, *what):
    3153         """Query and change vertical position of the view."""
    3154         if not what:
    3155             return self._getdoubles(self.tk.call(self._w, 'yview'))
    3156         self.tk.call((self._w, 'yview') + what)
    3157     def yview_moveto(self, fraction):
    3158         """Adjusts the view in the window so that FRACTION of the
    3159         total height of the canvas is off-screen to the top."""
    3160         self.tk.call(self._w, 'yview', 'moveto', fraction)
    3161     def yview_scroll(self, number, what):
    3162         """Shift the y-view according to NUMBER which is measured
    3163         in "units" or "pages" (WHAT)."""
    3164         self.tk.call(self._w, 'yview', 'scroll', number, what)
    31653186    def yview_pickplace(self, *what):
    31663187        """Obsolete function, use see."""
     
    31953216        # 'command' is the only supported keyword
    31963217        callback = kwargs.get('command')
    3197         if kwargs.has_key('command'):
     3218        if 'command' in kwargs:
    31983219            del kwargs['command']
    31993220        if kwargs:
     
    32363257        options = ()
    32373258        for k, v in cnf.items():
    3238             if callable(v):
     3259            if hasattr(v, '__call__'):
    32393260                v = self._register(v)
    32403261            options = options + ('-'+k, v)
     
    32593280            if v is not None:
    32603281                if k[-1] == '_': k = k[:-1]
    3261                 if callable(v):
     3282                if hasattr(v, '__call__'):
    32623283                    v = self._register(v)
    32633284                res = res + ('-'+k, v)
     
    33483369
    33493370
    3350 class Spinbox(Widget):
     3371class Spinbox(Widget, XView):
    33513372    """spinbox widget."""
    33523373    def __init__(self, master=None, cnf={}, **kw):
     
    35513572        The child argument is the name of the child widget
    35523573        followed by pairs of arguments that specify how to
    3553         manage the windows. Options may have any of the values
    3554         accepted by the configure subcommand.
     3574        manage the windows. The possible options and values
     3575        are the ones accepted by the paneconfigure method.
    35553576        """
    35563577        self.tk.call((self._w, 'add', child) + self._options(kw))
Note: See TracChangeset for help on using the changeset viewer.