1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
<!DOCTYPE HTML PUBLIC "-//Netscape_Microsoft//DTD HTML 3.0//EN">
<HTML>
<!-- This file generated using the Python HTMLgen module. -->
<HEAD>
<META NAME="GENERATOR" CONTENT="HTMLgen 1.1">
<TITLE>PmwBalloon.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
<FONT COLOR="#DD0000"># Display a message in a window when the mouse enters a widget and</FONT>
<FONT COLOR="#DD0000"># remains there for a set time (default 1 second). The window closes</FONT>
<FONT COLOR="#DD0000"># when the mouse leaves the widget, or any buttons are pressed while</FONT>
<FONT COLOR="#DD0000"># over the widget.</FONT>
# This class has one method <FONT COLOR="#009900">'bind'</FONT>, which takes a widget and a message
<FONT COLOR="#DD0000"># to display for that widget.</FONT>
<FONT COLOR="#DD0000"># TO DO:</FONT>
<FONT COLOR="#DD0000"># If the window appears off the screen, put it somewhere else!</FONT>
import os
import regsub
import Tkinter
import Pmw
<STRONG><FONT COLOR="#CC6600">class Balloon</FONT></STRONG>(Pmw.MegaToplevel):
<STRONG> def __init__</STRONG>(self, parent=None, **kw):
<FONT COLOR="#DD0000"># Define the megawidget options.</FONT>
optiondefs = (
(<FONT COLOR="#009900">'initwait'</FONT>, 500, None), # milliseconds
(<FONT COLOR="#009900">'label_background'</FONT>, <FONT COLOR="#009900">'lightyellow'</FONT>, None),
(<FONT COLOR="#009900">'label_justify'</FONT>, <FONT COLOR="#009900">'left'</FONT>, None),
(<FONT COLOR="#009900">'state'</FONT>, <FONT COLOR="#009900">'both'</FONT>, None),
(<FONT COLOR="#009900">'statuscommand'</FONT>, None, None),
(<FONT COLOR="#009900">'xoffset'</FONT>, 20, None), # pixels
(<FONT COLOR="#009900">'yoffset'</FONT>, 1, None), # pixels
)
self.defineoptions(kw, optiondefs)
<FONT COLOR="#DD0000"># Initialise the base class (after defining the options).</FONT>
Pmw.MegaToplevel.__init__(self, parent)
self.withdraw()
self.overrideredirect(1)
self.configure(hull_borderwidth=1, hull_background="black")
<FONT COLOR="#DD0000"># Create the components.</FONT>
interior = self.interior()
self._label = self.createcomponent(<FONT COLOR="#009900">'label'</FONT>,
(), None,
Tkinter.Label, (interior,))
self._label.pack()
<FONT COLOR="#DD0000"># Initialise instance variables.</FONT>
self._timer = None
<FONT COLOR="#DD0000"># Check keywords and initialise options.</FONT>
self.initialiseoptions(Balloon)
<STRONG> def destroy</STRONG>(self):
if self._timer is not None:
self.after_cancel(self._timer)
self._timer = None
Pmw.MegaToplevel.destroy(self)
<STRONG> def bind</STRONG>(self, widget, balloonHelp, statusHelp = None):
if balloonHelp is None and statusHelp is None:
self.unbind(widget)
return
if statusHelp is None:
statusHelp = balloonHelp
statusHelp = regsub.gsub(<FONT COLOR="#009900">'\n'</FONT>, <FONT COLOR="#009900">' '</FONT>, statusHelp)
widget.bind(<FONT COLOR="#009900">'<Enter>'</FONT>,
lambda event=None, self=self, w=widget,
sHelp=statusHelp, bHelp=balloonHelp:
self._enter(w, sHelp, bHelp, None))
<FONT COLOR="#DD0000"># Note: The Motion binding only works for basic widgets,</FONT>
<FONT COLOR="#DD0000"># not megawidgets.</FONT>
widget.bind(<FONT COLOR="#009900">'<Motion>'</FONT>,
lambda event=None, self=self, statusHelp=statusHelp:
self.showstatus(statusHelp))
widget.bind(<FONT COLOR="#009900">'<Leave>'</FONT>, self._leave)
widget.bind(<FONT COLOR="#009900">'<ButtonPress>'</FONT>, self._buttonpress)
<STRONG> def unbind</STRONG>(self, widget):
widget.unbind(<FONT COLOR="#009900">'<Motion>'</FONT>)
widget.unbind(<FONT COLOR="#009900">'<Enter>'</FONT>)
widget.unbind(<FONT COLOR="#009900">'<Leave>'</FONT>)
widget.unbind(<FONT COLOR="#009900">'<ButtonPress>'</FONT>)
<STRONG> def canvasbind</STRONG>(self, widget, item, balloonHelp, statusHelp = None):
if balloonHelp is None and statusHelp is None:
self.canvasunbind(widget)
return
if statusHelp is None:
statusHelp = balloonHelp
statusHelp = regsub.gsub(<FONT COLOR="#009900">'\n'</FONT>, <FONT COLOR="#009900">' '</FONT>, statusHelp)
widget.tag_bind(item, <FONT COLOR="#009900">'<Enter>'</FONT>,
lambda event=None, self=self, w=widget, item=item,
sHelp=statusHelp, bHelp=balloonHelp:
self._enter(w, sHelp, bHelp, item))
widget.tag_bind(item, <FONT COLOR="#009900">'<Motion>'</FONT>,
lambda event=None, self=self, statusHelp=statusHelp:
self.showstatus(statusHelp))
widget.tag_bind(item, <FONT COLOR="#009900">'<Leave>'</FONT>, self._leave)
widget.tag_bind(item, <FONT COLOR="#009900">'<ButtonPress>'</FONT>, self._buttonpress)
<STRONG> def canvasunbind</STRONG>(self, widget, item):
widget.tag_unbind(item, <FONT COLOR="#009900">'<Motion>'</FONT>)
widget.tag_unbind(item, <FONT COLOR="#009900">'<Enter>'</FONT>)
widget.tag_unbind(item, <FONT COLOR="#009900">'<Leave>'</FONT>)
widget.tag_unbind(item, <FONT COLOR="#009900">'<ButtonPress>'</FONT>)
<STRONG> def showstatus</STRONG>(self, statusHelp):
if self[<FONT COLOR="#009900">'state'</FONT>] in (<FONT COLOR="#009900">'status'</FONT>, <FONT COLOR="#009900">'both'</FONT>):
cmd = self[<FONT COLOR="#009900">'statuscommand'</FONT>]
if callable(cmd):
cmd(statusHelp)
<STRONG> def clearstatus</STRONG>(self):
if self[<FONT COLOR="#009900">'state'</FONT>] in (<FONT COLOR="#009900">'status'</FONT>, <FONT COLOR="#009900">'both'</FONT>):
cmd = self[<FONT COLOR="#009900">'statuscommand'</FONT>]
if callable(cmd):
cmd(None)
<STRONG> def _enter</STRONG>(self, widget, statusHelp, balloonHelp, item):
if self[<FONT COLOR="#009900">'state'</FONT>] in (<FONT COLOR="#009900">'balloon'</FONT>, <FONT COLOR="#009900">'both'</FONT>):
if self._timer is not None:
self.after_cancel(self._timer)
self._timer = None
self._timer = self.after(self[<FONT COLOR="#009900">'initwait'</FONT>],
lambda self=self, widget=widget, help=balloonHelp,
item=item:
self._showBalloon(widget, help, item))
self.showstatus(statusHelp)
<STRONG> def _leave</STRONG>(self, event):
if self._timer is not None:
self.after_cancel(self._timer)
self._timer = None
self.withdraw()
self.clearstatus()
<STRONG> def _buttonpress</STRONG>(self, event):
if self._timer is not None:
self.after_cancel(self._timer)
self._timer = None
self.withdraw()
<STRONG> def _showBalloon</STRONG>(self, widget, balloonHelp, item = None):
if item is None:
x = widget.winfo_rootx()
y = widget.winfo_rooty() + widget.winfo_height()
else:
<FONT COLOR="#DD0000"># The widget is a canvas. Place balloon under canvas item.</FONT>
bbox = widget.bbox(item)
x = widget.winfo_rootx() + bbox[0]
y = widget.winfo_rooty() + bbox[3]
x = x + self[<FONT COLOR="#009900">'xoffset'</FONT>]
y = y + self[<FONT COLOR="#009900">'yoffset'</FONT>]
self._label.configure(text=balloonHelp)
<FONT COLOR="#DD0000"># To avoid flashes on X and to position the window</FONT>
<FONT COLOR="#DD0000"># correctly on Win95 (caused by Tk bugs):</FONT>
if os.name != "nt":
self.geometry(<FONT COLOR="#009900">'%+d%+d'</FONT> % (x, y))
self.deiconify()
self.tkraise()
if os.name == "nt":
self.geometry(<FONT COLOR="#009900">'%+d%+d'</FONT> % (x, y))
</PRE>
</BODY> </HTML>
|