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.

Recommended Answers

All 2 Replies

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

YES! it works perfectly, thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.