wxPython - 无法隐藏桌面屏幕截图的wxFrame

时间:2016-05-10 18:50:23

标签: python wxpython

我是wxPython的新手,并且正在使用桌面截图工具。简而言之,用户在屏幕上选择矩形区域并从中生成图像,就像大多数屏幕截图工具一样。它的全部工作除了我无法获得主框架及其所有内容,隐藏或关闭,因此生成的屏幕截图图像包含覆盖蒙版和虚线选择框。

仅供参考,按ENTER键可触发屏幕截图。我已经尝试了很多方法来实现这一点而没有运气。隐藏框架,或先破坏面板等没有区别。但是,如果我用按钮将事情分解为两步以继续进行,那么一切正常,所以我认为它很小。我也在框架和面板上尝试了Refresh。对于它的价值,我的桌面环境如果是Linux KDE(最新的ArchLinux滚动版)。

这是我的完整代码。

#!/usr/bin/env python2

import wx, time

#--------------------------------------------------------------------------------------------------------#

class SelectableFrame(wx.Frame):
    c1 = None
    c2 = None

    def __init__(self, parent=None, id=-1, title=""):
        wx.Frame.__init__(self, parent, id, title, pos=(0, 0), size=wx.DisplaySize(), style=wx.FRAME_NO_TASKBAR | wx.NO_BORDER | wx.STAY_ON_TOP)

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

        self.panel.Bind(wx.EVT_MOTION, self.OnMouseMove)
        self.panel.Bind(wx.EVT_LEFT_DOWN, self.OnMouseSelect)
        self.panel.Bind(wx.EVT_RIGHT_DOWN, self.OnReset)
        self.panel.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
        self.panel.Bind(wx.EVT_PAINT, self.OnPaint)
        self.panel.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

        self.Bind(wx.EVT_CLOSE, self.OnClose)

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

        self.panel.SetBackgroundColour(wx.Colour(0, 0, 0))
        self.panel.SetBackgroundStyle(wx.BG_STYLE_COLOUR)

        self.SetTransparent(150)

    def OnClose(self, event):
        self.Destroy()

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

    def OnMouseSelect(self, event):
        self.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))
        self.c1 = event.GetPosition()

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

    def OnKeyDown(self, event):
        key = event.GetKeyCode()
        if key == wx.WXK_ESCAPE:
            self.Close()
        elif (key == wx.WXK_RETURN or key == wx.WXK_NUMPAD_ENTER) and self.RegionSelected():
            self.TakeScreenshot()
        event.Skip()

    def OnReset(self, event=None):
        wx.PaintDC(self.panel).Clear()
        self.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))

    def OnPaint(self, event):
        if not self.RegionSelected():
            return
        dc = wx.PaintDC(self.panel)
        dc.SetPen(wx.Pen("white", 1, wx.LONG_DASH))
        dc.SetBrush(wx.Brush(wx.Colour(100, 100, 100), wx.SOLID))

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

    def RegionSelected(self):
        if self.c1 is None or self.c2 is None:
            return False
        else:
            return True

    def TakeScreenshot(self):
        if not self.RegionSelected():
            return
        wx.PaintDC(self.panel).Clear()
        self.Hide()
        self.Refresh()
        dcScreen = wx.ScreenDC()
        bmp = wx.EmptyBitmap(self.c2.x - self.c1.x, self.c2.y - self.c1.y)
        memDC = wx.MemoryDC()
        memDC.SelectObject(bmp)
        memDC.Blit(0, 0, self.c2.x - self.c1.x, self.c2.y - self.c1.y, dcScreen, self.c1.x, self.c1.y)
        memDC.SelectObject(wx.NullBitmap)
        img = bmp.ConvertToImage()
        filename = "/tmp/ocr-screenshot-" + time.strftime('%Y%m%d-%H%M%S') + ".png"
        img.SaveFile(filename, wx.BITMAP_TYPE_PNG)
        self.Close()

#--------------------------------------------------------------------------------------------------------

class SelectableApp(wx.App):
    def OnInit(self):
        self.frame = SelectableFrame()
        self.frame.Show(True)
        self.SetTopWindow(self.frame)
        return True

#--------------------------------------------------------------------------------------------------------#

app = SelectableApp(False)
app.MainLoop()

0 个答案:

没有答案