Changeset 391 for python/trunk/Lib/lib-tk/Tkinter.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/lib-tk/Tkinter.py
r2 r391 31 31 """ 32 32 33 __version__ = "$Revision: 73770$"33 __version__ = "$Revision: 81008 $" 34 34 35 35 import sys … … 42 42 from types import * 43 43 from Tkconstants import * 44 import re 44 45 45 46 wantobjects = 1 … … 58 59 except AttributeError: _tkinter.deletefilehandler = None 59 60 61 62 _magic_re = re.compile(r'([\\{}])') 63 _space_re = re.compile(r'([\s])') 64 65 def _join(value): 66 """Internal function.""" 67 return ' '.join(map(_stringify, value)) 68 69 def _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 60 92 61 93 def _flatten(tuple): … … 155 187 pass 156 188 157 def _exit(code='0'): 158 """Internal function. Calling it will throw the exception SystemExit.""" 189 def _exit(code=0): 190 """Internal function. Calling it will raise the exception SystemExit.""" 191 try: 192 code = int(code) 193 except ValueError: 194 pass 159 195 raise SystemExit, code 160 196 … … 535 571 The type keyword specifies the form in which the data is 536 572 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. 538 575 539 576 This command is equivalent to: … … 541 578 selection_get(CLIPBOARD) 542 579 """ 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'] 543 586 return self.tk.call(('clipboard', 'get') + self._options(kw)) 544 587 … … 548 591 A widget specified for the optional displayof keyword 549 592 argument specifies the target display.""" 550 if not kw.has_key('displayof'): kw['displayof'] = self._w593 if 'displayof' not in kw: kw['displayof'] = self._w 551 594 self.tk.call(('clipboard', 'clear') + self._options(kw)) 552 595 def clipboard_append(self, string, **kw): … … 556 599 argument specifies the target display. The clipboard 557 600 can be retrieved with selection_get.""" 558 if not kw.has_key('displayof'): kw['displayof'] = self._w601 if 'displayof' not in kw: kw['displayof'] = self._w 559 602 self.tk.call(('clipboard', 'append') + self._options(kw) 560 603 + ('--', string)) … … 614 657 def selection_clear(self, **kw): 615 658 """Clear the current X selection.""" 616 if not kw.has_key('displayof'): kw['displayof'] = self._w659 if 'displayof' not in kw: kw['displayof'] = self._w 617 660 self.tk.call(('selection', 'clear') + self._options(kw)) 618 661 def selection_get(self, **kw): … … 622 665 the selection and defaults to PRIMARY. A keyword 623 666 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'] 626 677 return self.tk.call(('selection', 'get') + self._options(kw)) 627 678 def selection_handle(self, command, **kw): … … 654 705 selection - name of the selection (default PRIMARY), 655 706 type - type of the selection (e.g. STRING, FILE_NAME).""" 656 if not kw.has_key('displayof'): kw['displayof'] = self._w707 if 'displayof' not in kw: kw['displayof'] = self._w 657 708 name = self.tk.call(('selection', 'own') + self._options(kw)) 658 709 if not name: return None … … 1038 1089 return ('-displayof', self._w) 1039 1090 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 1040 1100 def _options(self, cnf, kw = None): 1041 1101 """Internal function.""" … … 1048 1108 if v is not None: 1049 1109 if k[-1] == '_': k = k[:-1] 1050 if callable(v):1110 if hasattr(v, '__call__'): 1051 1111 v = self._register(v) 1052 1112 elif isinstance(v, (tuple, list)): … … 1059 1119 else: 1060 1120 # 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)) 1062 1122 else: 1063 1123 v = ' '.join(nv) … … 1288 1348 if not value: 1289 1349 value = None 1290 elif '.' in value:1350 elif '.' in str(value): 1291 1351 value = getdouble(value) 1292 1352 else: … … 1413 1473 except: 1414 1474 self.widget._report_exception() 1475 1476 1477 class 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 1498 class 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) 1415 1517 1416 1518 … … 1619 1721 class Tk(Misc, Wm): 1620 1722 """Toplevel widget of Tk which represents mostly the main window 1621 of an appli ation. It has an associated Tcl interpreter."""1723 of an application. It has an associated Tcl interpreter.""" 1622 1724 _w = '.' 1623 1725 def __init__(self, screenName=None, baseName=None, className='Tk', … … 1635 1737 self.tk = None 1636 1738 if baseName is None: 1637 import sys,os1739 import os 1638 1740 baseName = os.path.basename(sys.argv[0]) 1639 1741 baseName, ext = os.path.splitext(baseName) … … 1644 1746 if useTk: 1645 1747 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) 1647 1751 def loadtk(self): 1648 1752 if not self._tkloaded: … … 1694 1798 such a file exists in the home directory.""" 1695 1799 import os 1696 if os.environ.has_key('HOME'): home = os.environ['HOME']1800 if 'HOME' in os.environ: home = os.environ['HOME'] 1697 1801 else: home = os.curdir 1698 1802 class_tcl = os.path.join(home, '.%s.tcl' % className) … … 1777 1881 key = words[i][1:] 1778 1882 value = words[i+1] 1779 if value[:1] == '.':1883 if str(value)[:1] == '.': 1780 1884 value = self._nametowidget(value) 1781 1885 dict[key] = value … … 1828 1932 key = words[i][1:] 1829 1933 value = words[i+1] 1830 if value[:1] == '.':1934 if str(value)[:1] == '.': 1831 1935 value = self._nametowidget(value) 1832 1936 dict[key] = value … … 1877 1981 key = words[i][1:] 1878 1982 value = words[i+1] 1879 if value[:1] == '.':1983 if str(value)[:1] == '.': 1880 1984 value = self._nametowidget(value) 1881 1985 dict[key] = value … … 1901 2005 self.tk = master.tk 1902 2006 name = None 1903 if cnf.has_key('name'):2007 if 'name' in cnf: 1904 2008 name = cnf['name'] 1905 2009 del cnf['name'] … … 1912 2016 self._w = master._w + '.' + name 1913 2017 self.children = {} 1914 if self. master.children.has_key(self._name):2018 if self._name in self.master.children: 1915 2019 self.master.children[self._name].destroy() 1916 2020 self.master.children[self._name] = self … … 1937 2041 for c in self.children.values(): c.destroy() 1938 2042 self.tk.call('destroy', self._w) 1939 if self. master.children.has_key(self._name):2043 if self._name in self.master.children: 1940 2044 del self.master.children[self._name] 1941 2045 Misc.destroy(self) … … 1965 2069 for wmkey in ['screen', 'class_', 'class', 'visual', 1966 2070 'colormap']: 1967 if cnf.has_key(wmkey):2071 if wmkey in cnf: 1968 2072 val = cnf[wmkey] 1969 2073 # TBD: a hack needed because some keys … … 2058 2162 return '@%r,%r' % (x, y) 2059 2163 2060 class Canvas(Widget ):2164 class Canvas(Widget, XView, YView): 2061 2165 """Canvas widget to display graphical elements like lines or text.""" 2062 2166 def __init__(self, master=None, cnf={}, **kw): … … 2298 2402 """Return the type of the item TAGORID.""" 2299 2403 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 the2307 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 the2319 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)2324 2404 2325 2405 class Checkbutton(Widget): … … 2352 2432 self.tk.call(self._w, 'toggle') 2353 2433 2354 class Entry(Widget ):2434 class Entry(Widget, XView): 2355 2435 """Entry widget which allows to display simple text.""" 2356 2436 def __init__(self, master=None, cnf={}, **kw): … … 2403 2483 select_from = selection_from 2404 2484 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.""" 2406 2487 return self.tk.getboolean( 2407 2488 self.tk.call(self._w, 'selection', 'present')) … … 2415 2496 self.tk.call(self._w, 'selection', 'to', index) 2416 2497 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 the2422 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)2427 2498 2428 2499 class Frame(Widget): … … 2436 2507 cnf = _cnfmerge((cnf, kw)) 2437 2508 extra = () 2438 if cnf.has_key('class_'):2509 if 'class_' in cnf: 2439 2510 extra = ('-class', cnf['class_']) 2440 2511 del cnf['class_'] 2441 elif cnf.has_key('class'):2512 elif 'class' in cnf: 2442 2513 extra = ('-class', cnf['class']) 2443 2514 del cnf['class'] … … 2466 2537 Widget.__init__(self, master, 'label', cnf, kw) 2467 2538 2468 class Listbox(Widget ):2539 class Listbox(Widget, XView, YView): 2469 2540 """Listbox widget which can display a list of strings.""" 2470 2541 def __init__(self, master=None, cnf={}, **kw): … … 2545 2616 """Return the number of elements in the listbox.""" 2546 2617 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 the2554 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 the2566 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)2571 2618 def itemcget(self, index, option): 2572 2619 """Return the resource value for an ITEM and an OPTION.""" … … 2815 2862 2816 2863 2817 class Text(Widget ):2864 class Text(Widget, XView, YView): 2818 2865 """Text widget which can display text in various forms.""" 2819 2866 def __init__(self, master=None, cnf={}, **kw): … … 3137 3184 return self.tk.splitlist( 3138 3185 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 the3146 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 measured3150 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 the3159 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 measured3163 in "units" or "pages" (WHAT)."""3164 self.tk.call(self._w, 'yview', 'scroll', number, what)3165 3186 def yview_pickplace(self, *what): 3166 3187 """Obsolete function, use see.""" … … 3195 3216 # 'command' is the only supported keyword 3196 3217 callback = kwargs.get('command') 3197 if kwargs.has_key('command'):3218 if 'command' in kwargs: 3198 3219 del kwargs['command'] 3199 3220 if kwargs: … … 3236 3257 options = () 3237 3258 for k, v in cnf.items(): 3238 if callable(v):3259 if hasattr(v, '__call__'): 3239 3260 v = self._register(v) 3240 3261 options = options + ('-'+k, v) … … 3259 3280 if v is not None: 3260 3281 if k[-1] == '_': k = k[:-1] 3261 if callable(v):3282 if hasattr(v, '__call__'): 3262 3283 v = self._register(v) 3263 3284 res = res + ('-'+k, v) … … 3348 3369 3349 3370 3350 class Spinbox(Widget ):3371 class Spinbox(Widget, XView): 3351 3372 """spinbox widget.""" 3352 3373 def __init__(self, master=None, cnf={}, **kw): … … 3551 3572 The child argument is the name of the child widget 3552 3573 followed by pairs of arguments that specify how to 3553 manage the windows. Options may have any of thevalues3554 a ccepted by the configure subcommand.3574 manage the windows. The possible options and values 3575 are the ones accepted by the paneconfigure method. 3555 3576 """ 3556 3577 self.tk.call((self._w, 'add', child) + self._options(kw))
Note:
See TracChangeset
for help on using the changeset viewer.