469,645 Members | 1,461 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

wxPython Status Bar - adjust to length of string

jlm699
314 100+
This is a simple one guys... I just want the last field of a status bar to adjust based on the length of the text contained within. I was using the example from the wxPython status bar demo where there is a timer in the last field, however on some systems with higher resolution the timer's minutes and seconds are cut-off. I tried adjusting the statusbarwidths to the length of the string, but this obviously won't work because that length is not in pixels. Here's my update function:
Expand|Select|Wrap|Line Numbers
  1.     # This is the timer's update function
  2.     def Notify(self):
  3.         t = time.localtime(time.time())
  4.         st = time.strftime(" %b %d, %Y  %I:%M:%S", t)
  5.         self.SetStatusWidths([-4, -2, 17, len(st)])
  6.         self.SetStatusText(st, 3)
  7.  
And then this next part can be put into any wx.Frame
Expand|Select|Wrap|Line Numbers
  1.         self.sb = self.CreateStatusBar(4)
  2.         self.sb.SetStatusWidths([-4, -2, 17, 150])
  3.  
So my question is basically is there a way to convert from len(str) into the pixels required to display len(str) ?

Thanks in advance
Aug 16 '07 #1
2 7388
bartonc
6,596 Expert 4TB
So my question is basically is there a way to convert from len(str) into the pixels required to display len(str) ?
I've seen this once, but can't find it at the moment. It goes something like the:
Expand|Select|Wrap|Line Numbers
  1. f = window.GetFont()
  2. dc = wx.WindowDC(window)
  3. dc.SetFont(f)
  4. width, height = dc.GetTextExtent("Text to measure")
By "window", I mean the widget that will get the text.
Aug 16 '07 #2
jlm699
314 100+
I've seen this once, but can't find it at the moment. It goes something like the:
Expand|Select|Wrap|Line Numbers
  1. f = window.GetFont()
  2. dc = wx.WindowDC(window)
  3. dc.SetFont(f)
  4. width, height = dc.GetTextExtent("Text to measure")
By "window", I mean the widget that will get the text.
Once again BartonC saves the day! Thanks a bunch, that code worked flawlessly with only one minor thing.

Since this is in a status bar and the text is at the end of the status bar, it gets cut off by the window resize 'grabber area' (if that makes any sense). See I have a date/time display in the last field of my status bar... so as I update the text I adjust the size of that last field with the following code:

Expand|Select|Wrap|Line Numbers
  1.     # This is the timer's update function
  2.     def Notify(self):
  3.         t = time.localtime(time.time())
  4.         st = time.strftime(" %b %d, %Y  %I:%M:%S", t)
  5.         self.SetStatusText(st, 3)
  6.         f = self.sb.GetFont()
  7.         dc = wx.WindowDC(self.sb)
  8.         dc.SetFont(f)
  9.         width, height = dc.GetTextExtent(st)
  10.         self.sb.SetStatusWidths([-4, -2, 17, width + 30])
  11.  
I know it's not a very good idea to just use an assumed size of 30 for that little box in the corner, but it works on the systems that used to get cut off and looks the same on the systems that never had the problem in the first place so; success!!

Thanks again to BartonC, the Forum Rock Star.
Jan 8 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Steve Lamb | last post: by
1 post views Thread by Petr Prikryl | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.