将 wx.DrawCircle 对齐到框架的中心

align wx.DrawCircle to the center of the frame

您好,我想在框架的中心画一个圆圈。 有没有像 wx.Align_Center 我可以和 wx.DrawCircle 一起使用的东西下面是我的代码。

#!/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()

只需获取框架的大小,然后将宽度和高度除以 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()

这会让你非常接近。您可能需要稍微调整一下高度,因为我认为 GetSize 不会考虑系统菜单的宽度(所有 windows 的顶部栏)。