-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathhelpwindow.py
More file actions
executable file
·229 lines (198 loc) · 8.76 KB
/
helpwindow.py
File metadata and controls
executable file
·229 lines (198 loc) · 8.76 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/env python3
#
#--------------------------------------------------------
# Help Window for the Open Galaxy project manager
#
#--------------------------------------------------------
# Written by Tim Edwards
# efabless, inc.
# September 12, 2016
# Version 0.1
#--------------------------------------------------------
import re
import tkinter
from tkinter import ttk
class HelpWindow(tkinter.Toplevel):
"""Open Galaxy help window."""
def __init__(self, parent=None, fontsize = 11, *args, **kwargs):
'''See the __init__ for Tkinter.Toplevel.'''
tkinter.Toplevel.__init__(self, parent, *args, **kwargs)
s = ttk.Style()
s.configure('normal.TButton', font=('Helvetica', fontsize), border = 3, relief = 'raised')
self.withdraw()
self.title('Open Galaxy Help')
self.helptitle = ttk.Label(self, style='title.TLabel', text = '(no text)')
self.helptitle.grid(column = 0, row = 0, sticky = "news")
self.helpbar = ttk.Separator(self, orient='horizontal')
self.helpbar.grid(column = 0, row = 1, sticky = "news")
self.hframe = tkinter.Frame(self)
self.hframe.grid(column = 0, row = 2, sticky = "news")
self.hframe.helpdisplay = ttk.Frame(self.hframe)
self.hframe.helpdisplay.pack(side = 'left', fill = 'both', expand = 'true')
self.hframe.helpdisplay.helptext = tkinter.Text(self.hframe.helpdisplay, wrap='word')
self.hframe.helpdisplay.helptext.pack(side = 'top', fill = 'both', expand = 'true')
# Add scrollbar to help window
self.hframe.scrollbar = ttk.Scrollbar(self.hframe)
self.hframe.scrollbar.pack(side='right', fill='y')
# attach help window to scrollbar
self.hframe.helpdisplay.helptext.config(yscrollcommand = self.hframe.scrollbar.set)
self.hframe.scrollbar.config(command = self.hframe.helpdisplay.helptext.yview)
self.hframe.toc = ttk.Treeview(self.hframe, selectmode='browse')
self.hframe.toc.bind('<<TreeviewSelect>>', self.toc_to_page)
self.hframe.toc.bind('<<TreeviewOpen>>', self.toc_toggle)
self.hframe.toc.bind('<<TreeviewClose>>', self.toc_toggle)
self.hframe.toc.tag_configure('title', font=('Helvetica', fontsize, 'bold italic'),
foreground = 'brown', anchor = 'center')
self.hframe.toc.heading('#0', text = "Table of Contents")
self.bbar = ttk.Frame(self)
self.bbar.grid(column = 0, row = 3, sticky = "news")
self.bbar.close_button = ttk.Button(self.bbar, text='Close',
command=self.close, style = 'normal.TButton')
self.bbar.close_button.grid(column=0, row=0, padx = 5)
self.bbar.prev_button = ttk.Button(self.bbar, text='Prev',
command=self.prevpage, style = 'normal.TButton')
self.bbar.prev_button.grid(column=1, row=0, padx = 5)
self.bbar.next_button = ttk.Button(self.bbar, text='Next',
command=self.nextpage, style = 'normal.TButton')
self.bbar.next_button.grid(column=2, row=0, padx = 5)
self.bbar.contents_button = ttk.Button(self.bbar, text='Table of Contents',
command=self.page_to_toc, style = 'normal.TButton')
self.bbar.contents_button.grid(column=3, row=0, padx = 5)
self.rowconfigure(0, weight=0)
self.rowconfigure(1, weight=0)
self.rowconfigure(2, weight=1)
self.rowconfigure(3, weight=0)
self.columnconfigure(0, weight=1)
# Help pages
self.pages = []
self.pageno = -1 # No page
self.toggle = False
def grid_configure(self, padx, pady):
pass
def redisplay(self):
# remove contents
if self.pageno >= 0 and self.pageno < len(self.pages):
self.hframe.helpdisplay.helptext.delete('1.0', 'end')
self.hframe.helpdisplay.helptext.insert('end', self.pages[self.pageno]['text'])
self.helptitle.configure(text = self.pages[self.pageno]['title'])
def toc_toggle(self, event):
self.toggle = True
def toc_to_page(self, event):
treeview = event.widget
selection = treeview.item(treeview.selection())
# Make sure any open/close callback is handled first!
self.update_idletasks()
if self.toggle:
# Item was opened or closed, so consider this a 'false select' and
# do not go to the page.
self.toggle = False
return
if 'values' in selection:
pagenum = selection['values'][0]
else:
print('Unknown page selected.')
pagenum = 0
# Display a page after displaying the table of contents
self.hframe.toc.pack_forget()
self.hframe.scrollbar.pack_forget()
self.hframe.helpdisplay.pack(side='left', fill='both', expand = 'true')
self.hframe.scrollbar.pack(side='right', fill='y')
self.hframe.scrollbar.config(command = self.hframe.helpdisplay.helptext.yview)
# Enable Prev and Next buttons
self.bbar.prev_button.configure(state='enabled')
self.bbar.next_button.configure(state='enabled')
# Redisplay
self.page(pagenum)
def page_to_toc(self):
# Display the table of contents after displaying a page
self.hframe.scrollbar.pack_forget()
self.hframe.helpdisplay.pack_forget()
self.hframe.toc.pack(side='left', fill='both', expand = 'true')
self.hframe.scrollbar.pack(side='right', fill='y')
self.hframe.scrollbar.config(command = self.hframe.toc.yview)
# Disable Prev and Next buttons
self.bbar.prev_button.configure(state='disabled')
self.bbar.next_button.configure(state='disabled')
# Simple add page with a single block of plain text
def add_page(self, toc_text, text_block):
newdict = {}
newdict['text'] = text_block
newdict['title'] = toc_text
self.pages.append(newdict)
newpageno = len(self.pages)
self.hframe.toc.insert('', 'end', text=str(newpageno) + '. ' + toc_text,
tag='title', value = newpageno - 1)
if self.pageno < 0:
self.pageno = 0 # First page
# Fill the help text from a file. The format of the file is:
# <page_num>
# <title>
# <text>
# '.'
# Text is multi-line and ends when '.' is encountered by itself
def add_pages_from_file(self, filename):
endpagerex = re.compile('^\.$')
newpagerex = re.compile('^[0-9\.]+$')
commentrex = re.compile('^[\-]+$')
hierarchy = ''
print('Loading help text from file ' + filename)
with open(filename, 'r') as f:
toc_text = []
page_text = []
for line in f:
if newpagerex.match(line) or endpagerex.match(line):
if toc_text and page_text:
newdict = {}
self.pages.append(newdict)
newpageno = len(self.pages)
if '.' in hierarchy:
pageinfo = hierarchy.rsplit('.', 1)
if pageinfo[1] == '':
parentid = ''
pageid = pageinfo[0]
else:
parentid = pageinfo[0]
pageid = pageinfo[1]
else:
parentid = ''
pageid = hierarchy
if parentid:
pageid = parentid + '.' + pageid
newdict['text'] = page_text
newdict['title'] = pageid + '. ' + toc_text
self.hframe.toc.insert(parentid, 'end',
text=newdict['title'], tag='title',
value = newpageno - 1, iid = pageid)
if newpagerex.match(line):
hierarchy = line.rstrip()
toc_text = []
elif not toc_text:
toc_text = line.rstrip()
page_text = []
elif not commentrex.match(line):
if not page_text:
page_text = line
else:
page_text += line
def nextpage(self):
# Go to next page
if self.pageno < len(self.pages) - 1:
self.pageno += 1
self.redisplay()
def prevpage(self):
# Go to previous page
if self.pageno > 0:
self.pageno -= 1
self.redisplay()
def page(self, pagenum):
# Go to indicated page
if pagenum >= 0 and pagenum < len(self.pages):
self.pageno = pagenum
self.redisplay()
def close(self):
# pop down help window
self.withdraw()
def open(self):
# pop up help window
self.deiconify()
self.lift()