How To Get The Width Of A String In Pixels?
I am using wxPython's HyperTreeList and I want to set the column width exactly equal to length of the largest string in it. To accomplish that, I'd like to to convert a python stri
Solution 1:
You'll have to do somethinglike (see the documentation of wxWidgets for more info)
f = window.GetFont()
dc = wx.WindowDC(window)
dc.SetFont(f)
width, height = dc.GetTextExtent("Text to measure")
Solution 2:
It depends on how you are printing the text.
You may be interested by PIL ImageDraw
which has a textsize
method. See http://effbot.org/imagingbook/imagedraw.htm
Update: This was answering the original question. It may looks a little off-topic after question updates.
Solution 3:
This is not my solution, I'm just passing it on as I found it works and most useful as in a program environment it reduces to only 3 lines. Python3.9 mac OS
from tkinter import * from tkinter import font as W1
FW=Tk() ; FW.withdraw() # a must but don't need a window Text ='1234567890'
def LENGTH(Text) : W2 = W1.Font(family='Comicsans' , size = 20) length = W2.measure(Text) print(length)
LENGTH(Text)
Post a Comment for "How To Get The Width Of A String In Pixels?"