Represents a named font. Constructor options are: font -- font specifier (name, system font, or (family, size, style)-tuple) name -- name to use for this font configuration (defaults to a unique name) exists -- does a named font by this name already exist? Creates a new name
| 23 | |
| 24 | |
| 25 | class Font: |
| 26 | """Represents a named font. |
| 27 | |
| 28 | Constructor options are: |
| 29 | |
| 30 | font -- font specifier (name, system font, or (family, size, style)-tuple) |
| 31 | name -- name to use for this font configuration (defaults to a unique name) |
| 32 | exists -- does a named font by this name already exist? |
| 33 | Creates a new named font if False, points to the existing font if True. |
| 34 | Raises _tkinter.TclError if the assertion is false. |
| 35 | |
| 36 | the following are ignored if font is specified: |
| 37 | |
| 38 | family -- font 'family', e.g. Courier, Times, Helvetica |
| 39 | size -- font size in points |
| 40 | weight -- font thickness: NORMAL, BOLD |
| 41 | slant -- font slant: ROMAN, ITALIC |
| 42 | underline -- font underlining: false (0), true (1) |
| 43 | overstrike -- font strikeout: false (0), true (1) |
| 44 | |
| 45 | """ |
| 46 | |
| 47 | counter = itertools.count(1) |
| 48 | |
| 49 | def _set(self, kw): |
| 50 | options = [] |
| 51 | for k, v in kw.items(): |
| 52 | options.append("-"+k) |
| 53 | options.append(str(v)) |
| 54 | return tuple(options) |
| 55 | |
| 56 | def _get(self, args): |
| 57 | options = [] |
| 58 | for k in args: |
| 59 | options.append("-"+k) |
| 60 | return tuple(options) |
| 61 | |
| 62 | def _mkdict(self, args): |
| 63 | options = {} |
| 64 | for i in range(0, len(args), 2): |
| 65 | options[args[i][1:]] = args[i+1] |
| 66 | return options |
| 67 | |
| 68 | def __init__(self, root=None, font=None, name=None, exists=False, |
| 69 | **options): |
| 70 | if root is None: |
| 71 | root = tkinter._get_default_root('use font') |
| 72 | tk = getattr(root, 'tk', root) |
| 73 | if font: |
| 74 | # get actual settings corresponding to the given font |
| 75 | font = tk.splitlist(tk.call("font", "actual", font)) |
| 76 | else: |
| 77 | font = self._set(options) |
| 78 | if not name: |
| 79 | name = "font" + str(next(self.counter)) |
| 80 | self.name = name |
| 81 | |
| 82 | if exists: |
searching dependent graphs…