在wxpython中帮助自定义GUI

时间:2010-07-19 21:36:11

标签: python user-interface wxpython

我是wxPython的新手,所以请耐心等待。我正在创建一个自定义GUI设置,需要获得两个属性。首先,我想创造一个不同颜色的内部边界(单个黑暗边界看起来太平淡)。其次,我需要绑定拖动属性,以便只有标签允许拖动而不是整个框架。

还有没有人有关于wxPython几何处理的好教程?

import wx

def GetRoundBitmap( w, h, r ):
    maskColor = wx.Color(0,0,0)
    shownColor = wx.Color(5,5,5)
    b = wx.EmptyBitmap(w,h)
    dc = wx.MemoryDC(b)
    dc.SetBrush(wx.Brush(maskColor))
    dc.DrawRectangle(0,0,w,h)
    dc.SetBrush(wx.Brush(shownColor))
    dc.SetPen(wx.Pen(shownColor))
    dc.DrawRoundedRectangle(0,0,w,h,r)
    dc.SelectObject(wx.NullBitmap)
    b.SetMaskColour(maskColor)
    return b

def GetRoundShape( w, h, r ):
    return wx.RegionFromBitmap( GetRoundBitmap(w,h,r) )

class FancyFrame(wx.Frame):
    def __init__(self):
        style = ( wx.CLIP_CHILDREN | wx.STAY_ON_TOP | wx.FRAME_NO_TASKBAR |
              wx.NO_BORDER | wx.FRAME_SHAPED  )
        wx.Frame.__init__(self, None, title='Fancy', style = style)
        self.SetSize( (250, 40) )
        self.SetPosition( (500,500) )
        self.SetTransparent( 160 )

        self.Bind(wx.EVT_KEY_UP, self.On_Esc)
        self.Bind(wx.EVT_MOTION, self.OnMouse)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        if wx.Platform == '__WXGTK__':
            self.Bind(wx.EVT_WINDOW_CREATE, self.SetRoundShape)
        else:
            self.SetRoundShape()

        self.Show(True)

        geo = wx.GridBagSizer()
        self.label = wx.StaticText(self,-1,label=u'Hello !')
        geo.Add(self.label, (0,2))

    def SetRoundShape(self, event=None):
        w, h = self.GetSizeTuple()
        self.SetShape(GetRoundShape( w,h, 10 ) )


    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        dc = wx.GCDC(dc)
        w, h = self.GetSizeTuple()
        r = 10
        dc.SetPen( wx.Pen("#000000", width = 4 ) )
        dc.SetBrush( wx.Brush("#9e9e9e") )
        dc.DrawRoundedRectangle( 0,0,w,h,r )

    def On_Esc(self, event):
        """quit if user press Esc"""
        if event.GetKeyCode() == 27 : #27 is Esc
            self.Close(force=True)
        else:
            event.Skip()

     def OnMouse(self, event):
        """implement dragging"""
        if not event.Dragging():
            self._dragPos = None
            return
        self.CaptureMouse()
        if not self._dragPos:
            self._dragPos = event.GetPosition()
        else:
            pos = event.GetPosition()
            displacement = self._dragPos - pos
            self.SetPosition( self.GetPosition() - displacement )



app = wx.App()
f = FancyFrame()
app.MainLoop()

1 个答案:

答案 0 :(得分:0)

我不确定你想改变什么边界。你在谈论所选对象的边界吗?至于拖动物体,我建议看一下wickPython绘图程序Whyteboard。应该如何选择和拖动对象非常好,并在Windows和Linux上工作。我不确定Mac ......

修改:忘记添加链接:http://whyteboard.org/


迈克·德里斯科尔

博客:http://blog.pythonlibrary.org