0

hi, i want to know how can calculate width value of text
for example: (text:'help me', font:'Tahoma 25')
i want to calculate the full width value of the text

so help me please to find a way to do that or with Tkinter lib or with any lib.

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Py-er
0

You could use the pygame module ...

# display text in a given font
# get the total text width

import pygame as pg
    
# initiate pygame
pg.init()

# color tuple(r,g,b)
yellow = (255,255,0)    

# create a 480x80 window/frame
screen = pg.display.set_mode((480, 80))

# create a surface (canvas)
surface = pg.Surface(screen.get_size())
# fill surface with given color
surface.fill(yellow)
# display text in a given font
font = pg.font.SysFont('Comic Sans MS', 36)
text = font.render("Hello from Monty PyGame", 1, (10, 10, 10))
textpos = text.get_rect()

# in case you want to know
textwidth = tuple(textpos)[2]
print "Width of text =", textwidth

surface.blit(text, textpos)

# blit everything to the screen at ulc x=0,y=0
screen.blit(surface, (0, 0))
# update to show
pg.display.flip()

# create the event loop to get things going
while 1:
    for event in pg.event.get():
        # exit condition (click on screen x)
        if event.type == pg.QUIT:
            raise SystemExit
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.