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):
    return 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 
    print b
    self.width, self.height = wx.GetDisplaySize()
    c = self.width, self.height
    print c

  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()
print frame.GetPanelSize()

Much appreciated,

Recommended Answers

All 3 Replies

Easiest way is to add

import wx.lib.mixins.inspection

at the top and




This will bring up an inspection window where you can examine all of your display items.

I strongly recommend that you upgrade to Python 3.9.x and the latest wxPython. Don't pick the most recent Python as there is not currently a wxPython build for that release.

I also recommend you take a look at OOP naming conventions. Typically, objects are named for what they are and methods for what they do.

I had added wx.lib.inspection to verify the size of the panel and saw the correct result. (1920. 968)
Is it possible to grab that result somehow to use in the program logic?


That's the nice thing about the inspection tool. The lower panel allows you to execute Python code on the fly. It also has intellisense. Select the object you are interested in from the top left panel (clicking the Sizer icon on the toolbar will eliminate sizer items from the tree). Then click in the lower panel. You'll see the Size propety in the top right panel. The currently selected object can be referred to by obj. So in the lower panel start typing


When you type the dot you will get a popup of all valid methods and properties. Either scroll down or continue typing something like


and when you press enter you will see the object resized. Very handy for trying out minor code changes or looking for the right method to use.

I frequently use

DEBUG = True

if DEBUG: import wx.lib.mixins.inspection
if DEBUG: wx.lib.inspection.InspectionTool().Show()

in code while I'm developing. Easy to just change it to

DEBUG = False

and just leave it in later.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.