我正在尝试使用Wx和PyAutoGui模块制作一个剪贴工具,但我遇到了一个问题:保存的图像文件位于错误的位置(见下图)
如你所见,我正试图获取一个特定区域,即红色矩形,并将该区域中的像素保存到文件“my”中_截图.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文件命令,但我不认为这是正确的修复方法,也不能保证其他屏幕大小的偏移值相同。。你知道吗
wxpython MouseEvent doc告诉我们
pyautogui
screenshot()
使用屏幕坐标。你知道吗所以,将
wx.Window.ClientToScreen
与c1
和c2
一起使用。你知道吗顺便说一句,您应该在
OnMouseUp
更新c2
,并检查它不是'None'或等于c1
。你知道吗相关问题 更多 >
编程相关推荐