5
我想在wxPython中創建一個簡單形狀的窗口。或多或少,我想要做的WX相當於Tkinter的self.overrideredirect(1)(它擺脫了默認的操作系統邊界),然後繞過窗口的角落。在wxPython中創建一個形狀窗口最簡單的方法是什麼?
我想在wxPython中創建一個簡單形狀的窗口。或多或少,我想要做的WX相當於Tkinter的self.overrideredirect(1)(它擺脫了默認的操作系統邊界),然後繞過窗口的角落。在wxPython中創建一個形狀窗口最簡單的方法是什麼?
wxPython演示中有一個有形的框架演示。我爲間接來源道歉。他們本來就以Windows安裝程序here:
你會想看看shaped_frame_mobile.py或shaped_frame.py,這兩個呼叫images.py從採樣窗口位圖上市。這與overrideredirect
並不完全相同,因爲您必須提供用於框架的圖像,但它仍可以幫助您完成類似的操作。
的重要部分是設置基於位圖的窗口形狀的功能和處理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)
編輯 - 下面是一個改變shaped_frame_mobile.py
加載在IMAGE_PATH指定的PNG圖片變量。將其改爲指向您的圖像:
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()
謝謝,儘管它引用了「圖像」模塊。我不確定哪個模塊是。你有任何線索嗎? – rectangletangle 2011-02-03 01:09:33