How to get the size of the wxpython panel?

I’m developing a calendar application The top level window is a frame containing a panel that displays the calendar grid and a panel that contains a “Close” button. I’m unable to obtain the size of the calendar grid panel. When I add code to get the panel size, the result is (20,20), which cannot be correct The screen size is (1920,1080) so I’m expecting something like (1920, 1000) When I add the wx.lib.inspection module, I see the correct size being displayed. It is (1920, 968)

Can anyone shed some light how to get the correct size of the panel?

This is the code I have so far

import wx

class DrawFrame(wx.Frame):
  def __init__(self):
    wx.Frame.__init__(self, parent=None, title='Agenda', style= wx.CAPTION | wx.CLOSE_BOX)
    self.drawpanel = DrawPanel(self)
    self.buttonpanel = ButtonPanel(self)
    self.framesizer = wx.BoxSizer(wx.VERTICAL)
    self.framesizer.Add(self.drawpanel,1, flag=wx.EXPAND)

    # Add an empty space 10 pixels high above and below the button panel
    self.framesizer.Add(self.buttonpanel,0, flag=wx.EXPAND)

  def GetPanelSize(self):
    print self.drawpanel.GetSize()

  def OnClose(self, event):

class DrawPanel(wx.Panel):
  # This panel's parent is DrawFrame. DrawFrame is the top level window.
  def __init__(self, parent):
    wx.Panel.__init__(self, parent=parent)
    self.parent = parent
    self.Bind(wx.EVT_PAINT, self.OnPaint)
    self.x1, self.y1, self.x2, self.y2 = wx.GetClientDisplayRect()
    b = self.x1, self.y1, self.x2, self.y2 
    self.width, self.height = wx.GetDisplaySize()
    c = self.width, self.height

  def OnPaint(self, event=None):
    dc = wx.PaintDC(self)
    dc.SetPen(wx.Pen(wx.BLACK, 2))

    DrawRectangle (self, x, y, width, height)
    Draw a rectangle with the given corner coordinate and size.
    x and y specify the top left corner coordinates and both width and height are positive.

    dc.DrawRectangle(self.x1 + 5, self.y1, self.x2 - 10, self.y2 - 60)
    dc.DrawLine(40, 100, 600, 100)

class ButtonPanel(wx.Panel):
  # This panel's parent is DrawFrame. DrawFrame is the top level window.
  def __init__(self, parent):
    wx.Panel.__init__(self, parent=parent)
    self.buttonpanelsizer = wx.BoxSizer(wx.HORIZONTAL)
    self.closebutton = wx.Button(self, label = 'Close')
    self.Bind(wx.EVT_BUTTON, self.OnClose, self.closebutton)
    self.buttonpanelsizer.Add(self.closebutton, 0, wx.ALIGN_CENTER)

  def OnClose(self, event):

app = wx.App(False)
frame = DrawFrame()

Much appreciated,

Hi @gdtraveller

I think the problem is that you are calling frame.GetPanelSize() before starting the main loop.

In DrawFrame.__init__() you call self.Maximize() but I think the actual maximising of the frame doesn’t happen until the main loop is running.

Are you running the wxGTK port of wxPython?

There is a mention of this in the documentation for wx.TopLevelWindow.Maximize() and wx.TopLevelWindow.Iconize() which are inherited by wx.Frame.

As a simple test, I added a call to self.GetPanelSize() to the OnClose() method as it will be called when the main loop is running:

  def OnClose(self, event):

When I click on the Close button, the program prints “(1920, 974)”.

[Note: I had to change your print statement to a print() function call because I am using Python 3.8.]

Thanks very much for that explanation.

I assumed incorrectly that the maximising of the frame occurred when init completed.