1title = 'Pmw.ScrolledText demonstration'
2
3# Import Pmw from this directory tree.
4import sys
5sys.path[:0] = ['../../..']
6
7import os
8import Tkinter
9import Pmw
10
11class Demo:
12    def __init__(self, parent):
13	# Create the ScrolledText.
14	self.st = Pmw.ScrolledText(parent,
15		borderframe = 1,
16		labelpos = 'n',
17		label_text='ScrolledText.py',
18		usehullsize = 1,
19		hull_width = 400,
20		hull_height = 300,
21		text_padx = 10,
22		text_pady = 10,
23		text_wrap='none'
24	)
25
26	# Create a group widget to contain the scrollmode options.
27	w = Pmw.Group(parent, tag_text='Scroll mode')
28	w.pack(side = 'bottom', padx = 5, pady = 5)
29
30	hmode = Pmw.OptionMenu(w.interior(),
31		labelpos = 'w',
32		label_text = 'Horizontal:',
33		items = ['none', 'static', 'dynamic'],
34		command = self.sethscrollmode,
35		menubutton_width = 8,
36	)
37	hmode.pack(side = 'left', padx = 5, pady = 5)
38	hmode.invoke('dynamic')
39
40	vmode = Pmw.OptionMenu(w.interior(),
41		labelpos = 'w',
42		label_text = 'Vertical:',
43		items = ['none', 'static', 'dynamic'],
44		command = self.setvscrollmode,
45		menubutton_width = 8,
46	)
47	vmode.pack(side = 'left', padx = 5, pady = 5)
48	vmode.invoke('dynamic')
49
50	buttonBox = Pmw.ButtonBox(parent)
51	buttonBox.pack(side = 'bottom')
52	buttonBox.add('yview', text = 'Show\nyview', command = self.showYView)
53	buttonBox.add('scroll', text = 'Page\ndown', command = self.pageDown)
54	buttonBox.add('center', text = 'Center', command = self.centerPage)
55
56	# Pack this last so that the buttons do not get shrunk when
57	# the window is resized.
58	self.st.pack(padx = 5, pady = 5, fill = 'both', expand = 1)
59
60	# Read this file into the text widget.
61	head, tail = os.path.split(sys.argv[0])
62	self.st.importfile(os.path.join(head,'ScrolledText.py'))
63
64	self.st.insert('end', '\nThis demonstrates how to\n' +
65	    'add a window to a text widget:  ')
66	counter = Pmw.Counter(self.st.component('text'),
67	    entryfield_value = 9999)
68	self.st.window_create('end', window = counter)
69
70    def sethscrollmode(self, tag):
71	self.st.configure(hscrollmode = tag)
72
73    def setvscrollmode(self, tag):
74	self.st.configure(vscrollmode = tag)
75
76    def showYView(self):
77        print self.st.yview()
78
79    def pageDown(self):
80        self.st.yview('scroll', 1, 'page')
81
82    def centerPage(self):
83        top, bottom = self.st.yview()
84        size = bottom - top
85        middle = 0.5 - size / 2
86        self.st.yview('moveto', middle)
87
88######################################################################
89
90# Create demo in root window for testing.
91if __name__ == '__main__':
92    root = Tkinter.Tk()
93    Pmw.initialise(root)
94    root.title(title)
95
96    exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy)
97    exitButton.pack(side = 'bottom')
98    widget = Demo(root)
99    root.mainloop()
100