Measure The Height Of A String In Tkinter Python?
I need the height in pixel of a string in a Tkiner widget. It is the text in a row of a Listbox. I know I can measure the width of a string with tkinter.font.Font.measure. But how
Solution 1:
tkf.Font(font=widget['font']).metrics('linespace')
gives the height in pixels of a given widget's font:
try: # In order to be able to import tkinter forimport tkinter as tk # either in python 2 or in python 3import tkinter.font as tkf
except ImportError:
import Tkinter as tk
import tkFont as tkf
if __name__ == '__main__':
root = tk.Tk()
widget = tk.Label(root, text="My String")
widget.pack()
print(tkf.Font(font=widget['font']).metrics('linespace'))
tk.mainloop()
Also note that the height of a one line string isn't dependent on its length, but only on its font in a particular environment.
Post a Comment for "Measure The Height Of A String In Tkinter Python?"