2011-03-01 13 views
2

우분투 리눅스에서 wxPython을 배우고 있습니다. 기본적으로 이동하려는 선인 자체 위젯을 정의하고 싶습니다. 창문 주위 .. 어딘가에 있지만, 문제는 내가 '위젯'을 투명한 배경에 '그리기'로 할 수 없다는 것입니다. 내가 할 수있는 최선의이 같은 것입니다 (노란 선이 투명 배경으로 독립적 인 위젯해야 을 -하지만 배경 소음 검은 색이) :wxPython - 투명/알파 배경에 그리기 (사용자 정의 위젯/패널 용)

bad transparency in this wxPython example

내가 생각 해낸 코드는 이하. 전체 창을 투명하게하고 싶지 않습니다. (wxpython - Python drawing on screen - Stack Overflow); 나는 wx.TRANSPARENT는 텍스트만을위한 것이며, 나는 wx.GCDC를 시도해야만한다. 그러나 작동하지 않는다. (wx.PaintDC and SetBackgroundMode(wx.TRANSPARENT) support - wxPython-users | Google Groups) 분명히 "wxGTK에서는 불가능하다."(wxPython-users - transparent background for a panel widget) ...

투명한 비트 맵/이미지를 사용하는 것이 유일한 방법 인 것처럼 보이고이를 사용자 정의 위젯의 배경으로 사용하는 것이 맞겠습니까? 그렇다면 wxPython에서이 비트 맵/이미지를 직접 생성 할 수 있습니까? (자체 포함 스크립트를 목표로하고 있습니다. 외부 .png에 의존하지 않는 것이 좋습니다 :))? 이것이 가능한 접근 인 경우 (나는 모든에서 사용하는 이런 종류의 예제를 찾을 수 ) 그리고, 누군가가, 사전에 어떤 도움 .. 최소한의 일 예에
건배를

감사를 날 지점 수 ! 어쩌면 당신은 직류의 GraphicsContext istead (DrawingContext)를 살펴해야

#!/usr/bin/python 
# -*- coding: utf-8 -*- 

import wx 

class CustomLine(wx.Panel): #PyControl 
    """ 
    A custom class for a line 
    Modified from http://wiki.wxpython.org/CreatingCustomControls 
    """ 
    def __init__(self, parent, id=wx.ID_ANY, label="", pos=wx.DefaultPosition, 
      size=wx.DefaultSize, style=wx.NO_BORDER, validator=wx.DefaultValidator, 
      name="CustomLine"): 
     """ 
     Default class constructor. 

     @param parent: Parent window. Must not be None. 
     @param id: CustomLine identifier. A value of -1 indicates a default value. 
     @param label: Text to be displayed next to the checkbox. 
     @param pos: CustomLine position. If the position (-1, -1) is specified 
        then a default position is chosen. 
     @param size: CustomLine size. If the default size (-1, -1) is specified 
        then a default size is chosen. 
     @param style: not used in this demo, CustomLine has only 2 state 
     @param validator: Window validator. 
     @param name: Window name. 
     """ 
     #~ wx.PyControl.__init__(self, parent, id, pos, size, style, validator, name) 
     wx.Panel.__init__(self, parent, id, pos, size, style) 

     # Bind the events related to our control: first of all, we use a 
     # combination of wx.BufferedPaintDC and an empty handler for 
     # wx.EVT_ERASE_BACKGROUND (see later) to reduce flicker 
     self.Bind(wx.EVT_PAINT, self.OnPaint) 
     self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) 
     self.lpen = wx.Pen('yellow', 2, wx.SOLID) 
     self.imagebkg = wx.EmptyImage(10, 10) 
     #~ self.imagebkg.SetData((255,255,255)) 
     #~ self.imagebkg.SetAlphaData((1)) 

    def OnPaint(self, event): 
     """ Handles the wx.EVT_PAINT event for CustomLine. """ 

     # If you want to reduce flicker, a good starting point is to 
     # use wx.BufferedPaintDC. 
     pdc = wx.BufferedPaintDC(self) 
     dc = wx.GCDC(pdc) 

     # Is is advisable that you don't overcrowd the OnPaint event 
     # (or any other event) with a lot of code, so let's do the 
     # actual drawing in the Draw() method, passing the newly 
     # initialized wx.BufferedPaintDC 
     self.Draw(dc) 

    def Draw(self, dc): 
     """ 
     Actually performs the drawing operations, for the bitmap and 
     for the text, positioning them centered vertically. 
     """ 

     # Get the actual client size of ourselves 
     width, height = self.GetClientSize() 

     if not width or not height: 
      # Nothing to do, we still don't have dimensions! 
      return 

     # Initialize the wx.BufferedPaintDC, assigning a background 
     # colour and a foreground colour (to draw the text) 
     #~ backColour = self.GetBackgroundColour() 
     #~ backBrush = wx.Brush((1,1,1,150), wx.TRANSPARENT) # backColour 
     #~ backBrush = wx.Brush((10,10,1,150)) # backColour 
     dc.SetBackground(wx.TRANSPARENT_BRUSH) #() backBrush 
     #~ dc.SetBackgroundMode(wx.TRANSPARENT) 
     dc.Clear() 

     dc.SetPen(self.lpen) 
     dc.DrawLine(0, 0, 100, 100) 

    def OnEraseBackground(self, event): 
     """ Handles the wx.EVT_ERASE_BACKGROUND event for CustomLine. """ 

     # This is intentionally empty, because we are using the combination 
     # of wx.BufferedPaintDC + an empty OnEraseBackground event to 
     # reduce flicker 
     pass 

class MyTestFrame(wx.Frame): 
    def __init__(self, parent, title): 
     super(MyTestFrame, self).__init__(parent, title=title, 
      size=(250, 150)) 

     # the master panel of the frame - "Add a panel so it looks correct on all platforms" 
     self.panel = wx.Panel(self, wx.ID_ANY) 
      # self.panel.SetBackgroundColour(wx.Colour(124, 224, 124)) # to confirm the square is the panel 

     self.mpanelA = wx.Panel(self.panel, -1, size=(200,50)) 
     self.mpanelA.SetBackgroundColour((200,100,200)) 
     self.mpanelB = wx.Panel(self.panel, -1, size=(50,200), pos=(50,30)) 
     self.mpanelB.SetBackgroundColour(wx.Colour(200,100,100,100)) 

     self.cline = CustomLine(self.panel, -1, size=(-1,200)) 

     self.Centre() 
     self.Show() 


if __name__ == '__main__': 
    app = wx.App() 
    MyTestFrame(None, 'Test') 
    app.MainLoop() 

답변

1

: 위의 이미지를 생성

코드입니다. 패널의 투명한 직사각형을 그리는 것과 같이 투명도를 더 잘 지원합니다.

+0

@Lars에 감사드립니다. 곧 확인하고 다시보고 할 것입니다. 건배! – sdaau