Search code examples
pythonpython-2.7wxpythonwxwidgets

align wx.DrawCircle to the center of the frame


Hi there I wanted to draw a circle at the center of the Frame. Is there anything like wx.Align_Center I can use with wx.DrawCircle Below is my code.

#!/usr/bin/python

# points.py

import wx
import random

class Points(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(250, 150))

        self.Bind(wx.EVT_PAINT, self.OnPaint)

        self.Centre()
        self.Show(True)

    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        dc.SetBrush(wx.Brush('RED'))
        dc.DrawCircle(20,20)


app = wx.App()
Points(None, -1, 'Test')
app.MainLoop()

Solution

  • Just get the size of the frame and then divide the width and the height by 2.

    import wx
    import random
    
    class Points(wx.Frame):
        def __init__(self, parent, id, title):
            wx.Frame.__init__(self, parent, id, title, size=(250, 150))
    
            self.Bind(wx.EVT_PAINT, self.OnPaint)
    
            self.Centre()
            self.Show(True)
    
        def OnPaint(self, event):
            w, h = self.GetClientSize()
            dc = wx.PaintDC(self)
            dc.SetBrush(wx.Brush('RED'))
            dc.DrawCircle(w/2,h/2, 20)
    
    
    app = wx.App()
    Points(None, -1, 'Test')
    app.MainLoop()
    

    That will get you pretty close. You might need to tweak the height a bit as I don't think GetSize accounts for the width of the System Menu (the top bar on all windows).