Forum: Poser Python Scripting


Subject: Defining wxPython button font size on a Mac

PhilC opened this issue on Jul 18, 2012 · 9 posts


PhilC posted Wed, 18 July 2012 at 1:53 PM

Here's the thing, I wrote PZ3editor in Python and used py2exe to make it into an executable for the PC. Works great :)

Now I'm asked to try and get it working on a Mac and yes running the raw PY files does work but Macs apparently totally ignore the font size commands resulting in every button label being too big for the button.

Test script:-

test button

fontSize = 8

import wx

class MyApp(wx.Frame):
    def init(self,parent,title):
        wx.Frame.init(self,parent,title=title,size=(300,200))

        self.SetFont(wx.Font(fontSize, wx.SWISS, wx.NORMAL, wx.NORMAL))

        # add panel
        self.p = wx.Panel(self,-1, size=(300,200))
        # add button
        b = wx.Button(self.p, 10, "Press This Button", (20, 20))
        self.Bind(wx.EVT_BUTTON, self.OnClick, b)
        b.SetSize(b.GetBestSize())

    def OnClick(self, event):
        print "Font size = %i" % fontSize

app = wx.App(False)
frame = MyApp(None, "Button Test")
frame.Show(True)
app.MainLoop()

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

I know some folks swear by their Macs, me I just stand by mine and swear!

Anyone have any idea how to define button font sizes on a Mac. To reformat my GUI and the multitude of associated pop up dialogs is frankly just not worth the effort.


Pret-a-3D posted Wed, 18 July 2012 at 2:32 PM

Have you triied using:

style=wx.BU_EXACTFIT

in the wx.Button constructor?

Paolo

https://www.preta3d.com
FB: https://www.facebook.com/RealityPlugIn
Tw: @preta3d
G+: https://plus.google.com/106625816153304163119
The Reality Gallery: https://reality-plug-in.deviantart.com


PhilC posted Wed, 18 July 2012 at 3:25 PM

Thanks but I need to keep the buttons the same size or else I'll have to reformat/resize everything.


bagginsbill posted Wed, 18 July 2012 at 4:35 PM

If you set the font on the button itself, instead of the parent frame, it respects it. However, it does not grow or shrink its height for bigger fonts, like Windows does. It would appear that the User Interface Enforcement Bureau at Apple think buttons must only come in one height.

But you seem to be trying to get them narrower? That works to a degree.

I found it possible to rename the wx.Button and then add my own definition. Then all existing code uses mine.

 


Renderosity forum reply notifications are wonky. If I read a follow-up in a thread, but I don't myself reply, then notifications no longer happen AT ALL on that thread. So if I seem to be ignoring a question, that's why. (Updated September 23, 2019)


bagginsbill posted Wed, 18 July 2012 at 4:36 PM

fontSize = 8 buttonFont = wx.Font(fontSize, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL)

if not hasattr(wx, 'RealButton'):
        wx.RealButton = wx.Button

class MyButton(wx.RealButton):
        def __init__(self, *a, **k):
                RealButton.__init__(self, *a, **k)
                self.Font = buttonFont

wx.Button = MyButton

Renderosity forum reply notifications are wonky. If I read a follow-up in a thread, but I don't myself reply, then notifications no longer happen AT ALL on that thread. So if I seem to be ignoring a question, that's why. (Updated September 23, 2019)


bagginsbill posted Wed, 18 July 2012 at 4:39 PM

Looks like this.

Note: Swiss works at 8 but is hard to read.


Renderosity forum reply notifications are wonky. If I read a follow-up in a thread, but I don't myself reply, then notifications no longer happen AT ALL on that thread. So if I seem to be ignoring a question, that's why. (Updated September 23, 2019)


Pret-a-3D posted Wed, 18 July 2012 at 4:46 PM

The whole re-aliasing of wx.Button is brilliant! Being able to do that kind of stuff can open a lot of possiblities. Thanks for the tip!

Paolo

https://www.preta3d.com
FB: https://www.facebook.com/RealityPlugIn
Tw: @preta3d
G+: https://plus.google.com/106625816153304163119
The Reality Gallery: https://reality-plug-in.deviantart.com


bagginsbill posted Wed, 18 July 2012 at 4:52 PM

Paolo,

Glad you like it. You know what is even more interesting? You can inject methods into those existing classes without subclassing them!!!

It's a little harder than subclassing, but very convenient for cases where you can't get in there fast enough to swap your class before some other bit of code is already remembering pointers to the original.

For example, as soon as you import wx, you're getting all of it. There may be library subclasses of wx.Button already set up. They will not derive from MyButton.

So in such cases, replacing a method of wx.Button (the original class) works.


Renderosity forum reply notifications are wonky. If I read a follow-up in a thread, but I don't myself reply, then notifications no longer happen AT ALL on that thread. So if I seem to be ignoring a question, that's why. (Updated September 23, 2019)


Pret-a-3D posted Wed, 18 July 2012 at 5:29 PM

Yeah, that part I was aware of it. It's similar to what you can do with JavaScript (shudder), I extended the String class in JS to provide missing services. But, the fact that you could alias/redirect a class is defintely handy. A good way to shoot yourself in the foot or play practical jokes too! ;-)

Paolo

https://www.preta3d.com
FB: https://www.facebook.com/RealityPlugIn
Tw: @preta3d
G+: https://plus.google.com/106625816153304163119
The Reality Gallery: https://reality-plug-in.deviantart.com