File: PmwDialog.py.html

package info (click to toggle)
python-pmw 0.6.2-0.1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,652 kB
  • ctags: 2,716
  • sloc: python: 10,720; makefile: 44; sh: 24
file content (176 lines) | stat: -rw-r--r-- 6,844 bytes parent folder | download
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
<!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>PmwDialog.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
<FONT COLOR="#DD0000"># Based on iwidgets2.2.0/dialog.itk and iwidgets2.2.0/dialogshell.itk code.</FONT>

<FONT COLOR="#DD0000"># Convention:</FONT>
<FONT COLOR="#DD0000">#   Each dialog window should have one of these as the rightmost button:</FONT>
<FONT COLOR="#DD0000">#     Close         Close a window which only displays information.</FONT>
<FONT COLOR="#DD0000">#     Cancel        Close a window which may be used to change the state of</FONT>
<FONT COLOR="#DD0000">#                   the application.</FONT>

import sys
import Tkinter
import Pmw

<FONT COLOR="#DD0000"># A Toplevel with a ButtonBox and child site.</FONT>

<STRONG><FONT COLOR="#CC6600">class Dialog</FONT></STRONG>(Pmw.MegaToplevel):
<STRONG>    def __init__</STRONG>(self, parent = None, **kw):

	<FONT COLOR="#DD0000"># Define the megawidget options.</FONT>
	INITOPT = Pmw.INITOPT
	optiondefs = (
	    (<FONT COLOR="#009900">'buttonbox_hull_borderwidth'</FONT>,   1,         None),
	    (<FONT COLOR="#009900">'buttonbox_hull_relief'</FONT>,        <FONT COLOR="#009900">'raised'</FONT>,  None),
	    (<FONT COLOR="#009900">'buttonboxpos'</FONT>,                 <FONT COLOR="#009900">'s'</FONT>,       INITOPT),
	    (<FONT COLOR="#009900">'buttons'</FONT>,                      (<FONT COLOR="#009900">'OK'</FONT>,),   self._buttons),
	    (<FONT COLOR="#009900">'command'</FONT>,                      None,      None),
	    (<FONT COLOR="#009900">'dialogchildsite_borderwidth'</FONT>,  1,         None),
	    (<FONT COLOR="#009900">'dialogchildsite_relief'</FONT>,       <FONT COLOR="#009900">'raised'</FONT>,  None),
	    (<FONT COLOR="#009900">'defaultbutton'</FONT>,                None,      self._defaultButton),
	    (<FONT COLOR="#009900">'separatorwidth'</FONT>,               0,         INITOPT),
	)
	self.defineoptions(kw, optiondefs)

	<FONT COLOR="#DD0000"># Initialise the base class (after defining the options).</FONT>
	Pmw.MegaToplevel.__init__(self, parent)

	<FONT COLOR="#DD0000"># Create the components.</FONT>

	oldInterior = Pmw.MegaToplevel.interior(self)

	<FONT COLOR="#DD0000"># Set up pack options according to the position of the button box.</FONT>
        pos = self[<FONT COLOR="#009900">'buttonboxpos'</FONT>]
	if pos not in <FONT COLOR="#009900">'nsew'</FONT>:
	    raise ValueError, \
	        <FONT COLOR="#009900">'bad buttonboxpos option "%s":  should be n, s, e, or w'</FONT> \
		    % pos

	if pos in <FONT COLOR="#009900">'ns'</FONT>:
	    orient = <FONT COLOR="#009900">'horizontal'</FONT>
	    fill = <FONT COLOR="#009900">'x'</FONT>
	    if pos == <FONT COLOR="#009900">'n'</FONT>:
	        side = <FONT COLOR="#009900">'top'</FONT>
	    else:
	        side = <FONT COLOR="#009900">'bottom'</FONT>
	else:
	    orient = <FONT COLOR="#009900">'vertical'</FONT>
	    fill = <FONT COLOR="#009900">'y'</FONT>
	    if pos == <FONT COLOR="#009900">'w'</FONT>:
	        side = <FONT COLOR="#009900">'left'</FONT>
	    else:
	        side = <FONT COLOR="#009900">'right'</FONT>

	<FONT COLOR="#DD0000"># Create the button box.</FONT>
	self._buttonBox = self.createcomponent(<FONT COLOR="#009900">'buttonbox'</FONT>,
		(), None,
		Pmw.ButtonBox, (oldInterior,), orient = orient)
	self._buttonBox.pack(side = side, fill = fill)

	<FONT COLOR="#DD0000"># Create the separating line.</FONT>
	width = self[<FONT COLOR="#009900">'separatorwidth'</FONT>]
	if width > 0:
	    self._separator = self.createcomponent(<FONT COLOR="#009900">'separator'</FONT>,
		    (), None,
		    Tkinter.Frame, (oldInterior,), relief = <FONT COLOR="#009900">'sunken'</FONT>,
		    height = width, width = width, borderwidth = width / 2)
	    self._separator.pack(side = side, fill = fill)
	
	<FONT COLOR="#DD0000"># Create the child site.</FONT>
	self.__dialogChildSite = self.createcomponent(<FONT COLOR="#009900">'dialogchildsite'</FONT>,
		(), None,
		Tkinter.Frame, (oldInterior,))
	self.__dialogChildSite.pack(side=side, fill=<FONT COLOR="#009900">'both'</FONT>, expand=1)

	self.oldButtons = ()
	self.oldDefault = None

	self.bind(<FONT COLOR="#009900">'&lt;Return&gt;'</FONT>, self._invokeDefault)
        self.userdeletefunc(self._doCommand)
        self.usermodaldeletefunc(self._doCommand)
	
	<FONT COLOR="#DD0000"># Check keywords and initialise options.</FONT>
	self.initialiseoptions(Dialog)

<STRONG>    def interior</STRONG>(self):
	return self.__dialogChildSite

<STRONG>    def invoke</STRONG>(self, index = <FONT COLOR="#009900">'default'</FONT>):
	return self._buttonBox.invoke(index)

<STRONG>    def _invokeDefault</STRONG>(self, event):
	try:
	    self._buttonBox.index(<FONT COLOR="#009900">'default'</FONT>)
	except ValueError:
	    sys.exc_traceback = None   <FONT COLOR="#DD0000"># Clean up object references</FONT>
	    return
	self._buttonBox.invoke()

<STRONG>    def _doCommand</STRONG>(self, name = None):
	command = self[<FONT COLOR="#009900">'command'</FONT>]
	if callable(command):
	    return command(name)
	else:
	    if self.active():
	        self.deactivate(name)
	    else:
	        self.withdraw()

<STRONG>    def _buttons</STRONG>(self):
	buttons = self[<FONT COLOR="#009900">'buttons'</FONT>]
	if type(buttons) != type(()) and type(buttons) != type([]):
	    raise ValueError, \
	        <FONT COLOR="#009900">'bad buttons option "%s": should be a tuple'</FONT> % str(buttons)
	if self.oldButtons == buttons:
	  return

	self.oldButtons = buttons

	for index in range(self._buttonBox.numbuttons()):
	    self._buttonBox.delete(0)
	for name in buttons:
	    self._buttonBox.add(name,
		command=lambda self=self, name=name: self._doCommand(name))

	if len(buttons) > 0:
	    defaultbutton = self[<FONT COLOR="#009900">'defaultbutton'</FONT>]
	    if defaultbutton is None:
		self._buttonBox.setdefault(None)
	    else:
		try:
		    self._buttonBox.index(defaultbutton)
		except ValueError:
		    sys.exc_traceback = None   <FONT COLOR="#DD0000"># Clean up object references</FONT>
		else:
		    self._buttonBox.setdefault(defaultbutton)
	self._buttonBox.alignbuttons()

<STRONG>    def _defaultButton</STRONG>(self):
	defaultbutton = self[<FONT COLOR="#009900">'defaultbutton'</FONT>]
	if self.oldDefault == defaultbutton:
	  return

	self.oldDefault = defaultbutton

	if len(self[<FONT COLOR="#009900">'buttons'</FONT>]) > 0:
	    if defaultbutton is None:
		self._buttonBox.setdefault(None)
	    else:
		try:
		    self._buttonBox.index(defaultbutton)
		except ValueError:
		    sys.exc_traceback = None   <FONT COLOR="#DD0000"># Clean up object references</FONT>
		else:
		    self._buttonBox.setdefault(defaultbutton)

</PRE>

 </BODY> </HTML>