Advertisement
here2share

# Tk_fontmeasure.py

Aug 5th, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. # Tk_fontmeasure.py
  2.  
  3. import Tkinter as tk
  4. import tkFont
  5. root = tk.Tk()
  6. canvas = tk.Canvas(root, width=300, height=200)
  7. canvas.pack()
  8. (x,y) = (5,5)
  9. text = "Hello Python World"
  10. fonts = []
  11. for (family,size) in [("times",12),("times",24)]:
  12.     font = tkFont.Font(family=family, size=size)
  13.     (w,h) = (font.measure(text),font.metrics("linespace"))
  14.     print "%s %s: (%s,%s)" % (family,size,w,h)
  15.     canvas.create_rectangle(x,y,x+w,y+h)
  16.     canvas.create_text(x,y,text=text,font=font,anchor=tk.NW)
  17.     fonts.append(font) # save object from garbage collecting
  18.     y += h+5
  19. tk.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement