22#
33# written by Fredrik Lundh, February 1998
44#
5- # FIXME: should add 'displayof' option where relevant (actual, families,
6- # measure, and metrics)
7- #
85
96__version__ = "0.9"
107
@@ -124,14 +121,17 @@ def copy(self):
124121 "Return a distinct copy of the current font"
125122 return Font (self ._root , ** self .actual ())
126123
127- def actual (self , option = None ):
124+ def actual (self , option = None , displayof = None ):
128125 "Return actual font attributes"
126+ args = ()
127+ if displayof :
128+ args = ('-displayof' , displayof )
129129 if option :
130- return self ._call ("font" , "actual" , self .name , "-" + option )
130+ args = args + ('-' + option , )
131+ return self ._call ("font" , "actual" , self .name , * args )
131132 else :
132133 return self ._mkdict (
133- self ._split (self ._call ("font" , "actual" , self .name ))
134- )
134+ self ._split (self ._call ("font" , "actual" , self .name , * args )))
135135
136136 def cget (self , option ):
137137 "Get font attribute"
@@ -148,32 +148,42 @@ def config(self, **options):
148148
149149 configure = config
150150
151- def measure (self , text ):
151+ def measure (self , text , displayof = None ):
152152 "Return text width"
153- return int (self ._call ("font" , "measure" , self .name , text ))
153+ args = (text ,)
154+ if displayof :
155+ args = ('-displayof' , displayof , text )
156+ return int (self ._call ("font" , "measure" , self .name , * args ))
154157
155- def metrics (self , * options ):
158+ def metrics (self , * options , ** kw ):
156159 """Return font metrics.
157160
158161 For best performance, create a dummy widget
159162 using this font before calling this method."""
160-
163+ args = ()
164+ displayof = kw .pop ('displayof' , None )
165+ if displayof :
166+ args = ('-displayof' , displayof )
161167 if options :
168+ args = args + self ._get (options )
162169 return int (
163- self ._call ("font" , "metrics" , self .name , self . _get ( options ) ))
170+ self ._call ("font" , "metrics" , self .name , * args ))
164171 else :
165- res = self ._split (self ._call ("font" , "metrics" , self .name ))
172+ res = self ._split (self ._call ("font" , "metrics" , self .name , * args ))
166173 options = {}
167174 for i in range (0 , len (res ), 2 ):
168175 options [res [i ][1 :]] = int (res [i + 1 ])
169176 return options
170177
171178
172- def families (root = None ):
179+ def families (root = None , displayof = None ):
173180 "Get font families (as a tuple)"
174181 if not root :
175182 root = tkinter ._default_root
176- return root .tk .splitlist (root .tk .call ("font" , "families" ))
183+ args = ()
184+ if displayof :
185+ args = ('-displayof' , displayof )
186+ return root .tk .splitlist (root .tk .call ("font" , "families" , * args ))
177187
178188
179189def names (root = None ):
@@ -205,10 +215,10 @@ def names(root=None):
205215
206216 print (f .measure ("hello" ), f .metrics ("linespace" ))
207217
208- print (f .metrics ())
218+ print (f .metrics (displayof = root ))
209219
210220 f = Font (font = ("Courier" , 20 , "bold" ))
211- print (f .measure ("hello" ), f .metrics ("linespace" ))
221+ print (f .measure ("hello" ), f .metrics ("linespace" , displayof = root ))
212222
213223 w = tkinter .Label (root , text = "Hello, world" , font = f )
214224 w .pack ()
0 commit comments