【问题标题】:align wx.DrawCircle to the center of the frame将 wx.DrawCircle 对齐到框架的中心
【发布时间】:2015-03-02 14:43:35
【问题描述】:

您好,我想在框架的中心画一个圆圈。 有没有像 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()

【问题讨论】:

    标签: python python-2.7 wxpython wxwidgets


    【解决方案1】:

    只需获取框架的大小,然后将宽度和高度除以 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 不会考虑系统菜单(所有窗口的顶部栏)的宽度。

    【讨论】:

    • GetClientSize 将排除框架客户区域之外的部分,如标题栏和边框。
    猜你喜欢
    • 1970-01-01
    • 2010-12-16
    • 2021-10-10
    • 2014-09-15
    • 2013-05-30
    • 2021-10-24
    • 2016-11-16
    • 2021-02-25
    • 2015-08-24
    相关资源
    最近更新 更多