Python Forum
How can I get the width of a string in Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I get the width of a string in Python?
#6
You should have experience with GUI programming or 2D-programming.
If not, it is difficult.

#!/usr/bin/python3
import wx  # wxpython 4.x
app = wx.App()

window = wx.Frame(None, title = "wxPython Frame", size = (300,200))
panel  = wx.Panel(window)

txt   = "Hello World"
label = wx.StaticText(panel, label = txt, pos = (100,50))

dc   = wx.ScreenDC()
dc.SetFont(label.GetFont())
size = dc.GetTextExtent(txt)
txtwh = "width=%d  height=%d" % (size.width, size.height)
label = wx.StaticText(panel, label = txtwh, pos = (100,100))

window.Show(True)
app.MainLoop()
Reply


Messages In This Thread
RE: How can I get the width of a string in Python? - by heiner55 - May-26-2019, 04:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code to set column width 1418 11 8,300 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  Fixed colum width for rowLabels i Matplotlib pandabay 0 1,166 Jun-10-2023, 03:40 PM
Last Post: pandabay
  width of Unicode character Skaperen 6 4,314 Sep-27-2021, 12:41 AM
Last Post: Skaperen
  image.thumbnail(width, height) not working PCesarano 2 5,054 Apr-08-2021, 06:09 PM
Last Post: PCesarano
  fixed width numbers Skaperen 15 12,796 May-27-2019, 09:42 AM
Last Post: Skaperen
  printing text tables with consistent width Skaperen 7 13,257 Jul-01-2018, 02:34 AM
Last Post: Skaperen
  How to measure an inclined beam width and height in image using python? zyb1003 1 4,036 Nov-07-2017, 05:02 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020