File: PmwRadioSelect.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 (160 lines) | stat: -rw-r--r-- 6,457 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
<!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>PmwRadioSelect.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
import Tkinter
import Pmw

<STRONG><FONT COLOR="#CC6600">class RadioSelect</FONT></STRONG>(Pmw.MegaWidget):
    <FONT COLOR="#DD0000"># A collection of several buttons.  In single mode, only one</FONT>
    <FONT COLOR="#DD0000"># button may be selected.  In multiple mode, any number of buttons</FONT>
    <FONT COLOR="#DD0000"># may be selected.</FONT>

<STRONG>    def __init__</STRONG>(self, parent = None, **kw):

	<FONT COLOR="#DD0000"># Define the megawidget options.</FONT>
	INITOPT = Pmw.INITOPT
	optiondefs = (
	    (<FONT COLOR="#009900">'command'</FONT>,       None,          None),
	    (<FONT COLOR="#009900">'labelmargin'</FONT>,   0,             INITOPT),
	    (<FONT COLOR="#009900">'labelpos'</FONT>,      None,          INITOPT),
	    (<FONT COLOR="#009900">'orient'</FONT>,       <FONT COLOR="#009900">'horizontal'</FONT>,   INITOPT),
	    (<FONT COLOR="#009900">'padx'</FONT>,          5,             INITOPT),
	    (<FONT COLOR="#009900">'pady'</FONT>,          5,             INITOPT),
	    (<FONT COLOR="#009900">'selectmode'</FONT>,    <FONT COLOR="#009900">'single'</FONT>,      INITOPT),
	)
	self.defineoptions(kw, optiondefs)

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

	<FONT COLOR="#DD0000"># Create the components.</FONT>
	interior = self.interior()
	if self[<FONT COLOR="#009900">'labelpos'</FONT>] is None:
	    self._radioSelectFrame = self._hull
	else:
	    self._radioSelectFrame = self.createcomponent(<FONT COLOR="#009900">'frame'</FONT>,
		    (), None,
		    Tkinter.Frame, (interior,))
	    self._radioSelectFrame.grid(column=2, row=2, sticky=<FONT COLOR="#009900">'nsew'</FONT>)
	    interior.grid_columnconfigure(2, weight=1)
	    interior.grid_rowconfigure(2, weight=1)

	    self.createlabel(interior)

	<FONT COLOR="#DD0000"># Initialise instance variables.</FONT>
	self._buttonList = []
	if self[<FONT COLOR="#009900">'selectmode'</FONT>] == <FONT COLOR="#009900">'single'</FONT>:
	    self.selection = None
	elif self[<FONT COLOR="#009900">'selectmode'</FONT>] == <FONT COLOR="#009900">'multiple'</FONT>:
	    self.selection = []
	else: 
	    raise ValueError, <FONT COLOR="#009900">'bad selectmode option "'</FONT> + \
		    self[<FONT COLOR="#009900">'selectmode'</FONT>] + <FONT COLOR="#009900">'": should be single or multiple'</FONT>

	<FONT COLOR="#DD0000"># Check keywords and initialise options.</FONT>
	self.initialiseoptions(RadioSelect)

<STRONG>    def getcurselection</STRONG>(self):
	return self.selection

<STRONG>    def numbuttons</STRONG>(self):
        return len(self._buttonList)

<STRONG>    def index</STRONG>(self, index):
	<FONT COLOR="#DD0000"># Return the integer index of the button with the given index.</FONT>

	listLength = len(self._buttonList)
	if type(index) == type(1):
	    if index < listLength:
		return index
	    else:
		raise ValueError, <FONT COLOR="#009900">'index "%s" is out of range'</FONT> % index
	elif index == <FONT COLOR="#009900">'end'</FONT>:
	    if listLength > 0:
		return listLength - 1
	    else:
		raise ValueError, <FONT COLOR="#009900">'RadioSelect has no buttons'</FONT>
	else:
	    for count in range(listLength):
		name = self._buttonList[count]
		if index == name:
		    return count
	    validValues = <FONT COLOR="#009900">'number, end or a name'</FONT>
	    raise ValueError, \
		    <FONT COLOR="#009900">'bad index "%s": must be %s'</FONT> % (index, validValues)

<STRONG>    def add</STRONG>(self, name, **kw):
	if name in self._buttonList:
	    raise ValueError, <FONT COLOR="#009900">'name "%s" already exists'</FONT> % name

	kw[<FONT COLOR="#009900">'command'</FONT>] = lambda self=self, name=name: self.invoke(name)
	if not kw.has_key(<FONT COLOR="#009900">'text'</FONT>):
	    kw[<FONT COLOR="#009900">'text'</FONT>] = name
	button = apply(self.createcomponent, (name,
		(), <FONT COLOR="#009900">'Button'</FONT>,
		Tkinter.Button, (self._radioSelectFrame,)), kw)

	if self[<FONT COLOR="#009900">'orient'</FONT>] == <FONT COLOR="#009900">'horizontal'</FONT>:
	    self._radioSelectFrame.grid_rowconfigure(0, weight=1)
	    col = len(self._buttonList)
	    button.grid(column=col, row=0, padx = self[<FONT COLOR="#009900">'padx'</FONT>],
		    pady = self[<FONT COLOR="#009900">'pady'</FONT>], sticky=<FONT COLOR="#009900">'nsew'</FONT>)
	    self._radioSelectFrame.grid_columnconfigure(col, weight=1)
	else:
	    self._radioSelectFrame.grid_columnconfigure(0, weight=1)
	    row = len(self._buttonList)
	    button.grid(column=0, row=row, padx = self[<FONT COLOR="#009900">'padx'</FONT>],
		    pady = self[<FONT COLOR="#009900">'pady'</FONT>], sticky=<FONT COLOR="#009900">'ew'</FONT>)
	    self._radioSelectFrame.grid_rowconfigure(row, weight=1)

	self._buttonList.append(name)
	return button

<STRONG>    def deleteall</STRONG>(self):
	for name in self._buttonList:
	    self.destroycomponent(name)
	self._buttonList = []
	if self[<FONT COLOR="#009900">'selectmode'</FONT>] == <FONT COLOR="#009900">'single'</FONT>:
	    self.selection = None
	else: 
	    self.selection = []

<STRONG>    def invoke</STRONG>(self, index):
	index = self.index(index)
	name = self._buttonList[index]

	if self[<FONT COLOR="#009900">'selectmode'</FONT>] == <FONT COLOR="#009900">'single'</FONT>:
	    for button in self._buttonList:
		if button == name:
		    self.component(button).configure(relief=<FONT COLOR="#009900">'sunken'</FONT>)
		else:
		    self.component(button).configure(relief=<FONT COLOR="#009900">'raised'</FONT>)
	    self.selection = name
	    command = self[<FONT COLOR="#009900">'command'</FONT>]
	    if callable(command):
		return command(name)
        else:
	    <FONT COLOR="#DD0000"># Multiple selections allowed</FONT>
	    if name in self.selection:
		self.component(name).configure(relief=<FONT COLOR="#009900">'raised'</FONT>)
		self.selection.remove(name)
		state = 0
	    else:
		self.component(name).configure(relief=<FONT COLOR="#009900">'sunken'</FONT>)
		self.selection.append(name)
		state = 1

            command = self[<FONT COLOR="#009900">'command'</FONT>]
            if callable(command):
	      return command(name, state)

</PRE>

 </BODY> </HTML>