473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 8053
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

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

Similar topics

3
by: Steve Lamb | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Ok, this one I did try before coming here... :) I am trying to report the status of what is going on to the status bar of the main window of my...
1
by: flupke | last post by:
Hi, i'm trying to convert my java console app to a python gui. Now, the only problem i seem to have at the moment are the resizers for the layout. It seems that for the purpose of what i'm...
12
by: Tom | last post by:
1) Portable to Windows, Unix-like platforms, and the Macintosh; 2) Powerful, GUI is very beautiful ; 3) Efficiency of development is high; What's the best, Tkinter, wxPython, QT, GTK or other? ...
1
by: Petr Prikryl | last post by:
Hi, I am experimenting with wxPython on Windows box. What I need to implement is a check tree control with 3 states for each checkbox: unchecked, checked, gray checked. The status of the...
3
by: Sean Tynan | last post by:
I want to find out the best way for a method to notify calling code of situations such as validation errors, etc. that may occur during method execution. e.g. say I have a method (business...
9
by: zxo102 | last post by:
Hi everyone, I am using a python socket server to collect data from a socket client and then control a image location ( wxpython) with the data, i.e. moving the image around in the wxpython frame....
3
by: WuJianWei | last post by:
If you type a string: "It's good." there's one space between the two words. but how can i adjust the string to two spaces or more. "it's good."
11
by: Jia Lu | last post by:
HI all I am making an application with wxpython. But I got a problem when I want to change the display string according to process status. I passed the frame to the processing function and...
0
by: Yasir Zaheer | last post by:
Hi, We are using Bloomberg API for .Net to connect to Bloomberg and get the field information for specific securities. The application is running on a Bloomberg terminal. While requesting, we...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.