我正在尝试删除放置在面板上某个随机点的图像。我以前曾试图找到这个here的解决方案,但没有人能够提供帮助。
我使用wx.DC取得了一些进展。但是,Clear方法的DC类的文档说:
Clear Clears the device context using the current background brush.
我在下面的程序中试过这个,但是清除的区域与面板背景颜色的颜色并不完全相同。任何人都可以建议如何将画笔颜色设置为面板背景颜色?
import wx
import random
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Remove image")
panel = MainPanel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(panel)
self.SetSizerAndFit(sizer)
self.Centre()
self.Show()
class MainPanel(wx.Panel):
def __init__(self, frame):
wx.Panel.__init__(self, frame)
self.Bind(wx.EVT_PAINT, self._on_paint)
self.frame = frame
# Button
cmd_refresh = wx.Button(self, wx.ID_REFRESH)
cmd_refresh.Bind(wx.EVT_BUTTON, self._on_cmd_refresh_click)
# Main sizer
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add((500, 300))
sizer.Add(cmd_refresh, flag=wx.ALL|wx.ALIGN_CENTER, border=10)
self.SetSizer(sizer)
self.started = False
def _on_paint(self, event):
del event
if not self.started:
bitmap = self._get_image()
self._draw_image(bitmap)
self._draw_image(bitmap)
self.started = True
def _get_image(self):
bitmap = wx.Bitmap()
bitmap.LoadFile("red.png", wx.BITMAP_TYPE_ANY)
self.image_width = bitmap.GetWidth()
self.image_height = bitmap.GetHeight()
return bitmap
def _draw_image(self, bitmap):
self.x_pos = random.randint(0, self.frame.GetSize()[0]-self.image_width)
self.y_pos = random.randint(0, self.frame.GetSize()[1]-self.image_height)
dc = wx.PaintDC(self)
dc.DrawBitmap(bitmap, self.x_pos, self.y_pos, True)
def _on_cmd_refresh_click(self, event):
del event
dc = wx.ClientDC(self)
points = (self.x_pos, self.y_pos, self.image_width, self.image_height)
dc.DestroyClippingRegion()
dc.SetClippingRegion(points)
dc.Clear()
if __name__ == '__main__':
screen_app = wx.App()
main_frame = MainFrame()
screen_app.MainLoop()
答案 0 :(得分:0)
我有一个解决方法
我已放置两行
dc = wx.ClientDC(self)
panel.SetBackgroundColour(dc.GetBackground().Colour)
在MainFrame类的 init 方法中。它有效,但不能令人满意,因为面板颜色不再是主题定义的(在我的情况下(225,225,225)而不是(212,212,212))。
这可能看起来不太重要,但