1title = 'Pmw.Balloon demonstration'
2
3# Import Pmw from this directory tree.
4import sys
5sys.path[:0] = ['../../..']
6
7import Tkinter
8import Pmw
9
10class Demo:
11    def __init__(self, parent):
12	# Create the Balloon.
13	self.balloon = Pmw.Balloon(parent)
14
15	# Create some widgets and megawidgets with balloon help.
16	frame = Tkinter.Frame(parent)
17	frame.pack(padx = 10, pady = 5)
18	field = Pmw.EntryField(frame,
19		labelpos = 'nw',
20		label_text = 'Command:')
21	field.setentry('mycommand -name foo')
22	field.pack(side = 'left', padx = 10)
23	self.balloon.bind(field, 'Command to\nstart/stop',
24		'Enter the shell command to control')
25
26	start = Tkinter.Button(frame, text='Start')
27	start.pack(side='left', padx = 10)
28	self.balloon.bind(start, 'Start the command')
29
30	stop = Tkinter.Button(frame, text='Stop')
31	stop.pack(side='left', padx = 10)
32	self.balloon.bind(stop, 'Stop the command')
33
34	self.suicide = Tkinter.Button(frame, text='Kill me soon!',
35            command = self.killButton)
36	self.suicide.pack(side='left', padx = 10)
37	self.balloon.bind(self.suicide, 'Watch this button disappear!')
38
39	scrolledCanvas = Pmw.ScrolledCanvas(parent,
40		canvas_width = 300,
41		canvas_height = 115,
42	)
43	scrolledCanvas.pack()
44        canvas = scrolledCanvas.component('canvas')
45        self.canvas = canvas
46
47	# Create some canvas items and individual help.
48	item = canvas.create_arc(5, 5, 35, 35, fill = 'red', extent = 315)
49	self.balloon.tagbind(canvas, item, 'This is help for\nan arc item')
50	item = canvas.create_bitmap(20, 150, bitmap = 'question')
51	self.balloon.tagbind(canvas, item, 'This is help for\na bitmap')
52	item = canvas.create_line(50, 60, 70, 80, 85, 20, width = 5)
53	self.balloon.tagbind(canvas, item, 'This is help for\na line item')
54	item = canvas.create_text(10, 90, text = 'Canvas items with balloons',
55                anchor = 'nw', font = field.cget('entry_font'))
56	self.balloon.tagbind(canvas, item, 'This is help for\na text item')
57
58	# Create two canvas items which have the same tag and which use
59	# the same help.
60	canvas.create_rectangle(100, 10, 170, 50, fill = 'aliceblue',
61		tags = 'TAG1')
62	self.bluecircle = canvas.create_oval(110, 30, 160, 80, fill = 'blue',
63		tags = 'TAG1')
64	self.balloon.tagbind(canvas, 'TAG1',
65		'This is help for the two blue items' + '\n' * 10 +
66                    'It is very, very big.',
67                'This is help for the two blue items')
68	item = canvas.create_text(180, 10, text = 'Delete',
69                anchor = 'nw', font = field.cget('entry_font'))
70	self.balloon.tagbind(canvas, item,
71                'After 2 seconds,\ndelete the blue circle')
72	canvas.tag_bind(item, '<ButtonPress>', self._canvasButtonpress)
73	scrolledCanvas.resizescrollregion()
74
75	scrolledText = Pmw.ScrolledText(parent,
76		text_width = 32,
77		text_height = 4,
78                text_wrap = 'none',
79        )
80	scrolledText.pack(pady = 5)
81        text = scrolledText.component('text')
82        self.text = text
83
84	text.insert('end',
85		'This is a text widget with ', '',
86		' balloon', 'TAG1',
87		'\nhelp. Find the ', '',
88		' text ', 'TAG1',
89		' tagged with', '',
90		' help.', 'TAG2',
91		'\n', '',
92                'Remove tag 1.', 'TAG3',
93                '\nAnother line.\nAnd another', '',
94	)
95	text.tag_configure('TAG1', borderwidth = 2, relief = 'sunken')
96	text.tag_configure('TAG3', borderwidth = 2, relief = 'raised')
97
98	self.balloon.tagbind(text, 'TAG1',
99		'There is one secret\nballoon help.\nCan you find it?')
100	self.balloon.tagbind(text, 'TAG2',
101		'Well done!\nYou found it!')
102	self.balloon.tagbind(text, 'TAG3',
103		'After 2 seconds\ndelete the tag')
104	text.tag_bind('TAG3', '<ButtonPress>', self._textButtonpress)
105
106	frame = Tkinter.Frame(parent)
107	frame.pack(padx = 10)
108	self.toggleBalloonVar = Tkinter.IntVar()
109	self.toggleBalloonVar.set(1)
110	toggle = Tkinter.Checkbutton(frame,
111		variable = self.toggleBalloonVar,
112		text = 'Balloon help', command = self.toggle)
113	toggle.pack(side = 'left', padx = 10)
114	self.balloon.bind(toggle, 'Toggle balloon help\non and off')
115
116	self.toggleStatusVar = Tkinter.IntVar()
117	self.toggleStatusVar.set(1)
118	toggle = Tkinter.Checkbutton(frame,
119		variable = self.toggleStatusVar,
120		text = 'Status help', command = self.toggle)
121	toggle.pack(side = 'left', padx = 10)
122	self.balloon.bind(toggle,
123                'Toggle status help on and off, on and off' + '\n' * 10 +
124                    'It is very, very big, too.',
125                'Toggle status help on and off')
126
127	# Create and pack the MessageBar.
128	messageBar = Pmw.MessageBar(parent,
129		entry_width = 40,
130		entry_relief='groove',
131		labelpos = 'w',
132	        label_text = 'Status:')
133	messageBar.pack(fill = 'x', expand = 1, padx = 10, pady = 5)
134
135	# Configure the balloon to display its status messages in the
136	# message bar.
137	self.balloon.configure(statuscommand = messageBar.helpmessage)
138
139    def toggle(self):
140	if self.toggleBalloonVar.get():
141	    if self.toggleStatusVar.get():
142		self.balloon.configure(state = 'both')
143	    else:
144		self.balloon.configure(state = 'balloon')
145	else:
146	    if self.toggleStatusVar.get():
147		self.balloon.configure(state = 'status')
148	    else:
149		self.balloon.configure(state = 'none')
150
151    def killButton(self):
152        # Test for old bug when destroying widgets 1) while the
153        # balloon was up and 2) during the initwait period.
154        print 'Destroying button in 2 seconds'
155        self.suicide.after(2000, self.suicide.destroy)
156
157    def _canvasButtonpress(self, event):
158        print 'Destroying blue circle in 2 seconds'
159	self.canvas.after(2000, self.deleteBlueCircle)
160
161    def deleteBlueCircle(self):
162        self.balloon.tagunbind(self.canvas, self.bluecircle)
163        self.canvas.delete(self.bluecircle)
164
165    def _textButtonpress(self, event):
166        print 'Deleting the text tag in 2 seconds'
167	self.text.after(2000, self.deleteTextTag)
168
169    def deleteTextTag(self):
170	self.balloon.tagunbind(self.text, 'TAG1')
171        self.text.tag_delete('TAG1')
172
173
174######################################################################
175
176# Create demo in root window for testing.
177if __name__ == '__main__':
178    root = Tkinter.Tk()
179    Pmw.initialise(root, 12, fontScheme = 'default')
180    root.title(title)
181
182    exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy)
183    exitButton.pack(side = 'bottom')
184    widget = Demo(root)
185    root.mainloop()
186