Phoenix icon indicating copy to clipboard operation
Phoenix copied to clipboard

Wrong display for 5 digit float in SpinCtrlDouble.

Open MidnightBiscuit opened this issue 7 months ago • 1 comments

Operating system: WIN 10 wxPython version & source: wxPython-4.2.3, pip install -U wxPython Python version & source: python 3.11.5, anaconda env

Description of the problem:

I need to implement wx.SpinCtrlDouble in my GUI. As soon as I require more than 3 digits, they appear at launch as in the screenshot. The float inside is selected, and shifted to the left so you cannot see the whole number, as if the cursor was moved to the right. So rather than seeing "21.62100", you just see ".62100". I tried to span wx.SpinCtrlDouble more or align the text to the right but it changed nothing.

Image

Code Example (click to expand)
class Mywin(wx.Frame): 
    def __init__(self): 
        wx.Frame.__init__(self, None, title = 'Hello')
        # main panel window
        panel = wx.Panel(self)        
        # main vbox
        vbox = wx.BoxSizer(wx.VERTICAL)
        # vbox.SetMinSize(self,(600,800))

        # StaticBoxSizer for Keithley controls
        BoxKeithleySizer = wx.StaticBoxSizer(wx.VERTICAL, panel, 'Keithley :') # StaticBox with edge and title
        BoxKeithleyGrid = wx.GridBagSizer(hgap=5, vgap=5) # add widgets to this
        toto = BoxKeithleySizer.GetStaticBox()
        toto.SetBackgroundColour((255, 201, 201)) # set background color of the StaticBox

        # widgets to go in the StaticBoxSizer for Keithley controls
        TextKeithleyFreq = wx.StaticText(BoxKeithleySizer.GetStaticBox(), -1, "Freq.")
        BoxKeithleyFreq = wx.SpinCtrlDouble(BoxKeithleySizer.GetStaticBox(), -1)
        UnitKeithleyFreq = wx.StaticText(BoxKeithleySizer.GetStaticBox(), -1, "MHz")

        BoxKeithleyFreq.SetDigits(5)
        BoxKeithleyFreq.SetIncrement(0.001)
        BoxKeithleyFreq.SetMin(21.600)
        BoxKeithleyFreq.SetMax(21.650)
        BoxKeithleyFreq.SetValue(21.621)

        # add widgets to the StaticBoxSizer for Keithley controls
        BoxKeithleyGrid.Add(TextKeithleyFreq, pos=(0,0), flag = wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        BoxKeithleyGrid.Add(BoxKeithleyFreq, pos=(0,1), span=(1,5), flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL)
        BoxKeithleyGrid.Add(UnitKeithleyFreq, pos=(0,6), flag = wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)

        BoxKeithleySizer.Add(BoxKeithleyGrid)
        # add Sizers to main vbox and panel
        vbox.Add(BoxKeithleySizer,0, wx.ALL|wx.LEFT, 5)
        panel.SetSizer(vbox)

class MyApp(wx.App):
    def OnInit(self):

        frame = Mywin()
        frame.Show()
        self.SetTopWindow(frame)

        return True

def main():
    app = MyApp(redirect=False)
    app.MainLoop()

if __name__ == "__main__" :
    main()

MidnightBiscuit avatar Jun 05 '25 14:06 MidnightBiscuit

Operating system: Windows 10/11 wxPython version: 4.2.3 Python version: 3.11.9

Solution:

Hello/Bonjour Adrien, I simply added a fixed size for wx.SpinCtrlDouble(80,-1) to ensure enough width to correctly display the inserted value.

Image

import wx

class Mywin(wx.Frame): 
    def __init__(self): 
        wx.Frame.__init__(self, None, title = 'Hello')
        # main panel window  
        panel = wx.Panel(self)        
        # main vbox  
        vbox = wx.BoxSizer(wx.VERTICAL)

        # StaticBoxSizer for Keithley controls  
        BoxKeithleySizer = wx.StaticBoxSizer(wx.VERTICAL, panel, 'Keithley :') # StaticBox with edge and title  
        BoxKeithleyGrid = wx.GridBagSizer(hgap=5, vgap=5) # add widgets to this  
        toto = BoxKeithleySizer.GetStaticBox()
        toto.SetBackgroundColour((255, 201, 201)) # set background color of the StaticBox

        # widgets to go in the StaticBoxSizer for Keithley controls  
        TextKeithleyFreq = wx.StaticText(BoxKeithleySizer.GetStaticBox(), -1, "Freq.")
        BoxKeithleyFreq = wx.SpinCtrlDouble(BoxKeithleySizer.GetStaticBox(), -1, value="", pos=wx.DefaultPosition,
               size=(80, -1), style=wx.SP_ARROW_KEYS|wx.TE_LEFT, min=0, max=100, initial=0, inc=1,
               name=("wxSpinCtrlDouble"))
        UnitKeithleyFreq = wx.StaticText(BoxKeithleySizer.GetStaticBox(), -1, "MHz")

        BoxKeithleyFreq.SetDigits(5)
        BoxKeithleyFreq.SetIncrement(0.001)
        BoxKeithleyFreq.SetMin(21.600)
        BoxKeithleyFreq.SetMax(21.650)
        BoxKeithleyFreq.SetValue(21.621)
        print(BoxKeithleyFreq.GetValue())

        # add widgets to the StaticBoxSizer for Keithley controls  
        BoxKeithleyGrid.Add(TextKeithleyFreq, pos=(0,0), flag = wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        BoxKeithleyGrid.Add(BoxKeithleyFreq, pos=(0,1), span=(1,5), flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL)
        BoxKeithleyGrid.Add(UnitKeithleyFreq, pos=(0,6), flag = wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)

        BoxKeithleySizer.Add(BoxKeithleyGrid)
        # add Sizers to main vbox and panel  
        vbox.Add(BoxKeithleySizer, 0, wx.ALL|wx.LEFT, 5)
        panel.SetSizer(vbox)

class MyApp(wx.App):
    def OnInit(self):
        frame = Mywin()
        frame.Show()
        self.SetTopWindow(frame)
        return True

def main():
    app = MyApp(redirect=False)
    app.MainLoop()

if __name__ == "__main__":
    main()

Ecco37 avatar Jun 14 '25 09:06 Ecco37