Samuel
Samuel

Reputation: 6237

Can't put the panel at the specified position on wxpython

I can get the correct result if I don't comment the self.panel1. If I comment the self.panel1. The panel2 can't render at position(200,0). It take the full frame window.

Following is my code

#!/usr/bin/env python
import wx

class MyForm(wx.Frame):
   def __init__(self):
        wx.Frame.__init__(self, None, size=(400,400))    

        # self.panel1=wx.Panel(parent=self,
        #                      size=(200,200),
        #                      pos=(0,0),
        #                      style=wx.BORDER)            

        self.panel2=wx.Panel(parent=self,
                             size=(200,200),
                             pos=(200,0),
                             style=wx.BORDER)


# Run the program
app = wx.App(False)
frame = MyForm().Show()
app.MainLoop()

Upvotes: 2

Views: 679

Answers (1)

falsetru
falsetru

Reputation: 369054

According to wxWidget - wxFrame documentation:

wxFrame processes the following events:

wxEVT_SIZE: if the frame has exactly one child window, not counting the status and toolbar, this child is resized to take the entire frame client area. If two or more windows are present, they should be laid out explicitly either by manually handling wxEVT_SIZE or using sizers;

Workaround: Make a extra panel (outer_panel in the following example) to contain a single panel.

import wx

class MyForm(wx.Frame):
   def __init__(self):
        wx.Frame.__init__(self, None, size=(400,400))    
        self.outer_panel = wx.Panel(self) # <--------------
        self.panel2=wx.Panel(parent=self.outer_panel,
                             size=(200,200),
                             pos=(200,0),
                             style=wx.BORDER)
        self.panel2.SetBackgroundColour(wx.RED)

app = wx.App(False)
MyForm().Show()
app.MainLoop()

UPDATE Another solution suggested by Robin Dunn; catch frame's EVT_SIZE to prevent the default handler from being called.

import wx

class MyForm(wx.Frame):
   def __init__(self):
        wx.Frame.__init__(self, None, size=(400,400))
        self.Bind(wx.EVT_SIZE, lambda *args: 0) # <--------
        self.panel2=wx.Panel(parent=self,
                             size=(200,200),
                             pos=(200,0),
                             style=wx.BORDER)


app = wx.App(False)
frame = MyForm().Show()
app.MainLoop()

Upvotes: 1

Related Questions