网上看到的一段代码,很简洁.创建不规则窗体.例如(实际运行不会看到白色部分,下面的效果跟网页背景有关):
下载文件,保存为bg.png,跟py文件放在同一路径下,直接运行即可(必须已经安装wxpython)
#!/usr/bin/env python #-*- coding:utf-8 -*- import sys reload(sys) sys.setdefaultencoding("utf-8") import wx import os.path class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self,parent=None,id=-1,title="不规则窗体",pos=(100,100),style=wx.FRAME_SHAPED|wx.SIMPLE_BORDER|wx.STAY_ON_TOP) self.pt = wx.Point(0,0) img = wx.Image(os.path.sep.join([os.path.curdir,'bg.png'])) img.SetMask(True) img.SetMaskColour(255,255,255) self.bg = wx.BitmapFromImage(img) self.SetSize(wx.Size(self.bg.GetWidth(),self.bg.GetHeight())) self.hasShape = False self.OnWindowCreate() self.Bind(wx.EVT_RIGHT_UP,self.OnRightClickEvent) self.Bind(wx.EVT_WINDOW_CREATE,self.OnWindowCreate) self.Bind(wx.EVT_LEFT_DOWN,self.OnLeftClickDown) self.Bind(wx.EVT_LEFT_UP,self.OnLeftClickUp) self.Bind(wx.EVT_MOTION,self.OnMouseMotion) self.Bind(wx.EVT_PAINT,self.OnPaint) def OnRightClickEvent(self,event): if wx.MessageBox("退出?","退出",wx.YES_NO,self) == 2: wx.Exit() def OnLeftClickDown(self,event): self.CaptureMouse() pos = event.GetPosition() self.pt = wx.Point(pos.x,pos.y) def OnMouseMotion(self,event): if event.Dragging() and event.LeftIsDown(): pos = self.ClientToScreen(event.GetPosition()) self.Move((pos.x-self.pt.x,pos.y-self.pt.y)) def OnLeftClickUp(self,event): if self.HasCapture(): self.ReleaseMouse() def OnWindowCreate(self,event=None): r = wx.RegionFromBitmap(self.bg) self.SetShape(r) def OnPaint(self,event): dc = wx.PaintDC(self) dc.DrawBitmap(self.bg,0,0,True) class MyApp(wx.App): def OnInit(self): self.frame = MyFrame() self.frame.Show(True) self.SetTopWindow(self.frame) return True if __name__ == "__main__": app = MyApp() app.MainLoop()
No comments :
Post a Comment