473,544 Members | 420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

wxPython Status Bar - adjust to length of string

jlm699
314 Contributor
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 8079
bartonc
6,596 Recognized Expert Expert
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 Contributor
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
4505
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 wxPython app. I know I'm on the right track with SetStatusText() but it doesn't always alter the text. What the program is doing is taking a...
1
2057
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 trying to do, specifying the coordinates is easier that fighting with the layout resizers. 1) I have a screen split in 2. Left side is a textcontrol...
12
12643
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? Thanks!
1
2282
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 checkbox should reflect the status of the children. When ALL children are checked, then the status should be checked. When NO children are checked, the...
3
2724
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 logic layer) that inserts a user into a database, and returns the newly created UserID: public function CreateUser(userName as string, userEmail as...
9
5514
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. But the "app.MainLoop()" in wxpython looks like conflicting with the "while 1:" in socket server. After I commented the "app.MainLoop()",...
3
1348
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
1432
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 use the frame.txtobj to change displaying strings. I found it is a bad method to do that.
0
2236
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 first start the 'MarketDataAdapter' and then send the request. Whenever we send the request to the Bloomberg agent, the application gets a series...
0
7368
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7777
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7384
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5916
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3420
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3414
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1844
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
990
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.