Search code examples
pythonwxpython

Buttons overlap each other


I have the following code:

    self.btn1 = wx.Button(self, -1, _("a"))
    self.btn2 = wx.Button(self, -1, _("b"))
    btnSizer = wx.BoxSizer(wx.HORIZONTAL)
    btnSizer.Add(self.btn1 , 0, wx.RIGHT, 10)
    btnSizer.Add(self.btn2 , 0, wx.RIGHT, 10)

This works well. However there is a case where I change the title of btn2 :

self.btn1.SetLabel('bbbbb')

When I do that btn1 overlaps btn2....

first row is the original second row is after set label.

enter image description here

How do I make the screen refresh to the new size of buttons?


Solution

  • You can use self.Layout() but in this case it really shouldn't be necessary. There must be some issue that you are having with your code.

    import wx
    
    class ButtonFrame(wx.Frame):
        def __init__(self, value):
            wx.Frame.__init__(self,None)
            self.btn1 = wx.Button(self, -1, ("a"))
            self.btn2 = wx.Button(self, -1, ("b"))
            self.btnSizer = wx.BoxSizer(wx.HORIZONTAL)
            self.btnSizer.Add(self.btn1 , 0, wx.RIGHT, 10)
            self.btnSizer.Add(self.btn2 , 0, wx.RIGHT, 10)
            self.btn1.Bind(wx.EVT_BUTTON, self.OnPressA)
            self.btn2.Bind(wx.EVT_BUTTON, self.OnPressB)
            self.SetSizer(self.btnSizer)
            self.Centre()
            self.Show()
    
        def OnPressA(self,evt):
            self.btn1.SetLabel('bbbbbbbbbbbbbbbbbbbbbbbbbb')
    #        self.Layout()
    
        def OnPressB(self,evt):
            self.btn2.SetLabel('aaaaaaaaaaaaaaaaaaaaaaaaaa')
    #        self.Layout()
    
    if __name__ == "__main__":
        app = wx.App(False)
        ButtonFrame(None)
        app.MainLoop()