在屏幕上绘制 Python [英] Python drawing on screen

查看:50
本文介绍了在屏幕上绘制 Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个需要选择屏幕区域的应用程序.我需要将光标更改为十字,然后在用户选择上绘制一个矩形.我搜索的第一件事是如何操作光标,我遇到了 wxPython.使用 wxPython 我可以轻松地在带有面板的框架上执行此操作,问题是我需要窗口是透明的,以便用户在选择所需区域时可以看到他的屏幕,但是如果我制作框架和面板对象透明,一切都会变得有问题.

I'm coding an application that needs to select an area of the screen. I need to change the cursor to a cross and then draw a rectangle on the user selection. The first thing I searched for is how to manipulate the cursor and I came across wxPython. With wxPython I could easily do this on a Frame with a Panel, the thing is that I'd need the window to be transparent so the user can see his screen while is selecting the desired area, but if I make the Frame and the Panel objects transparent everything gets buggy.

所以,我对任何解决方案都持开放态度,无论是使用 wxPython 还是不使用它,因为我真的不知道我是否正确使用它.

So, I'm open to any solution, either using wxPython or not using it because I don't really know if I'm using it right.

我是 Python 新手,我的母语不是英语,所以如果您看不懂,我很抱歉.

I'm new to Python and I'm not a native english speaker, so I'm sorry if you can't understand something.

这是我写的

import wx

class SelectableFrame(wx.Frame):

    c1 = None
    c2 = None

    def __init__(self, parent=None, id=-1, title=""):
        wx.Frame.__init__(self, parent, id, title, size=wx.DisplaySize(), style=wx.TRANSPARENT_WINDOW)

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

        self.panel.Bind(wx.EVT_MOTION, self.OnMouseMove)
        self.panel.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
        self.panel.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
        self.panel.Bind(wx.EVT_PAINT, self.OnPaint)

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

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

    def OnMouseDown(self, event):
        self.c1 = event.GetPosition()

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

    def OnPaint(self, event):
        if self.c1 is None or self.c2 is None: return

        dc = wx.PaintDC(self.panel)
        dc.SetPen(wx.Pen('red', 1))
        dc.SetBrush(wx.Brush(wx.Color(0, 0, 0), wx.TRANSPARENT))

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

    def PrintPosition(self, pos):
        return str(pos.x) + " " + str(pos.y)


class MyApp(wx.App):

    def OnInit(self):
        frame = SelectableFrame()
        frame.Show(True)
        self.SetTopWindow(frame)

        return True



app = MyApp(0)
app.MainLoop()

推荐答案

你不应该在窗口创建中使用 wx.TRANSPARENT,它主要用于 wxDC 绘制命令.要使窗口透明,只需调用 win.SetTransparent(amount),其中数量是 0-255,255 表示不透明,0 表示完全透明.见 http://www.wxpython.org/docs/api/wx.Window-class.html#SetTransparent

You shouldn't be using wx.TRANSPARENT in window creation, that is mostly used for wxDC paint commands. To make a window transparent just call win.SetTransparent(amount), where amount is from 0-255, 255 means opaque, 0 means totally transparent. see http://www.wxpython.org/docs/api/wx.Window-class.html#SetTransparent

我已经修改了你的代码,它只有在你的平台支持透明窗口时才有效,你可以通过 CanSetTransparent 进行检查.我在 Windows XP 上测试过.

I have modified your code, it will work only if your platform supports transparent windows, you can check that by CanSetTransparent. I tested it on windows XP.

import wx

class SelectableFrame(wx.Frame):

    c1 = None
    c2 = None

    def __init__(self, parent=None, id=-1, title=""):
        wx.Frame.__init__(self, parent, id, title, size=wx.DisplaySize())

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

        self.panel.Bind(wx.EVT_MOTION, self.OnMouseMove)
        self.panel.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
        self.panel.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
        self.panel.Bind(wx.EVT_PAINT, self.OnPaint)

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

        self.SetTransparent(50)

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

    def OnMouseDown(self, event):
        self.c1 = event.GetPosition()

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

    def OnPaint(self, event):
        if self.c1 is None or self.c2 is None: return

        dc = wx.PaintDC(self.panel)
        dc.SetPen(wx.Pen('red', 1))
        dc.SetBrush(wx.Brush(wx.Color(0, 0, 0), wx.TRANSPARENT))

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

    def PrintPosition(self, pos):
        return str(pos.x) + " " + str(pos.y)


class MyApp(wx.App):

    def OnInit(self):
        frame = SelectableFrame()
        frame.Show(True)
        self.SetTopWindow(frame)

        return True


app = MyApp(0)
app.MainLoop()

这篇关于在屏幕上绘制 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆