2011-02-02 29 views

Trả lời

9

Có một bản trình bày khung hình trong các bản trình diễn wxPython. Tôi xin lỗi vì nguồn gián tiếp. Họ ban đầu được đưa ra khi một trình cài đặt windows here:

source code

Bạn sẽ muốn nhìn vào shaped_frame_mobile.py hoặc shaped_frame.py, mà cả hai cuộc gọi images.py từ danh sách đó cho bitmap cửa sổ mẫu. Nó không phải là chính xác tương đương với overrideredirect vì bạn sẽ phải cung cấp một hình ảnh được vẽ cho khung, nhưng nó vẫn có thể giúp bạn thực hiện một cái gì đó tương tự.

Các bộ phận quan trọng là những chức năng mà thiết lập các hình dạng cửa sổ dựa trên bitmap và xử lý các sự kiện wx.EVT_PAINT:

def SetWindowShape(self, evt=None): 
    r = wx.RegionFromBitmap(self.bmp) 
    self.hasShape = self.SetShape(r) 

def OnPaint(self, evt): 
    dc = wx.PaintDC(self) 
    dc.DrawBitmap(self.bmp, 0,0, True) 

Edit - Dưới đây là một thay đổi shaped_frame_mobile.py đó tải các hình ảnh .png quy định tại các IMAGE_PATH biến. Thay đổi điều đó để trỏ tới hình ảnh của bạn:

import wx 

# Create a .png image with something drawn on a white background 
# and put the path to it here. 
IMAGE_PATH = '/python26/projects/shapedwin/image.png' 


class ShapedFrame(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, -1, "Shaped Window", 
       style = wx.FRAME_SHAPED | wx.SIMPLE_BORDER) 
     self.hasShape = False 
     self.delta = wx.Point(0,0) 

     # Load the image 
     image = wx.Image(IMAGE_PATH, wx.BITMAP_TYPE_PNG) 
     image.SetMaskColour(255,255,255) 
     image.SetMask(True)    
     self.bmp = wx.BitmapFromImage(image) 

     self.SetClientSize((self.bmp.GetWidth(), self.bmp.GetHeight())) 
     dc = wx.ClientDC(self) 
     dc.DrawBitmap(self.bmp, 0,0, True) 
     self.SetWindowShape() 
     self.Bind(wx.EVT_LEFT_DCLICK, self.OnDoubleClick) 
     self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) 
     self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) 
     self.Bind(wx.EVT_MOTION, self.OnMouseMove) 
     self.Bind(wx.EVT_RIGHT_UP, self.OnExit) 
     self.Bind(wx.EVT_PAINT, self.OnPaint) 
     self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape) 

    def SetWindowShape(self, evt=None): 
     r = wx.RegionFromBitmap(self.bmp) 
     self.hasShape = self.SetShape(r) 

    def OnDoubleClick(self, evt): 
     if self.hasShape: 
      self.SetShape(wx.Region()) 
      self.hasShape = False 
     else: 
      self.SetWindowShape() 

    def OnPaint(self, evt): 
     dc = wx.PaintDC(self) 
     dc.DrawBitmap(self.bmp, 0,0, True) 

    def OnExit(self, evt): 
     self.Close() 

    def OnLeftDown(self, evt): 
     self.CaptureMouse() 
     pos = self.ClientToScreen(evt.GetPosition()) 
     origin = self.GetPosition() 
     self.delta = wx.Point(pos.x - origin.x, pos.y - origin.y) 

    def OnMouseMove(self, evt): 
     if evt.Dragging() and evt.LeftIsDown(): 
      pos = self.ClientToScreen(evt.GetPosition()) 
      newPos = (pos.x - self.delta.x, pos.y - self.delta.y) 
      self.Move(newPos) 

    def OnLeftUp(self, evt): 
     if self.HasCapture(): 
      self.ReleaseMouse() 



if __name__ == '__main__': 
    app = wx.PySimpleApp() 
    ShapedFrame().Show() 
    app.MainLoop() 
+0

Cảm ơn, mặc dù nó tham chiếu đến mô-đun "hình ảnh". Tôi không chắc chính xác mô-đun nào. Bạn có chút manh mối nào không? – rectangletangle

+0

Đó là tệp 'images.py' từ liên kết nguồn ở trên. Bạn có thể thả nó vào cùng thư mục với các thư mục khác, hoặc loại bỏ 'import images' và nạp bitmap của riêng bạn thay cho dòng' images.getVippiBitmap() '. Tôi không biết tại sao bản demo sử dụng một mô-đun python để cung cấp một hình ảnh. –

+0

@ Anteater7171 Đã thêm mã được thay đổi vào câu trả lời của tôi. Hiển thị dễ hơn là giải thích. –

Các vấn đề liên quan