Doing mouse-overs
Process the EVT_ENTER_WINDOW and EVT_LEAVE_WINDOW events.
Example
Originator of this item: please feel free to delete this example once you can supply the one that you want. - Bill Bell
This script is an extension of CreatingCollectionOfControls, you might wish to read that one first. This example shows how to use EVT_ENTER_WINDOW and EVT_LEAVE_WINDOW so that when the user's mouse is over a button in the collection a message is displayed in the status bar, and when the mouse is moved away the message is removed.
1 from wxPython . wx import *
2
3 class ButtonColumn ( wxPanel ):
4 '''Create aligned column of equal-sized buttons with distinct labels and OnClick destinations.
5
6 Illustrates use of nested classes for creating a collection of controls.
7 Also illustrates use of mouse over and mouse leave events.
8 '''
9 class aButton ( wxButton ):
10 '''Nested button class for use by ButtonColumn class.'''
11 def __init__ ( self, parent, label, whotocall, whotonotify, msg ):
12 """Expects reference to 'ButtonColumn', list of labels for buttons and list of functions to be called for OnClick events"""
13 id = wxNewId ( )
14 wxButton . __init__ ( self, parent, id, label, wxDefaultPosition, wxDefaultSize, 1 )
15 self . whotocall = whotocall
16 self . whotonotify = whotonotify
17 self . msg = msg
18 EVT_BUTTON ( self, id, self . OnClick )
19 EVT_ENTER_WINDOW ( self, self . OnEnter )
20 EVT_LEAVE_WINDOW ( self, self . OnLeave )
21
22 def OnClick ( self, event ):
23 if self . whotocall: self . whotocall ( )
24
25 def OnEnter ( self, event ) :
26 if self . whotonotify : self . whotonotify ( 1, self . msg )
27
28 def OnLeave ( self, event ) :
29 if self . whotonotify : self . whotonotify ( 0, self . msg )
30
31 def __init__ ( self, parent, width, buttons, Bottom = 0 ):
32 """Expects reference to 'parent' of 'ButtonColumn', button column 'width', list of button descriptor tuples, and number of buttons to be displayed at the bottom of the column.
33
34 Each button descriptor consists of a label for the button and a reference to the function to be called when the button is clicked.
35 """
36 wxPanel . __init__ ( self, parent, -1, wxDefaultPosition, ( 100, 200 ) )
37 self . parent = parent
38
39 """Create the upper collection of buttons"""
40 previous = None
41 for button in buttons [ 0 : len ( buttons ) -Bottom ]:
42 oneButton = self . aButton ( self, button [ 0 ], button [ 1 ], button [ 2 ], button [ 3 ] )
43 lc = wxLayoutConstraints ( )
44 lc . left . SameAs ( self, wxLeft, 5 )
45 lc . right . SameAs ( self, wxRight, 5 )
46 lc . height . AsIs ( )
47 if previous: lc . top . SameAs ( previous, wxBottom, 5 )
48 else: lc . top . SameAs ( self, wxTop, 5 )
49 oneButton . SetConstraints ( lc )
50 previous = oneButton
51
52 """Create the lower collection of buttons"""
53 buttons . reverse ( )
54 previous = None
55 for button in buttons [ 0 : Bottom ]:
56 oneButton = self . aButton ( self, button [ 0 ], button [ 1 ], button [ 2 ], button [ 3 ] )
57 lc = wxLayoutConstraints ( )
58 lc . left . SameAs ( self, wxLeft, 5 )
59 lc . right . SameAs ( self, wxRight, 5 )
60 lc . height . AsIs ( )
61 if previous: lc . bottom . SameAs ( previous, wxTop, 5 )
62 else: lc . bottom . SameAs ( self, wxBottom, 5 )
63 oneButton . SetConstraints ( lc )
64 previous = oneButton
65
66 if __name__ == '__main__':
67 class TestFrame(wxFrame):
68 def __init__(self):
69 wxFrame . __init__ (
70 self, None, -1, "Button Column Test",
71 size = ( 450, 300 ),
72 style = wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE
73 )
74 self . SetAutoLayout ( true )
75 buttons = [
76 ( 'OK', self . OKClicked, self . OnMessage, 'OK button text', ),
77 ( 'Cancel', self . CancelClicked, self . OnMessage, 'Cancel button text', ),
78 ( 'Re-invert', self . ReinvertClicked, self . OnMessage, 'Re-invert button text', ),
79 ( 'Exit', self . ExitClicked, self . OnMessage, 'Exit button text', ),
80 ]
81
82 self . tp = ButtonColumn ( self, 45, buttons, 2 )
83
84 lc = wxLayoutConstraints ( )
85 lc . right . SameAs ( self, wxRight)
86 lc . width . AsIs ( )
87 lc . top . SameAs ( self, wxTop )
88 lc . bottom . SameAs ( self, wxBottom )
89 self . tp . SetConstraints ( lc )
90
91 self . CreateStatusBar ( )
92
93 def OnMessage ( self, on, msg ) :
94 if not on : msg = ""
95 self . SetStatusText ( msg )
96
97 def OKClicked ( self ):
98 print "OKClicked"
99
100 def CancelClicked ( self ):
101 print "CancelClicked"
102
103 def ReinvertClicked ( self ):
104 print "ReInvertClicked"
105
106 def ExitClicked ( self ):
107 print "ExitClicked"
108 self . Close ( )
109
110 app = wxPySimpleApp()
111 frame = TestFrame()
112 frame . Show(true)
113 app . MainLoop()