Wxpython 修复windows上所有黑色wx光标
下面的代码演示了Windows上某些wx游标的问题(OSX游标具有白色轮廓)。。。也就是说,它们都是黑色的,因此在黑色背景上完全看不见Wxpython 修复windows上所有黑色wx光标,wxpython,cursor,matplotlib,Wxpython,Cursor,Matplotlib,下面的代码演示了Windows上某些wx游标的问题(OSX游标具有白色轮廓)。。。也就是说,它们都是黑色的,因此在黑色背景上完全看不见 import wx app = wx.PySimpleApp() f = wx.Frame(None) f.SetBackgroundColour(wx.Color(0)) f.SetCursor(wx.StockCursor(wx.CURSOR_CROSS)) f.Show() app.MainLoop() 我想知道是否有人找到了修补windows图标的方法
import wx
app = wx.PySimpleApp()
f = wx.Frame(None)
f.SetBackgroundColour(wx.Color(0))
f.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))
f.Show()
app.MainLoop()
我想知道是否有人找到了修补windows图标的方法,或者是否有我不知道的修复方法
我遇到的更具体的问题是matplotlibs的wx后端使用wx.CURSOR_十字来缩放imshow绘图,我使用它来显示大部分为黑色的图像。我还没有找到一种方法来定制mpl选择的游标,但我想我会在挖掘时提出这个问题
谢谢,
亚当
注意:使用wxPython版本2.8.10.1和matplotlib版本0.99和1.0
进度:
我似乎至少可以通过以下操作来创建自己的光标,但我非常沮丧地发现,我无法在任何地方包含白色。关于这一点的文件是可怕的
import numpy as np
buf = np.ones((16,16,3), dtype='uint8') * 127 # pixels untouched by the following operations will outline the crosshair shape (wish they could be white)
buf[7,:,:] = 0 # horizontal black line
buf[:,7,:] = 0 # vertical black line
buf[:6,:6, :] = 255 # evidently values > 127 are interpreted as alpha
buf[9:,:6, :] = 255
buf[9:, 9:, :] = 255
buf[:6, 9:, :] = 255
im = wx.ImageFromBuffer(16, 16, buf.tostring()) # passing a separate alpha buffer just gets ignored
im.SetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_X, 7)
im.SetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_Y, 7)
cursor = wx.CursorFromImage(im)
嗯,这是我想到的。处理鼠标进入和鼠标离开事件不够好,因为matplotlib导航工具栏将在事件发生后设置光标,因此我们需要将其设置为鼠标运动 我无法得到十字线图标周围的白色轮廓,但我现在已经足够接近了。就是这样
# Bind mouse motion events in the canvas
self.figure.canvas.mpl_connect('motion_notify_event', self.on_mouse_move)
# ...
def on_mouse_move(self, evt):
#
# LAAAME SAUCE -- Crosshair cursor is all black on Windows making it
# virtually invisible on dark images. Use custom cursor instead.
#
if (sys.platform.lower().startswith('win') and
evt.inaxes and
self.navtoolbar.mode == 'Zoom to rect mode'): # NOTE: There are no constants for the navbar modes
#
# Build the crosshair cursor image as a numpy array.
# Sadly I can't figure out how to make a white outline since every
# value above 127 is apparently transparent.
# Soooo the outline is yellow.
#
# Best docs I could find: http://wxruby.rubyforge.org/doc/cursor.html
#
buf = np.ones((16,16,3), dtype='uint8') * 255
buf[:,:,2] = 1
buf[7,1:-1,:] = buf[1:-1,7,:] = 0
buf[:6,:6,:] = buf[9:,:6,:] = buf[9:,9:,:] = buf[:6,9:,:] = 255
#
# NOTE: I tried making an alpha channel and doing
# wx.ImageFromBuffer(16, 16, buf.tostring(), alpha_buffer.to_string())
# ...no good. wx just ignores the channel.
#
im = wx.ImageFromBuffer(16, 16, buf.tostring())
im.SetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_X, 7)
im.SetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_Y, 7)
cursor = wx.CursorFromImage(im)
self.figure.canvas.SetCursor(cursor)
我也在做同样的事情,我成功地创建了一个自定义导航工具栏,并重定向了set_游标回调
class CustomNavigationToolbar(NavigationToolbar2WxAgg):
"""
Extend the default wx toolbar with your own event handlers
"""
ON_CUSTOM = wx.NewId()
def __init__(self, canvas):
NavigationToolbar2WxAgg.__init__(self, canvas)
self.set_cursor = self.customSetCursor
def customSetCursor(self, event):
self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
我这里也有同样的问题,如果我有一个黑色的图像,点击“缩放”按钮后,光标变成黑色,我看不到鼠标的位置 我只是看了一下文件:
\matplotlib\backends\backend_wx.py
围绕第1593行(我使用的是matplotlib 1.3.1),并更改一些代码,如下所示:
cursord = {
cursors.MOVE : wx.CURSOR_HAND,
cursors.HAND : wx.CURSOR_HAND,
cursors.POINTER : wx.CURSOR_ARROW,
cursors.SELECT_REGION : wx.CURSOR_ARROW, #wx.CURSOR_CROSS, change here!!!
}
这应该可以解决问题