home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / lib-tk / tkFont.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  6.5 KB  |  208 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. __version__ = '0.9'
  5. import Tkinter
  6. NORMAL = 'normal'
  7. ROMAN = 'roman'
  8. BOLD = 'bold'
  9. ITALIC = 'italic'
  10.  
  11. def nametofont(name):
  12.     '''Given the name of a tk named font, returns a Font representation.
  13.     '''
  14.     return Font(name = name, exists = True)
  15.  
  16.  
  17. class Font:
  18.     """Represents a named font.
  19.  
  20.     Constructor options are:
  21.  
  22.     font -- font specifier (name, system font, or (family, size, style)-tuple)
  23.     name -- name to use for this font configuration (defaults to a unique name)
  24.     exists -- does a named font by this name already exist?
  25.        Creates a new named font if False, points to the existing font if True.
  26.        Raises _tkinter.TclError if the assertion is false.
  27.  
  28.        the following are ignored if font is specified:
  29.  
  30.     family -- font 'family', e.g. Courier, Times, Helvetica
  31.     size -- font size in points
  32.     weight -- font thickness: NORMAL, BOLD
  33.     slant -- font slant: ROMAN, ITALIC
  34.     underline -- font underlining: false (0), true (1)
  35.     overstrike -- font strikeout: false (0), true (1)
  36.  
  37.     """
  38.     
  39.     def _set(self, kw):
  40.         options = []
  41.         for k, v in kw.items():
  42.             options.append('-' + k)
  43.             options.append(str(v))
  44.         
  45.         return tuple(options)
  46.  
  47.     
  48.     def _get(self, args):
  49.         options = []
  50.         for k in args:
  51.             options.append('-' + k)
  52.         
  53.         return tuple(options)
  54.  
  55.     
  56.     def _mkdict(self, args):
  57.         options = { }
  58.         for i in range(0, len(args), 2):
  59.             options[args[i][1:]] = args[i + 1]
  60.         
  61.         return options
  62.  
  63.     
  64.     def __init__(self, root = None, font = None, name = None, exists = False, **options):
  65.         if not root:
  66.             root = Tkinter._default_root
  67.         
  68.         if font:
  69.             font = root.tk.splitlist(root.tk.call('font', 'actual', font))
  70.         else:
  71.             font = self._set(options)
  72.         if not name:
  73.             name = 'font' + str(id(self))
  74.         
  75.         self.name = name
  76.         if exists:
  77.             self.delete_font = False
  78.             if self.name not in root.tk.call('font', 'names'):
  79.                 raise Tkinter._tkinter.TclError, 'named font %s does not already exist' % (self.name,)
  80.             
  81.             if font:
  82.                 root.tk.call('font', 'configure', self.name, *font)
  83.             
  84.         else:
  85.             root.tk.call('font', 'create', self.name, *font)
  86.             self.delete_font = True
  87.         self._root = root
  88.         self._split = root.tk.splitlist
  89.         self._call = root.tk.call
  90.  
  91.     
  92.     def __str__(self):
  93.         return self.name
  94.  
  95.     
  96.     def __eq__(self, other):
  97.         if self.name == other.name:
  98.             pass
  99.         return isinstance(other, Font)
  100.  
  101.     
  102.     def __getitem__(self, key):
  103.         return self.cget(key)
  104.  
  105.     
  106.     def __setitem__(self, key, value):
  107.         self.configure(**{
  108.             key: value })
  109.  
  110.     
  111.     def __del__(self):
  112.         
  113.         try:
  114.             if self.delete_font:
  115.                 self._call('font', 'delete', self.name)
  116.         except (AttributeError, Tkinter.TclError):
  117.             pass
  118.  
  119.  
  120.     
  121.     def copy(self):
  122.         '''Return a distinct copy of the current font'''
  123.         return Font(self._root, **self.actual())
  124.  
  125.     
  126.     def actual(self, option = None):
  127.         '''Return actual font attributes'''
  128.         if option:
  129.             return self._call('font', 'actual', self.name, '-' + option)
  130.         else:
  131.             return self._mkdict(self._split(self._call('font', 'actual', self.name)))
  132.  
  133.     
  134.     def cget(self, option):
  135.         '''Get font attribute'''
  136.         return self._call('font', 'config', self.name, '-' + option)
  137.  
  138.     
  139.     def config(self, **options):
  140.         '''Modify font attributes'''
  141.         if options:
  142.             self._call('font', 'config', self.name, *self._set(options))
  143.         else:
  144.             return self._mkdict(self._split(self._call('font', 'config', self.name)))
  145.  
  146.     configure = config
  147.     
  148.     def measure(self, text):
  149.         '''Return text width'''
  150.         return int(self._call('font', 'measure', self.name, text))
  151.  
  152.     
  153.     def metrics(self, *options):
  154.         '''Return font metrics.
  155.  
  156.         For best performance, create a dummy widget
  157.         using this font before calling this method.'''
  158.         if options:
  159.             return int(self._call('font', 'metrics', self.name, self._get(options)))
  160.         else:
  161.             res = self._split(self._call('font', 'metrics', self.name))
  162.             options = { }
  163.             for i in range(0, len(res), 2):
  164.                 options[res[i][1:]] = int(res[i + 1])
  165.             
  166.             return options
  167.  
  168.  
  169.  
  170. def families(root = None):
  171.     '''Get font families (as a tuple)'''
  172.     if not root:
  173.         root = Tkinter._default_root
  174.     
  175.     return root.tk.splitlist(root.tk.call('font', 'families'))
  176.  
  177.  
  178. def names(root = None):
  179.     '''Get names of defined fonts (as a tuple)'''
  180.     if not root:
  181.         root = Tkinter._default_root
  182.     
  183.     return root.tk.splitlist(root.tk.call('font', 'names'))
  184.  
  185. if __name__ == '__main__':
  186.     root = Tkinter.Tk()
  187.     f = Font(family = 'times', size = 30, weight = NORMAL)
  188.     print f.actual()
  189.     print f.actual('family')
  190.     print f.actual('weight')
  191.     print f.config()
  192.     print f.cget('family')
  193.     print f.cget('weight')
  194.     print names()
  195.     print f.measure('hello'), f.metrics('linespace')
  196.     print f.metrics()
  197.     f = Font(font = ('Courier', 20, 'bold'))
  198.     print f.measure('hello'), f.metrics('linespace')
  199.     w = Tkinter.Label(root, text = 'Hello, world', font = f)
  200.     w.pack()
  201.     w = Tkinter.Button(root, text = 'Quit!', command = root.destroy)
  202.     w.pack()
  203.     fb = Font(font = w['font']).copy()
  204.     fb.config(weight = BOLD)
  205.     w.config(font = fb)
  206.     Tkinter.mainloop()
  207.  
  208.