2010-02-24 195 views
6

我正在編寫一個需要選擇屏幕區域的應用程序。我需要將光標更改爲十字,然後在用戶選擇上繪製一個矩形。我搜索的第一件事是如何操作光標,我遇到了wxPython。使用wxPython,我可以很容易地在一個帶有面板的框架上做到這一點,事情是我需要窗口是透明的,這樣用戶可以在選擇所需區域時看到他的屏幕,但是如果我使框架和麪板對象透明的一切都變得越野車。屏幕上的Python繪圖

所以,我打開任何解決方案,無論是使用wxPython或不使用它,因爲我真的不知道我是否正確使用它。

我是Python的新手,我不是英語母語的人,所以我很抱歉如果你不明白的東西。

這是我編寫

import wx 

class SelectableFrame(wx.Frame): 

    c1 = None 
    c2 = None 

    def __init__(self, parent=None, id=-1, title=""): 
     wx.Frame.__init__(self, parent, id, title, size=wx.DisplaySize(), style=wx.TRANSPARENT_WINDOW) 

     self.panel = wx.Panel(self, size=self.GetSize(), style=wx.TRANSPARENT_WINDOW) 

     self.panel.Bind(wx.EVT_MOTION, self.OnMouseMove) 
     self.panel.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown) 
     self.panel.Bind(wx.EVT_LEFT_UP, self.OnMouseUp) 
     self.panel.Bind(wx.EVT_PAINT, self.OnPaint) 

     self.SetCursor(wx.StockCursor(wx.CURSOR_CROSS)) 

    def OnMouseMove(self, event): 
     if event.Dragging() and event.LeftIsDown(): 
      self.c2 = event.GetPosition() 
      self.Refresh() 

    def OnMouseDown(self, event): 
     self.c1 = event.GetPosition() 

    def OnMouseUp(self, event): 
     self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW)) 

    def OnPaint(self, event): 
     if self.c1 is None or self.c2 is None: return 

     dc = wx.PaintDC(self.panel) 
     dc.SetPen(wx.Pen('red', 1)) 
     dc.SetBrush(wx.Brush(wx.Color(0, 0, 0), wx.TRANSPARENT)) 

     dc.DrawRectangle(self.c1.x, self.c1.y, self.c2.x - self.c1.x, self.c2.y - self.c1.y) 

    def PrintPosition(self, pos): 
     return str(pos.x) + " " + str(pos.y) 


class MyApp(wx.App): 

    def OnInit(self): 
     frame = SelectableFrame() 
     frame.Show(True) 
     self.SetTopWindow(frame) 

     return True 



app = MyApp(0) 
app.MainLoop() 

回答

5

你不應該在窗口創建使用wx.TRANSPARENT,即主要用於油漆wxDC命令。要使窗口透明,只需調用win.SetTransparent(amount),其中數量爲0-255,255表示不透明,0表示完全透明。請參閱http://www.wxpython.org/docs/api/wx.Window-class.html#SetTransparent

我已經修改了您的代碼,只有在您的平臺支持透明窗口時才能使用,您可以通過CanSetTransparent查看。我在Windows XP上測試過它。

import wx 

class SelectableFrame(wx.Frame): 

    c1 = None 
    c2 = None 

    def __init__(self, parent=None, id=-1, title=""): 
     wx.Frame.__init__(self, parent, id, title, size=wx.DisplaySize()) 

     self.panel = wx.Panel(self, size=self.GetSize()) 

     self.panel.Bind(wx.EVT_MOTION, self.OnMouseMove) 
     self.panel.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown) 
     self.panel.Bind(wx.EVT_LEFT_UP, self.OnMouseUp) 
     self.panel.Bind(wx.EVT_PAINT, self.OnPaint) 

     self.SetCursor(wx.StockCursor(wx.CURSOR_CROSS)) 

     self.SetTransparent(50) 

    def OnMouseMove(self, event): 
     if event.Dragging() and event.LeftIsDown(): 
      self.c2 = event.GetPosition() 
      self.Refresh() 

    def OnMouseDown(self, event): 
     self.c1 = event.GetPosition() 

    def OnMouseUp(self, event): 
     self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW)) 

    def OnPaint(self, event): 
     if self.c1 is None or self.c2 is None: return 

     dc = wx.PaintDC(self.panel) 
     dc.SetPen(wx.Pen('red', 1)) 
     dc.SetBrush(wx.Brush(wx.Color(0, 0, 0), wx.TRANSPARENT)) 

     dc.DrawRectangle(self.c1.x, self.c1.y, self.c2.x - self.c1.x, self.c2.y - self.c1.y) 

    def PrintPosition(self, pos): 
     return str(pos.x) + " " + str(pos.y) 


class MyApp(wx.App): 

    def OnInit(self): 
     frame = SelectableFrame() 
     frame.Show(True) 
     self.SetTopWindow(frame) 

     return True 


app = MyApp(0) 
app.MainLoop() 
+0

謝謝,這解決了越野車問題,但現在我有另一個問題!選擇矩形也變得透明,所以我不能讓窗口完全透明並仍然看到選擇。但是現在這沒問題。 – Franco 2010-02-24 05:12:34