I must not understand how to deal with wxFonts. I would be very grateful if someone can explain to me a few things after looking at and running my test code:
1) Why does the font instance gotten by "font = button.GetFont()" return a different instance than the font instance gotten by "print button.GetFont()"?
2) Why does the font instance gotten after "button.SetFont(font)" return yet a third instance of wxFont?
3) How do I successfully set a font property and make it stick, such as the SetPointSize() in my example?
#3 is the real problem. Perhaps #1 and #2 are just because a deep copy is being made or something, and I shouldn't be concerned about it. But the question remains: what is happening and how do I stop it?
FWIW it seems I can change the font properties after the button and frame are being shown, for example from an event handler. I just can't seem to change them until it is shown or something. Is the frame's font overriding the button's font? Help?
I must not understand how to deal with wxFonts. I would be very grateful if someone can explain to me a few things after looking at and running my test code:
1) Why does the font instance gotten by "font = button.GetFont()" return a different instance than the font instance gotten by "print button.GetFont()"?
2) Why does the font instance gotten after "button.SetFont(font)" return yet a third instance of wxFont?
A number of classes in wxWidgets are really just reference counting wrappers around another class that implements the real thing. This allows quick and cheap copies of the wrapper to be created as needed for passing to or returning from functions or etc. If something is changed via the wrapper then a new instance of the refdata is created.
From the Python perspecitve you are seeing multiple instances of wxFont, but they may be refering to the same wxFontRefData object, (or not, it's done in a way that you usually don't need to care.) If needed you can test for equivallent fonts using ==.
3) How do I successfully set a font property and make it stick, such as the SetPointSize() in my example?
This should work in current versions of wxPython:
f = button.GetFont()
f.SetPointSize(20)
button.SetFont(f)
There was a bug in some past versions when this didn't work, and you would have to create a new wx.Font using the properties of the old font except the ones you need to change.
FWIW it seems I can change the font properties after the button and frame are being shown, for example from an event handler. I just can't seem to change them until it is shown or something. Is the frame's font overriding the button's font? Help?
What platform and version?
···
--
Robin Dunn
Software Craftsman http://wxPython.org Java give you jitters? Relax with wxPython!
This works if called from a EVT_BUTTON handler, but not before apparently. Does my test work for you?
Yes, but only after I changed the order of the SetLabel and the SetFont. Apparently the SetLabel is resetting the font. Please enter a bug report about this.
Confirmed the workaround, and submitted the bug report. Do you think it is likely that any other action will also reset the font? And what font do you think it is using - the font of the parent or some other font?
This works if called from a EVT_BUTTON handler, but not before apparently. Does my test work for you?
Yes, but only after I changed the order of the SetLabel and the SetFont. Apparently the SetLabel is resetting the font. Please enter a bug report about this.
Confirmed the workaround, and submitted the bug report. Do you think it is likely that any other action will also reset the font?
It's possible but I tried several things that might behave similarly without any troubles.
And what font do you think it is using - the font of the parent or some other font?
It's probably whatever is the default for the widget type as defined in your current theme or .gtkrc files.
···
--
Robin Dunn
Software Craftsman http://wxPython.org Java give you jitters? Relax with wxPython!