pythonpython-2.7wxpythonsizer

wx python mysteriously adding a newline character to GUI output


so the goal here is to take an input from the user and display relevant data from an excel sheet. Our problem is a rather minor, but irritating one... When the input doesn't get a match from the database, we will display a message saying that the input number was invalid. We used a helper function to format the output to look like this

Here's the formatting code:

    def makeRMAString(self, RMAnum, DMRnum, serial, status, cdate, udate):
        rma_string = 'RMA#\t: {}\nDMR#\t: {} \nSerial#\t: {}\nStatus\t: {}\nCreated\t: {}\nUpdate\t: {}'
        return rma_string.format(RMAnum, DMRnum, serial, status, cdate, udate)

Screenshot shows that all is well when the input matches something in the database. When it doesn't, we execute the following line:

self.rmaData.Label = self.makeRMAString('Invalid RMA number', '' , '', '', '', '')

And this is where the issue occurs; instead of having a nicely formatted output, the 'Invalid RMA number' gets pushed into the next line as if there was a nextline added in: like this

I launched from command prompt to see the output, and it looks just fine there. Here's proof

We are using wxpython's BoxSizer to help:

self.rmaData = wx.StaticText(self.panel, wx.ID_ANY, self.makeRMAString('','','','','',''))
self.sizer_rmaHoriz = wx.BoxSizer(wx.HORIZONTAL)
self.sizer_rmaHoriz.Add(self.rmaData, 4, wx.ALIGN_LEFT|wx.CENTER, 15)
self.rmaData.SetFont(font)

We are using python 2.7.10


Solution

  • As requested a demo of a Grid sizer.
    I've chosen a GridBagSizer, it seems the most appropriate to your requirement.

    import wx  
    data = [
            ["99550","539761","016833-0695","Shipped","03/27/20","Unknown"],
            ["99551","539761","xxxxxx-xxxx","Shipped","03/22/20","Unknown"],
            ["99554","539761","016833-0695","Warehouse 1","01/12/19","Unknown"],
            ['Invalid RMA number','','','','','',''],
            ["99555","539761","777777-0695","Warehouse 2","02/27/20","Unknown"],
            ["99556","539761","016833-XXXX","Shipped","03/27/20","03/24/20"],
            ['Invalid RMA number','','','','','','']
            ]
            
    class Example(wx.Frame): 
       
        def __init__(self, parent, title): 
            super(Example, self).__init__(parent, title = title) 
                 
            self.InitUI() 
            self.Centre() 
            self.Show()      
             
        def InitUI(self): 
            self.idx = 0       
            panel = wx.Panel(self) 
            sizer = wx.GridBagSizer(0,0)
    
            self.rma = wx.TextCtrl(panel,style=wx.TE_PROCESS_ENTER) 
            sizer.Add(self.rma, pos = (0, 1), flag = wx.ALIGN_CENTRE_VERTICAL)  
            EnterB = wx.Button(panel, label = "Enter RMA") 
            sizer.Add(EnterB, pos = (0,2),flag = wx.ALL, border = 5)    
            
            text = wx.StaticText(panel, label = "RMA   :") 
            sizer.Add(text, pos = (1, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
                
            self.tc = wx.TextCtrl(panel, style=wx.TE_READONLY, size=(150,-1)) 
            sizer.Add(self.tc, pos = (1, 1), flag = wx.EXPAND) 
                 
            text1 = wx.StaticText(panel, label = "DMR   :") 
            sizer.Add(text1, pos = (2, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5) 
            
            self.tc1 = wx.TextCtrl(panel, style=wx.TE_READONLY) 
            sizer.Add(self.tc1, pos = (2,1), flag = wx.EXPAND) 
                 
            text2 = wx.StaticText(panel,label = "Serial :") 
            sizer.Add(text2, pos = (3, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5) 
                
            self.tc2 = wx.TextCtrl(panel, style=wx.TE_READONLY) 
            sizer.Add(self.tc2, pos = (3,1), flag = wx.EXPAND) 
                
            text3 = wx.StaticText(panel,label = "Status :") 
            sizer.Add(text3, pos = (4, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5)
                
            self.tc3 = wx.TextCtrl(panel, style=wx.TE_READONLY) 
            sizer.Add(self.tc3, pos = (4,1),flag = wx.EXPAND) 
                 
            text4 = wx.StaticText(panel,label = "Created:") 
            sizer.Add(text4, pos = (5, 0), flag = wx.ALIGN_CENTRE_VERTICAL|wx.ALL, border = 5) 
                
            self.tc4 = wx.TextCtrl(panel, style=wx.TE_READONLY) 
            sizer.Add(self.tc4, pos = (5,1), flag = wx.EXPAND) 
        
            text5 = wx.StaticText(panel,label = "Update :") 
            sizer.Add(text5, pos = (6, 0), flag = wx.ALIGN_CENTRE_VERTICAL) 
                
            self.tc5 = wx.TextCtrl(panel, style=wx.TE_READONLY) 
            sizer.Add(self.tc5, pos = (6,1), flag = wx.EXPAND)          
        
            Next = wx.Button(panel, label = "Demo") 
                
            sizer.Add(Next, pos = (7, 1)) 
                
            panel.SetSizerAndFit(sizer)
    
            EnterB.Bind(wx.EVT_BUTTON, self.OnRma)
            self.rma.Bind(wx.EVT_TEXT_ENTER, self.OnRma)
            Next.Bind(wx.EVT_BUTTON, self.OnNext)
            
        def OnRma(self, event):
            rma = self.rma.GetValue()
            idx = [i for i, e in enumerate(data) if e[0] == rma]
            if idx:
                self.idx = idx[0]
                self.OnNext(None)
            else:
                self.tc.SetValue('Invalid RMA number')                       
                self.tc1.SetValue('')                       
                self.tc2.SetValue('')                       
                self.tc3.SetValue('')                       
                self.tc4.SetValue('')                       
                self.tc5.SetValue('')                       
    
        def OnNext(self, event):
            self.rma.SetValue('')                       
            self.tc.SetValue(data[self.idx][0])                       
            self.tc1.SetValue(data[self.idx][1])                       
            self.tc2.SetValue(data[self.idx][2])                       
            self.tc3.SetValue(data[self.idx][3])                       
            self.tc4.SetValue(data[self.idx][4])                       
            self.tc5.SetValue(data[self.idx][5])                       
            self.idx += 1
            if self.idx > len(data) - 1:
                self.idx = 0
            
    app = wx.App() 
    Example(None, title = 'GridBag Demo') 
    app.MainLoop()
    

    Useage:

    enter image description here

    enter image description here