I can't make wx.StaticText update correctly with a new label

I've been gone for a while, but I'm back.

I want to put a block of uneditable text into a window with
scrollbars. I update the text with a SetLabel() on the StaticText
widget. The presense of a newline causes it to act badly, while no
newlines cause it to act as I would expect (but at the price of only
having a single line of text). I've tried lots of things, like Fit(),
SetSize(), more calls to SetupScrolling(), Wrap( -1 ), etc, but
nothing seems to work.

What I see when I run the program below is that the window has a
column of text as such:
A
longe
r line
of
text
doesn
't
work
out
right
if it
has a
newli
ne in
it!

And if I scroll down so that the first line (A) isn't in the window
then the blue color disappears completely.

What am I missing please?

import wx
import wx.lib.scrolledpanel as scrolled

class TestPanel(scrolled.ScrolledPanel):
    def __init__(self, parent):
        scrolled.ScrolledPanel.__init__(self, parent, -1)

        vbox = wx.BoxSizer(wx.VERTICAL)
        self.desc = wx.StaticText(self, -1, "initial" )
        self.desc.SetForegroundColour("Blue")
        vbox.Add(self.desc, 0, wx.ALIGN_LEFT|wx.ALL, 5)
        self.SetSizer(vbox)
        self.SetAutoLayout(1)
        self.SetupScrolling()

app = wx.App(0)
frame = wx.Frame(None, wx.ID_ANY)
fa = TestPanel(frame)
frame.Show()

fa.desc.SetLabel( "A longer line of text doesn't work out right if it
has a newline in it!\n" )

app.MainLoop()

···

--
-- Schlake

Your program works fine for me. I get a column of blue text as you
described, it doesn't change when I scroll, and if I change the code
to have more newlines they appear as appropriate. Even if I remove the
one newline in the example I still get that column instead of a single
long line.

-Chris

···

On Fri, Sep 9, 2011 at 1:10 PM, William D. Colburn (Schlake) <schlake@gmail.com> wrote:

I've been gone for a while, but I'm back.

I want to put a block of uneditable text into a window with
scrollbars. I update the text with a SetLabel() on the StaticText
widget. The presense of a newline causes it to act badly, while no
newlines cause it to act as I would expect (but at the price of only
having a single line of text). I've tried lots of things, like Fit(),
SetSize(), more calls to SetupScrolling(), Wrap( -1 ), etc, but
nothing seems to work.

What I see when I run the program below is that the window has a
column of text as such:
A
longe
r line
of
text
doesn
't
work
out
right
if it
has a
newli
ne in
it!

And if I scroll down so that the first line (A) isn't in the window
then the blue color disappears completely.

What am I missing please?

import wx
import wx.lib.scrolledpanel as scrolled

class TestPanel(scrolled.ScrolledPanel):
def __init__(self, parent):
scrolled.ScrolledPanel.__init__(self, parent, -1)

   vbox = wx\.BoxSizer\(wx\.VERTICAL\)
   self\.desc = wx\.StaticText\(self, \-1, &quot;initial&quot; \)
   self\.desc\.SetForegroundColour\(&quot;Blue&quot;\)
   vbox\.Add\(self\.desc, 0, wx\.ALIGN\_LEFT|wx\.ALL, 5\)
   self\.SetSizer\(vbox\)
   self\.SetAutoLayout\(1\)
   self\.SetupScrolling\(\)

app = wx.App(0)
frame = wx.Frame(None, wx.ID_ANY)
fa = TestPanel(frame)
frame.Show()

fa.desc.SetLabel( "A longer line of text doesn't work out right if it
has a newline in it!\n" )

app.MainLoop()

--
-- Schlake

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

When I checked out the Docs I couldn't find a Multi-Line attribute for
StaticText...

A Multi-Line attribute does exist for the RichTextCtrl and the
TextCtrl

Maybe Robin can shed some light as to whether the StaticText control
should or should not have multiple lines and if so how to get hem to
work properly?

But the column of text is wrong. I want lines of text, not a column.

···

On Fri, Sep 9, 2011 at 3:14 PM, Michael A. Druckenmiller Sr. <madrucke@yahoo.com> wrote:

When I checked out the Docs I couldn't find a Multi-Line attribute for
StaticText...

A Multi-Line attribute does exist for the RichTextCtrl and the
TextCtrl

Maybe Robin can shed some light as to whether the StaticText control
should or should not have multiple lines and if so how to get hem to
work properly?

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

--
-- Schlake

When I run it as you wrote it*, I don’t get a column - I get a single line of text. (There’s a newline at the end of it, but it’s invisible.) If I add more newlines in the middle of the text, the line breaks appropriately.

My environment:
Win7 x64
Python version 2.7.1
wxPython version 2.8.12.0
Possibly there’s something funky in your environment?

  • “as I assume you wrote it”, that is. As I received it, there’s a line break between “it” and “has”, which causes an error. Ah, the joys of pasting code into an email!
···

On Fri, Sep 9, 2011 at 4:24 PM, William D. Colburn (Schlake) schlake@gmail.com wrote:

But the column of text is wrong. I want lines of text, not a column.

What is the platform and wx version? Whether a static text automatically wraps or not is undefined and platform dependent. On the other hand, what is supposed to work everywhere is that it will honor newlines in the text. Wrap() basically just inserts newlines in the label's text.

If your intent is that the widget remains the same width as it is when displaying the "initial" text, then you can do it like the attached modification to your sample. If you want some other result you'll need to explain it some more.

As for the color change, not sure what's going on there. More details are needed.

st.py (806 Bytes)

···

On 9/9/11 1:10 PM, William D. Colburn (Schlake) wrote:

I've been gone for a while, but I'm back.

I want to put a block of uneditable text into a window with
scrollbars. I update the text with a SetLabel() on the StaticText
widget. The presense of a newline causes it to act badly, while no
newlines cause it to act as I would expect (but at the price of only
having a single line of text). I've tried lots of things, like Fit(),
SetSize(), more calls to SetupScrolling(), Wrap( -1 ), etc, but
nothing seems to work.

What I see when I run the program below is that the window has a
column of text as such:
A
longe
r line
of
text
doesn
't
work
out
right
if it
has a
newli
ne in
it!

And if I scroll down so that the first line (A) isn't in the window
then the blue color disappears completely.

What am I missing please?

--
Robin Dunn
Software Craftsman

Python 2.7.1 on MacOS Lion (10.6 I think is the number) with wx
2.8.12.1 (mac-unicode).

My intent is that when I update the text the window gets scrollbars as
needed to display it "as is" and obeying the newlines. For the
example I provided I'd expect there to be one long line of text. It
appears that the StaticText widget is sizing itself when initially
created based on it's label, and never resizing. I've tried lots of
things to make it resize, but none seems to work. Most, in fact,
cause it to size itself to (0,0).

···

On Fri, Sep 9, 2011 at 6:29 PM, Robin Dunn <robin@alldunn.com> wrote:

On 9/9/11 1:10 PM, William D. Colburn (Schlake) wrote:

I've been gone for a while, but I'm back.

I want to put a block of uneditable text into a window with
scrollbars. I update the text with a SetLabel() on the StaticText
widget. The presense of a newline causes it to act badly, while no
newlines cause it to act as I would expect (but at the price of only
having a single line of text). I've tried lots of things, like Fit(),
SetSize(), more calls to SetupScrolling(), Wrap( -1 ), etc, but
nothing seems to work.

What I see when I run the program below is that the window has a
column of text as such:
A
longe
r line
of
text
doesn
't
work
out
right
if it
has a
newli
ne in
it!

And if I scroll down so that the first line (A) isn't in the window
then the blue color disappears completely.

What am I missing please?

What is the platform and wx version? Whether a static text automatically
wraps or not is undefined and platform dependent. On the other hand, what
is supposed to work everywhere is that it will honor newlines in the text.
Wrap() basically just inserts newlines in the label's text.

If your intent is that the widget remains the same width as it is when
displaying the "initial" text, then you can do it like the attached
modification to your sample. If you want some other result you'll need to
explain it some more.

As for the color change, not sure what's going on there. More details are
needed.

--
Robin Dunn
Software Craftsman
http://wxPython.org

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

--
-- Schlake

All it should need is to call the scrolled panel's SetupScrolling again. That will recalculate the virtual size of the scrolled window based on the size that the sizer reports that it needs. However it looks like the static text has a bug in 2.8 and/or carbon where it is not recalculating its best size correctly when there are newlines in the text. The attached works fine for me on a 2.9-cocoa build.

st.py (1.26 KB)

···

On 9/9/11 5:59 PM, William D. Colburn (Schlake) wrote:

Python 2.7.1 on MacOS Lion (10.6 I think is the number) with wx
2.8.12.1 (mac-unicode).

My intent is that when I update the text the window gets scrollbars as
needed to display it "as is" and obeying the newlines. For the
example I provided I'd expect there to be one long line of text. It
appears that the StaticText widget is sizing itself when initially
created based on it's label, and never resizing. I've tried lots of
things to make it resize, but none seems to work. Most, in fact,
cause it to size itself to (0,0).

--
Robin Dunn
Software Craftsman

I installed 2.9-cocoa and my program suddenly runs like I expected it
too. Thanks.

···

On Fri, Sep 9, 2011 at 8:21 PM, Robin Dunn <robin@alldunn.com> wrote:

size correctly when there are newlines in the text. The attached works fine
for me on a 2.9-cocoa build.

--
-- Schlake

OP, I do not see subclassing that way often

class MyClass(Class.Subclass):
    def __init__(self, ...):
        Class.Subclass.__init__(...)

I'd worry about MRO issues doing it that way.
Why? Class.Subclass.__init__(..) just might be different then
super(MyClass, self).__init__() in this case.
Personally I don't know enough about putting in a base like that (in a
class definition header like you have).

It was not Class.Subclass, it was module.Class.

···

On 10/16/11 1:55 PM, Dev Player wrote:

OP, I do not see subclassing that way often

class MyClass(Class.Subclass):
     def __init__(self, ...):
         Class.Subclass.__init__(...)

I'd worry about MRO issues doing it that way.
Why? Class.Subclass.__init__(..) just might be different then
super(MyClass, self).__init__() in this case.
Personally I don't know enough about putting in a base like that (in a
class definition header like you have).

--
Robin Dunn
Software Craftsman