简体   繁体   English

如何使用DC.Clear()在wxPython中删除图像

[英]How to remove image in wxPython using DC.Clear()

I am trying to remove an image that has been placed at some random point on a panel. 我正在尝试删除放置在面板上某个随机点的图像。 I have previously tried to find a solution to this here , but no one has been able to help. 我以前试图找到解决这一这里 ,但没有一个人能提供帮助。

I have made some progress using wx.DC. 我在使用wx.DC方面取得了一些进展。 However, the documentation of the DC class for the Clear method says: 但是,Clear方法的DC类的文档说:

Clear   Clears the device context using the current background brush.

I have tried this in the following program, however the cleared region is not quite the same colour as the panel background colour. 我在以下程序中尝试过此操作,但是清除区域的颜色与面板背景颜色不太相同。 Can anyone please suggest how to set the brush colour to the panel background colour? 有人可以建议如何将画笔颜色设置为面板背景颜色吗?

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()

I have a work-around for this 我有一个解决方法

I have placed the two lines 我已经放置了两行

    dc = wx.ClientDC(self)
    panel.SetBackgroundColour(dc.GetBackground().Colour)

in the init method of the MainFrame class. 在MainFrame类的init方法中。 It works but it is unsatisfactory because the panel colour is no longer that defined by the theme (in my case (225, 225, 225) instead of (212, 212, 212)). 它可以工作,但是不能令人满意,因为面板颜色不再是主题定义的颜色(在我的情况下是(225,225,225)而不是(212,212,212))。

This might not seem significant, but 这似乎并不重要,但是

  1. it detracts from the unity of applications using the theme and that seems very untidy; 它损害了使用主题的应用程序的统一性,这似乎很不整洁;
  2. the colour selected is outside my control. 选择的颜色超出了我的控制范围。 I wonder where this arbitrary colour comes from? 我想知道这种任意颜色来自何处?
  3. The original works perfectly under Windows but not Ubuntu. 原始版本可以在Windows下完美运行,但不能在Ubuntu下运行。 So it means having OS dependent code. 因此,这意味着具有依赖于操作系统的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM