use of wx.Font on StaticText (label with spaces) causing unexpected behavior

Hi,

I am in the process of migrating my work from wx (2.8, 3.0) classic versions to 4.x (phoenix) on my Ubuntu 14.04/18.04 machines. I see a strange behaviour during the use of StaticText with wx.Font. Attached is my code snippet.

In short, if a statictext name has space and if we use wx.Font on it, the name is written on multiple lines. But if the name doesn’t have space & used with font, the display on the Frame is OK. Also if the name with space is used without wx.Font, the display is ok on the Frame.

My Ubuntu 14.04 configuration

Python 2.7.6 on linux2, wxPython 4.0.6 gtk3 (phoenix) wxWidgets 3.0.5

Isolation:

Same code works ok on Windows

Same code works ok on Ubuntu 14.04 with wxgtk2.8

test.py (1.33 KB)

linux_frame.jpg

windows_frame.jpg

···

##########################

#!/usr/bin/env python

import wx

import wx.lib.scrolledpanel as scroll

import wx.lib.inspection

class IPClass(scroll.ScrolledPanel):

def __init__( self, parent):

	scroll.ScrolledPanel.__init__( self, parent, -1, name = "Test" )

	self.sizer = wx.BoxSizer( wx.VERTICAL )

	#font = wx.Font(20.5, wx.FONTFAMILY_DECORATIVE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, wx.FONTENCODING_DEFAULT)

	name1 = "NAME WITH SPACES"

	name2 = "NAME_WITHOUT_SPACES"

	name3 = "NAME WITH SPACES WITHOUT FONT USAGE"

	txt = wx.StaticText( self, -1, name1)

	txt1 = wx.StaticText( self, -1, name2)

	txt2 = wx.StaticText( self, -1, name3)

	ft = txt.GetFont()

	ft.PointSize += 2.5

	ft = ft.Bold()

	txt.SetFont(ft)

	txt1.SetFont(ft)

	self.sizer.Add( txt, 0, wx.ALIGN_LEFT|wx.ALL, 5 )

	self.sizer.AddSpacer(10)

	self.sizer.Add( txt1, 0, wx.ALIGN_LEFT|wx.ALL, 5 )

	self.sizer.AddSpacer(10)

	self.sizer.Add( txt2, 0, wx.ALIGN_LEFT|wx.ALL, 5 )

	self.sizer.AddSpacer(10)

	self.SetSizer( self.sizer )

	self.SetAutoLayout(1)

	self.SetupScrolling()

#-------------------------------------------------------------------------------------------------------------------------------

if name == “main”:

app = wx.App()

frame = wx.Frame(None, -1, "Test", size=(640, 480))

win = IPClass(frame)

frame.Show(True)

wx.lib.inspection.InspectionTool().Show()

app.MainLoop()

############################################################

Any ideas on where the problem could be?

Varadharajan Kannan wrote:

I am in the process of migrating my work from wx (2.8, 3.0) classic versions to 4.x (phoenix) on my Ubuntu 14.04/18.04 machines. I see a strange behaviour during the use of StaticText with wx.Font. Attached is my code snippet.
In short, if a statictext name has space and if we use wx.Font on it, the name is written on multiple lines. But if the name doesn't have space & used with font, the display on the Frame is OK. Also if the name with space is used without wx.Font, the display is ok on the Frame.

This seems related to another static text query we had earlier this month. I think the bottom line is that the control only adjusts its base size when the text changes, so when you change the font, it tries to fit in the same width and ends up wrapping. For what it's worth, I saw a very similar behavior in a C++ wxWidgets app, and I solved it by getting in the habit of saying

 txt\.Wrap\( \-1 \)

every time I create a static text with a non-default font.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

It may depend on the version of GTK too. This sample works as expected for me.

···

On Wednesday, May 29, 2019 at 6:13:07 PM UTC-7, Tim Roberts wrote:

This seems related to another static text query we had earlier this
month. I think the bottom line is that the control only adjusts its
base size when the text changes, so when you change the font, it tries
to fit in the same width and ends up wrapping.

Robin

Thanks for your reply. But is the wrapping behaviour limited only to text with spaces? I don’t see this happening with txt1.
BTW, txt.Wrap doesn’t seem to help me.

···

Thanks,
Varadharajan Kannan

nandhu81@gmail.com wrote:

Thanks for your reply. But is the wrapping behaviour limited only to text with spaces? I don't see this happening with txt1.

Yes.

BTW, txt.Wrap doesn't seem to help me.

Have you tried resetting the label after setting the font? It would be inconvenient, but it's an experiment.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Other things to try after making the font change:

widget.InvalidateBestSize()

That will cause the best size to be recalculated the next time it is requested, although I think this should already be happening when SetFont is called.

self.Layout() or maybe self.SendSizeEvent()

Since your test sample does the changes before the sizer is created and before the frame gets its first size event, but maybe calling Layout will help in your real application if things are done differently there.

widget.SetMinSize(widget.GetBestSize())

Setting the min size is probably redundant since the sizer will use the best size if the min size is not set, but maybe the min size is already being set somehow and it is stuck at the wrong size.

Also, you’re launching the WIT in this test sample so use it to examine the layout related properties of the text widgets and determine what is not correct, and then experiment calling methods on the live widgets to see what might help.

···

On Thursday, May 30, 2019 at 10:04:59 AM UTC-7, Tim Roberts wrote:

nandhu81@gmail.com wrote:

Thanks for your reply. But is the wrapping behaviour limited only to
text with spaces? I don’t see this happening with txt1.

Yes.

BTW, txt.Wrap doesn’t seem to help me.

Have you tried resetting the label after setting the font? It would be
inconvenient, but it’s an experiment.

Robin

By resetting you mean
txt.SetLabel(“”) ?

···

On Thursday, May 30, 2019 at 6:04:59 PM UTC+1, Tim Roberts wrote:

nand...@gmail.com wrote:

Thanks for your reply. But is the wrapping behaviour limited only to
text with spaces? I don’t see this happening with txt1.

Yes.

BTW, txt.Wrap doesn’t seem to help me.

Have you tried resetting the label after setting the font? It would be
inconvenient, but it’s an experiment.


Tim Roberts, ti...@probo.com

Providenza & Boekelheide, Inc.

wx.version() gives this output

4.0.6 gtk3 (phoenix) wxWidgets 3.0.5

Are you talking about gtk3 version? How can I check the version/update to the latest one? Could you let me know what version works for you?

···

On Thursday, May 30, 2019 at 3:15:41 AM UTC+1, Robin Dunn wrote:

On Wednesday, May 29, 2019 at 6:13:07 PM UTC-7, Tim Roberts wrote:

This seems related to another static text query we had earlier this
month. I think the bottom line is that the control only adjusts its
base size when the text changes, so when you change the font, it tries
to fit in the same width and ends up wrapping.

It may depend on the version of GTK too. This sample works as expected for me.

Robin

All the below mentioned options doesn’t seem to be helping either. Need to spend a bit more time on Inspection tool a bit later.

Meanwhile attached is comparison picture of the same code between 2.8 & 4.0.6 wxpython versions on Ubuntu 14.04. Something is certainly not right with my configuration when I use 4.0.6. Even the frame background seems corrupted (All Black)

···

On Thursday, May 30, 2019 at 7:04:51 PM UTC+1, Robin Dunn wrote:

On Thursday, May 30, 2019 at 10:04:59 AM UTC-7, Tim Roberts wrote:

nand...@gmail.com wrote:

Thanks for your reply. But is the wrapping behaviour limited only to
text with spaces? I don’t see this happening with txt1.

Yes.

BTW, txt.Wrap doesn’t seem to help me.

Have you tried resetting the label after setting the font? It would be
inconvenient, but it’s an experiment.

Other things to try after making the font change:

widget.InvalidateBestSize()

That will cause the best size to be recalculated the next time it is requested, although I think this should already be happening when SetFont is called.

self.Layout() or maybe self.SendSizeEvent()

Since your test sample does the changes before the sizer is created and before the frame gets its first size event, but maybe calling Layout will help in your real application if things are done differently there.

widget.SetMinSize(widget.GetBestSize())

Setting the min size is probably redundant since the sizer will use the best size if the min size is not set, but maybe the min size is already being set somehow and it is stuck at the wrong size.

Also, you’re launching the WIT in this test sample so use it to examine the layout related properties of the text widgets and determine what is not correct, and then experiment calling methods on the live widgets to see what might help.

Robin

Ok, I can resolve the issue by using [GenStaticText](https://wxpython.org/Phoenix/docs/html/wx.lib.stattext.GenStaticText.html#wx.lib.stattext.GenStaticText) instead of wx.StaticText

https://wxpython.org/Phoenix/docs/html/wx.lib.stattext.html

···

Thanks,
Varadharajan Kannan