【问题标题】:Making "Snipping Tool" from Python using Wx and PyAutoGui使用 Wx 和 PyAutoGui 从 Python 制作“截图工具”
【发布时间】:2019-12-24 09:21:37
【问题描述】:

我正在尝试使用 Wx 和 PyAutoGui 模块制作一个截图工具,我遇到了一个问题:保存的图像文件位置错误(见下图)

Picture Link

如您所见,我正在尝试抓取特定区域,即红色矩形,并将该区域中的像素保存到文件“my_screenshot.png”中。但是,位置/坐标似乎已关闭(您可以看到矩形,它应该是屏幕截图的区域)

代码如下:

import wx
import pyautogui

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.Cursor(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.Cursor(wx.CURSOR_ARROW))
        region = (self.c1.x, self.c1.y, self.c2.x - self.c1.x, self.c2.y - self.c1.y)
        pyautogui.screenshot('my_screenshot.png', region=region)
        print("MouseUp: " + str(region))
        self.Hide()

    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.Colour(0, 0, 0), wx.TRANSPARENT))

        region = (self.c1.x, self.c1.y, self.c2.x - self.c1.x, self.c2.y - self.c1.y)
        dc.DrawRectangle(self.c1.x, self.c1.y, self.c2.x - self.c1.x, self.c2.y - self.c1.y)
        print("Draw: " + str(region))


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

根据我收集到的信息,它占用了宽度和高度,但 x 和 y 位置错误,我该如何解决?谢谢

编辑:这些函数之间似乎存在值差异

    def OnMouseDown(self, event):
        self.c1 = event.GetPosition()
        print("MouseDown[event]: " + str(self.c1))
        print("MouseDown[gui]: "+  str(pyautogui.position()))

输出:

MouseDown[event]: (729, 484)
MouseDown[gui]: Point(x=737, y=515)

x 的偏移量为 +8,y 的偏移量为 +31。这种尿失禁是怎么发生的?我的修补程序是将这些偏移量添加到 pyautogui.screenshot 命令中,但我认为这不是正确的修复程序,并且不能保证与其他屏幕尺寸相同的偏移量值..

【问题讨论】:

    标签: python python-3.x wxwidgets pyautogui


    【解决方案1】:

    wxpython MouseEvent doc 告诉

    与鼠标事件关联的位置在 窗口中表示 产生事件的窗口的坐标,你可以使用 wx.Window.ClientToScreen 将其转换为屏幕坐标和 可能调用wx.Window.ScreenToClient next 将其转换为窗口 另一个窗口的坐标。

    pyautogui' screenshot() 适用于屏幕坐标

    所以,将wx.Window.ClientToScreenc1c2 一起使用。

    顺便说一句,您也应该在OnMouseUp 更新c2,并检查它不是“无”或等于c1

    【讨论】:

      猜你喜欢
      • 2016-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多