Hello everyone,
I am writing a user interface for a behavioural experiment. I need
that when the players mouse over a certain area of the screen, another
area of the screen gets highlighted.
I have solved the problem in the past by creating a
wx.EVT_ENTER_WINDOW, binding it to the correct widget; this event
calls a method setting to bold and bigger font all the elements of the
area of the screen I need to highlight. This works, but it is not so
nice to see and not so intuitive for the subjects.
It would be more intuitive that, when the user mouses over the widget,
a red rectangle gets drawn upon the needed area, to be deleted when
the user mouses away from the trigger widget.
I tried to use wx.PaintDC, following online tutorials. But I have
problems: in all examples, the EVT_PAINT has been bounded to the
panel. I need a sort of 'second order bounding': I need in a way to
bound a wx.EVT_ENTER_WINDOW to the trigger widget, and I need this
event to trigger a EVT_PAINT. How do I do this?
Do I need nested events, i.e. events bounded to events? In other
words: how can one event (mousing over a widget) generate a chain of
events, first a evt_enter_window, and then this in turn generating a
evt_paint?
Here follows a test app I have written. It DOES NOT work on windows
(because all PaintDC outside of a EVT_PAINT does not work) and it
works in a peculiar way in Linux: when mousing over, the rectangle is
drawn; but when mousing away, all the frame becomes white.
Any suggestion on how to make a mouseover event trigger a paint event?
Thanks!
Paolo
···
-----------------------------------------
import wx
class MyFrame(wx.Frame):
def __init__(self, parent=None, id=-1, title=None):
wx.Frame.__init__(self, parent, id, title)
self.panel = wx.Panel(self, size=(550, 300))
self.button1 = wx.Button(self.panel, -1, 'Mouse Here',
size=(80,30))
self.button1.Bind(wx.EVT_ENTER_WINDOW, self.on_paint)
self.button1.Bind(wx.EVT_LEAVE_WINDOW, self.on_clearpaint)
self.button1.SetPosition((10,10))
self.Fit()
def on_paint(self, event):
dc = wx.PaintDC(self.panel)
dc.SetPen(wx.Pen('red', 4))
rect = wx.Rect(100, 10, 200, 200)
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.DrawRoundedRectangleRect(rect, 8)
def on_clearpaint(self, event):
dc = wx.PaintDC(self.panel)
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.Clear()
app = wx.PySimpleApp()
frame1 = MyFrame(title='rounded-rectangle & circle')
frame1.Center()
frame1.Show()
app.MainLoop()