Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | title = 'Pmw.ScrolledFrame demonstration' |
2 | ||
3 | # Import Pmw from this directory tree. | |
4 | import sys | |
5 | sys.path[:0] = ['../../..'] | |
6 | ||
7 | import Tkinter | |
8 | import Pmw | |
9 | ||
10 | class Demo: | |
11 | def __init__(self, parent): | |
12 | # Create the ScrolledFrame. | |
13 | self.sf = Pmw.ScrolledFrame(parent, | |
14 | labelpos = 'n', label_text = 'ScrolledFrame', | |
15 | usehullsize = 1, | |
16 | hull_width = 400, | |
17 | hull_height = 220, | |
18 | ) | |
19 | ||
20 | # Create a group widget to contain the flex options. | |
21 | w = Pmw.Group(parent, tag_text='Flex') | |
22 | w.pack(side = 'bottom', padx = 5, pady = 3) | |
23 | ||
24 | hflex = Pmw.OptionMenu(w.interior(), | |
25 | labelpos = 'w', | |
26 | label_text = 'Horizontal:', | |
27 | items = ['fixed', 'expand', 'shrink', 'elastic'], | |
28 | command = self.sethflex, | |
29 | menubutton_width = 8, | |
30 | ) | |
31 | hflex.pack(side = 'left', padx = 5, pady = 3) | |
32 | hflex.invoke('fixed') | |
33 | ||
34 | vflex = Pmw.OptionMenu(w.interior(), | |
35 | labelpos = 'w', | |
36 | label_text = 'Vertical:', | |
37 | items = ['fixed', 'expand', 'shrink', 'elastic'], | |
38 | command = self.setvflex, | |
39 | menubutton_width = 8, | |
40 | ) | |
41 | vflex.pack(side = 'left', padx = 5, pady = 3) | |
42 | vflex.invoke('fixed') | |
43 | ||
44 | # Create a group widget to contain the scrollmode options. | |
45 | w = Pmw.Group(parent, tag_text='Scroll mode') | |
46 | w.pack(side = 'bottom', padx = 5, pady = 0) | |
47 | ||
48 | hmode = Pmw.OptionMenu(w.interior(), | |
49 | labelpos = 'w', | |
50 | label_text = 'Horizontal:', | |
51 | items = ['none', 'static', 'dynamic'], | |
52 | command = self.sethscrollmode, | |
53 | menubutton_width = 8, | |
54 | ) | |
55 | hmode.pack(side = 'left', padx = 5, pady = 3) | |
56 | hmode.invoke('dynamic') | |
57 | ||
58 | vmode = Pmw.OptionMenu(w.interior(), | |
59 | labelpos = 'w', | |
60 | label_text = 'Vertical:', | |
61 | items = ['none', 'static', 'dynamic'], | |
62 | command = self.setvscrollmode, | |
63 | menubutton_width = 8, | |
64 | ) | |
65 | vmode.pack(side = 'left', padx = 5, pady = 3) | |
66 | vmode.invoke('dynamic') | |
67 | ||
68 | self.radio = Pmw.RadioSelect(parent, selectmode = 'multiple', | |
69 | command = self.radioSelected) | |
70 | self.radio.add('center', text = 'Keep centered vertically') | |
71 | self.radio.pack(side = 'bottom') | |
72 | ||
73 | buttonBox = Pmw.ButtonBox(parent) | |
74 | buttonBox.pack(side = 'bottom') | |
75 | buttonBox.add('add', text = 'Add a button', command = self.addButton) | |
76 | buttonBox.add('yview', text = 'Show yview', command = self.showYView) | |
77 | buttonBox.add('scroll', text = 'Page down', command = self.pageDown) | |
78 | ||
79 | # Pack this last so that the buttons do not get shrunk when | |
80 | # the window is resized. | |
81 | self.sf.pack(padx = 5, pady = 3, fill = 'both', expand = 1) | |
82 | ||
83 | self.frame = self.sf.interior() | |
84 | ||
85 | self.row = 0 | |
86 | self.col = 0 | |
87 | ||
88 | for count in range(15): | |
89 | self.addButton() | |
90 | ||
91 | def sethscrollmode(self, tag): | |
92 | self.sf.configure(hscrollmode = tag) | |
93 | ||
94 | def setvscrollmode(self, tag): | |
95 | self.sf.configure(vscrollmode = tag) | |
96 | ||
97 | def sethflex(self, tag): | |
98 | self.sf.configure(horizflex = tag) | |
99 | ||
100 | def setvflex(self, tag): | |
101 | self.sf.configure(vertflex = tag) | |
102 | ||
103 | def addButton(self): | |
104 | button = Tkinter.Button(self.frame, | |
105 | text = '(%d,%d)' % (self.col, self.row)) | |
106 | button.grid(row = self.row, column = self.col, sticky = 'nsew') | |
107 | ||
108 | self.frame.grid_rowconfigure(self.row, weight = 1) | |
109 | self.frame.grid_columnconfigure(self.col, weight = 1) | |
110 | if self.sf.cget('horizflex') == 'expand' or \ | |
111 | self.sf.cget('vertflex') == 'expand': | |
112 | self.sf.reposition() | |
113 | ||
114 | if 'center' in self.radio.getcurselection(): | |
115 | self.sf.update_idletasks() | |
116 | self.centerPage() | |
117 | ||
118 | if self.col == self.row: | |
119 | self.col = 0 | |
120 | self.row = self.row + 1 | |
121 | else: | |
122 | self.col = self.col + 1 | |
123 | ||
124 | def showYView(self): | |
125 | print self.sf.yview() | |
126 | ||
127 | def pageDown(self): | |
128 | self.sf.yview('scroll', 1, 'page') | |
129 | ||
130 | def radioSelected(self, name, state): | |
131 | if state: | |
132 | self.centerPage() | |
133 | ||
134 | def centerPage(self): | |
135 | # Example of how to use the yview() method of Pmw.ScrolledFrame. | |
136 | top, bottom = self.sf.yview() | |
137 | size = bottom - top | |
138 | middle = 0.5 - size / 2 | |
139 | self.sf.yview('moveto', middle) | |
140 | ||
141 | ###################################################################### | |
142 | ||
143 | # Create demo in root window for testing. | |
144 | if __name__ == '__main__': | |
145 | import os | |
146 | if os.name == 'nt': | |
147 | size = 16 | |
148 | else: | |
149 | size = 12 | |
150 | root = Tkinter.Tk() | |
151 | Pmw.initialise(root, size = size, fontScheme = 'pmw2') | |
152 | root.title(title) | |
153 | ||
154 | exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) | |
155 | exitButton.pack(side = 'bottom') | |
156 | widget = Demo(root) | |
157 | root.mainloop() |