Page 1 of 1

resizing the width of a SpinCtrl the elegant way

Posted: Sat Apr 25, 2015 2:21 pm
by MoonKid
I want to have SpinCtrl that just use nothing more then the width needed for (e.g.) 2 digits.

I know I can set SetMinSize() and use GetTextExtend("00") to compute the needed size of a string.

But the SpinButtons have a width, too. As a workaround I could create one time a spinbutton store its width and use this value to create the SpinCtrls the way I need them.

But the TextCtrl has a space (margin?) between its text and the border of itself.

All this values differs on different plattforms. So I need a plattform independend solution for this.

How could this be solved?

It would be easier if I could access the TextCtrl inside of the SpinCtrl directly. So it would be more logical to set the MinSize of the TextCtrl. But in this case there still would be the problem with the margins of the TextCtrl. And in my opinion it would be a dirty way if I just would add fixed pixel values (e.g. 10 px) as margins.

And creating my own SpinCtrl using TextCtrl and SpinButtons wouldn't solve all problems.

I used wx with C++ some years ago. Now I am using it with Python (wxPhoenix with Python3). But the problem is still the same when I interprete the docu files correct.

Re: resizing the width of a SpinCtrl the elegant way

Posted: Sun Apr 26, 2015 1:03 pm
by catalin
AFAIR a spinCtrl will have its initial width determined by its initial value. You might also need to change its default 'max' value.
Another possible way is to use wxControl::GetSizeFromTextSize(), see its docs and given example.

Re: resizing the width of a SpinCtrl the elegant way

Posted: Sun Apr 26, 2015 3:52 pm
by MoonKid
Ah nice. This works nearly perfect.

Currently I have not C++ environment to test it with the original wxWidgets.

But with wxPyhton (on KDE 4 with Kubuntu 14.04.2) the computed with needs only 1 pixel more. ;)

Code: Select all

size = spinCtrl.GetSizeFromTextSize(spinCtrl.GetTextExtent('00'))
The last (right) pixels of the second 0 in the SpinControl are cutted by the SpinButtons. When I add manualy one pixel more to the width it looks pretty.

Sorry, for asking. But could you be so nice and check that behaviour with C++ wxWidgets and post your result (version, gui-backend, os, ...)?
It would be helpfull to find out if it is wxPython-related or wxWidgets-related.

I tested that on LXDE (Lubuntu 14.04.2), which means Gtk2 I think. There it works perfect without adding manualy a pixel more to the width. I used wxPhoenix (dev version of wxPython) with Python3.