File: PmwScrolledText.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 (205 lines) | stat: -rw-r--r-- 8,662 bytes parent folder | download | duplicates (2)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!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>PmwScrolledText.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
<FONT COLOR="#DD0000"># Based on iwidgets2.2.0/scrolledtext.itk code.   </FONT>

import Tkinter
import Pmw

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

	<FONT COLOR="#DD0000"># Define the megawidget options.</FONT>
	INITOPT = Pmw.INITOPT
	optiondefs = (
	    (<FONT COLOR="#009900">'labelmargin'</FONT>,    0,            INITOPT),
	    (<FONT COLOR="#009900">'labelpos'</FONT>,       None,         INITOPT),
	    (<FONT COLOR="#009900">'scrollmargin'</FONT>,   2,            INITOPT),
	    (<FONT COLOR="#009900">'vscrollmode'</FONT>,   <FONT COLOR="#009900">'dynamic'</FONT>,     self._vscrollMode),
	    (<FONT COLOR="#009900">'hscrollmode'</FONT>,   <FONT COLOR="#009900">'dynamic'</FONT>,     self._hscrollMode),
	)
	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()

	<FONT COLOR="#DD0000"># Create the text widget.</FONT>
	self._textbox = self.createcomponent(<FONT COLOR="#009900">'text'</FONT>,
		(), None,
		Tkinter.Text, (interior,),
		xscrollcommand=self._scrollTextX,
		yscrollcommand=self._scrollTextY)
	self._textbox.grid(row = 2, column = 2, sticky = <FONT COLOR="#009900">'news'</FONT>)
	interior.grid_rowconfigure(2, weight = 1, minsize = 0)
	interior.grid_columnconfigure(2, weight = 1, minsize = 0)

	<FONT COLOR="#DD0000"># Create the vertical scrollbar</FONT>
	self._vertScrollbar = self.createcomponent(<FONT COLOR="#009900">'vertscrollbar'</FONT>,
		(), <FONT COLOR="#009900">'Scrollbar'</FONT>,
		Tkinter.Scrollbar, (interior,),
		orient=<FONT COLOR="#009900">'vertical'</FONT>, command=self._textbox.yview)

	<FONT COLOR="#DD0000"># Create the horizontal scrollbar</FONT>
	self._horizScrollbar = self.createcomponent(<FONT COLOR="#009900">'horizscrollbar'</FONT>,
		(), <FONT COLOR="#009900">'Scrollbar'</FONT>,
		Tkinter.Scrollbar, (interior,),
	        orient=<FONT COLOR="#009900">'horizontal'</FONT>, command=self._textbox.xview)

	self.createlabel(interior, childCols = 3, childRows = 3)

	<FONT COLOR="#DD0000"># Initialise instance variables.</FONT>
	self._vertMode = None
	self._horizMode = None
	self._horizRecurse = 0

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

    <FONT COLOR="#DD0000"># ======================================================================</FONT>

    <FONT COLOR="#DD0000"># Configuration methods.</FONT>

<STRONG>    def _vscrollMode</STRONG>(self):
	mode = self[<FONT COLOR="#009900">'vscrollmode'</FONT>]

	if mode == <FONT COLOR="#009900">'static'</FONT>:
	    self._vertScrollbarDisplay(1)
	elif mode == <FONT COLOR="#009900">'dynamic'</FONT> or mode == <FONT COLOR="#009900">'none'</FONT>:
	    self._vertScrollbarDisplay(0)
	else:
	    message = <FONT COLOR="#009900">'bad vscrollmode option "%s": should be static, dynamic, or none'</FONT> % mode
	    raise ValueError, message

<STRONG>    def _hscrollMode</STRONG>(self):
	mode = self[<FONT COLOR="#009900">'hscrollmode'</FONT>]

	if mode == <FONT COLOR="#009900">'static'</FONT>:
	    self._horizScrollbarDisplay(1)
	elif mode == <FONT COLOR="#009900">'dynamic'</FONT> or mode == <FONT COLOR="#009900">'none'</FONT>:
	    self._horizScrollbarDisplay(0)
	else:
	    message = <FONT COLOR="#009900">'bad hscrollmode option "%s": should be static, dynamic, or none'</FONT> % mode
	    raise ValueError, message

    <FONT COLOR="#DD0000"># ======================================================================</FONT>

    <FONT COLOR="#DD0000"># Public methods.</FONT>

<STRONG>    def clear</STRONG>(self):
	self._textbox.delete(<FONT COLOR="#009900">'1.0'</FONT>, <FONT COLOR="#009900">'end'</FONT>)

<STRONG>    def importfile</STRONG>(self, fileName, where = <FONT COLOR="#009900">'end'</FONT>):
	file = open(fileName, <FONT COLOR="#009900">'r'</FONT>)
	self._textbox.insert(where, file.read())
	file.close()

<STRONG>    def exportfile</STRONG>(self, fileName):
	file = open(fileName, <FONT COLOR="#009900">'w'</FONT>)
	file.write(self._textbox.get(<FONT COLOR="#009900">'1.0'</FONT>, <FONT COLOR="#009900">'end'</FONT>))
	file.close()

<STRONG>    def settext</STRONG>(self, text):
	disabled = (self._textbox.cget(<FONT COLOR="#009900">'state'</FONT>) == <FONT COLOR="#009900">'disabled'</FONT>)
	if disabled:
	    self._textbox.configure(state=<FONT COLOR="#009900">'normal'</FONT>)
	self._textbox.delete(<FONT COLOR="#009900">'0.0'</FONT>, <FONT COLOR="#009900">'end'</FONT>)
	self._textbox.insert(<FONT COLOR="#009900">'end'</FONT>, text)
	if disabled:
	    self._textbox.configure(state=<FONT COLOR="#009900">'disabled'</FONT>)

    <FONT COLOR="#DD0000"># Override Tkinter.Text get method, so that if it is called with</FONT>
    <FONT COLOR="#DD0000"># no arguments, return all text (consistent with other widgets).</FONT>
<STRONG>    def get</STRONG>(self, first=None, last=None):
	if not first and not last:
	    return self._textbox.get(<FONT COLOR="#009900">'1.0'</FONT>, <FONT COLOR="#009900">'end'</FONT>)
	else:
	    return self._textbox.get(first, last)


    <FONT COLOR="#DD0000"># ======================================================================</FONT>

    <FONT COLOR="#DD0000"># Private methods.</FONT>

<STRONG>    def _vertScrollbarDisplay</STRONG>(self, mode):
	if mode != self._vertMode:
	    self._vertMode = mode

	    margin = self[<FONT COLOR="#009900">'scrollmargin'</FONT>]
	    interior = self.interior()

	    if self._vertMode:
		self._vertScrollbar.grid(row = 2, column = 4, sticky = <FONT COLOR="#009900">'news'</FONT>)
		interior.grid_columnconfigure(3, minsize = margin)
	    else:
		self._vertScrollbar.grid_forget()
		interior.grid_columnconfigure(3, minsize = 0)

<STRONG>    def _horizScrollbarDisplay</STRONG>(self, mode):
	if mode != self._horizMode:
	    self._horizMode = mode

	    margin = self[<FONT COLOR="#009900">'scrollmargin'</FONT>]
	    interior = self.interior()

	    if self._horizMode:
		self._horizScrollbar.grid(row = 4, column = 2, sticky = <FONT COLOR="#009900">'news'</FONT>)
		interior.grid_rowconfigure(3, minsize = margin)
	    else:
		self._horizScrollbar.grid_forget()
		interior.grid_rowconfigure(3, minsize = 0)

<STRONG>    def _scrollTextX</STRONG>(self, first, last):
	self._horizScrollbar.set(first, last)

	<FONT COLOR="#DD0000"># Infinite recursion must be avoided for dynamic horizontal scrollbars.</FONT>
	<FONT COLOR="#DD0000"># A max depth of 1 is allowed for hiding the scrollbar, 2 for showing</FONT>
	# it, so that showing always <FONT COLOR="#009900">'wins'</FONT>!
	if self[<FONT COLOR="#009900">'hscrollmode'</FONT>] == <FONT COLOR="#009900">'dynamic'</FONT>:
	    if first == <FONT COLOR="#009900">'0'</FONT> and last == <FONT COLOR="#009900">'1'</FONT>:
		if self._horizMode:
		    if self._horizRecurse < 2:
			self._horizRecurse = self._horizRecurse + 1
			self._horizScrollbarDisplay(0)
			self.update_idletasks()
			self._horizRecurse = self._horizRecurse - 1
	    else:
		if not self._horizMode:
		    if self._horizRecurse < 3:
			self._horizRecurse = self._horizRecurse + 1
			self._horizScrollbarDisplay(1)
			self.update_idletasks()
			self._horizRecurse = self._horizRecurse - 1

<STRONG>    def _scrollTextY</STRONG>(self, first, last):
	<FONT COLOR="#DD0000"># The text widget calls this just after it has been created,</FONT>
	# with arguments (<FONT COLOR="#009900">'0'</FONT>, <FONT COLOR="#009900">'0'</FONT>).
	if first == <FONT COLOR="#009900">'0'</FONT> and last == <FONT COLOR="#009900">'0'</FONT>:
	    return
	self._vertScrollbar.set(first, last)

	if self[<FONT COLOR="#009900">'vscrollmode'</FONT>] == <FONT COLOR="#009900">'dynamic'</FONT>:
	    if first == <FONT COLOR="#009900">'0'</FONT> and last == <FONT COLOR="#009900">'1'</FONT>:
		self._vertScrollbarDisplay(0)
	    else:
		self._vertScrollbarDisplay(1)

    <FONT COLOR="#DD0000"># Need to explicitly forward this to override the stupid</FONT>
    <FONT COLOR="#DD0000"># (grid_)bbox method inherited from Tkinter.Frame.Grid.</FONT>
<STRONG>    def bbox</STRONG>(self, index):
	return self._textbox.bbox(index)

Pmw.forwardmethods(ScrolledText, Tkinter.Text, <FONT COLOR="#009900">'_textbox'</FONT>)

</PRE>

 </BODY> </HTML>