1

我想在上面画一张背景图片和一些较小的图片(在前景中)。据我了解,首先绘制的图片是前景,如果我在同一位置绘制第二张图片,那么它将在背景中。我的问题是,背景图片必须在开始时绘制,然后(在某个事件中)前景图片应绘制在该背景图片的顶部。我不好的解决方案:销毁第一张(背景)图片并重新绘制它。问题:图片闪烁。那么有没有更好的解决方案呢?这是一些代码:

def drawBG(self):    
    self.picBG = wx.StaticBitmap(self,size=(1020,252),pos=(0,160))
    self.picBG.SetBitmap(wx.Bitmap(path))

def drawFG(self,event):
    self.picFG = wx.StaticBitmap(self,size=(80,80),pos=(500,180))
    self.picFG.SetBitmap(wx.Bitmap(path))
    self.picBG.Destroy()
    self.drawBG()
4

1 回答 1

1

您可以在面板上设置背景图像,然后使用 wx.StaticBitmap 小部件将其他图像放在它上面。我写了一篇关于如何在 wx.Panel 上放置背景图片的教程:http: //www.blog.pythonlibrary.org/2010/03/18/wxpython-putting-a-background-image-on-a-panel /

这是我教程中的代码:

import wx

########################################################################
class MainPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)
        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
        self.frame = parent

        sizer = wx.BoxSizer(wx.VERTICAL)
        hSizer = wx.BoxSizer(wx.HORIZONTAL)

        for num in range(4):
            label = "Button %s" % num
            btn = wx.Button(self, label=label)
            sizer.Add(btn, 0, wx.ALL, 5)
        hSizer.Add((1,1), 1, wx.EXPAND)
        hSizer.Add(sizer, 0, wx.TOP, 100)
        hSizer.Add((1,1), 0, wx.ALL, 75)
        self.SetSizer(hSizer)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)

    #----------------------------------------------------------------------
    def OnEraseBackground(self, evt):
        """
        Add a picture to the background
        """
        # yanked from ColourDB.py
        dc = evt.GetDC()

        if not dc:
            dc = wx.ClientDC(self)
            rect = self.GetUpdateRegion().GetBox()
            dc.SetClippingRect(rect)
        dc.Clear()
        bmp = wx.Bitmap("butterfly.jpg")
        dc.DrawBitmap(bmp, 0, 0)


########################################################################
class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, size=(600,450))
        panel = MainPanel(self)        
        self.Center()

########################################################################
class Main(wx.App):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, redirect=False, filename=None):
        """Constructor"""
        wx.App.__init__(self, redirect, filename)
        dlg = MainFrame()
        dlg.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = Main()
    app.MainLoop()

现在您只需将按钮换成 StaticBitmap 小部件。

于 2013-04-08T21:49:22.660 回答