view sandbox/ttk.py @ 822:d87129162eb4
Implemented & tested new markup API. See #95
1) Sequences, Alignment and Blocks now have two new methods:
- add_markup(name, markup_class=optional, **kwargs=optional)
- remove_markup(name)
name refers to the same name as in aln.markups[name] or sequence[i].name
It is now explicitly denied to create markups any other way.
2) Markups now have `remove()` method that means 'release all memory that would
not be released otherwised, if we just remove markup from the dictionary'. For
sequences markups it removes markup attribute from each monomer.
3) Added necessary del sequence_markup[monomer] method.
4) Many base classes have attribute `kind`; for Alignments and Blocks it is
'alignment', for Sequences it is 'sequence' for AlignmentMarkups it is
'alignment_markup' for SequenceMarkups it is 'sequence_markup'. This attribute
is crucial for new alignment construction API.
5) Common stuff for MarkupContainers (Alignments and Sequences) is in
MarkupContainerMixin.
author |
Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru> |
date |
Fri, 15 Jul 2011 16:43:03 +0400 |
parents |
4e6e85851133 |
children |
|
line source
1 from tkinter import ttk, filedialog
4 class Scrollbar(tkinter.Scrollbar):
6 def __init__(self, *args, **kw):
7 tkinter.Scrollbar.__init__(self, *args, **kw)
8 self['command'] = self._command
9 self._attached_widgets = set()
10 self._orient = {'horizontal': 'x', 'vertical': 'y'}[self['orient']]
12 def attach(self, widget):
13 self._attached_widgets.add(widget)
14 command = self._orient + 'scrollcommand'
15 widget[command] = lambda *args, **kw: self._set(widget, *args, **kw)
17 def _command(self, *args, **kw):
18 command = self._orient + 'view'
20 for widget in self._attached_widgets:
21 result = getattr(widget, command)(*args, **kw)
24 def _set(self, sender, *args, **kw):
25 command = self._orient + 'view'
27 for widget in self._attached_widgets:
30 getattr(widget, command)('moveto', args[0])
32 class MegaText(tkinter.Text):
34 def __init__(self, *args, **kw):
35 tkinter.Text.__init__(self, *args, **kw)
37 self.bind("<B1-Motion>", self._select)
38 self.bind("<B1-ButtonRelease>", self._select_end)
39 self.tag_configure('vselection', borderwidth=1, relief="solid", background='#ffbbbb')
41 def tag_clear(self, tag):
42 """Remove the tag from anywhere in the text."""
43 ranges = self.tag_ranges(tag)
44 for begin, end in zip(ranges[::2], ranges[1::2]):
45 self.tag_remove(tag, begin, end)
47 def insert(self, *args, **kw):
48 self['state'] = 'normal'
49 result = tkinter.Text.insert(self, *args, **kw)
50 self['state'] = 'disabled'
53 def _select (self, ev):
54 index = self.index("@%s,%s" % (ev.x, ev.y))
55 line, pos = map(int, index.split('.'))
56 if not hasattr(self, '_selection'):
57 self._selection = [line, line, pos, pos]
58 self._selection[1] = line
59 self._selection[3] = pos
61 self.tag_clear('vselection')
62 line0, line1 = sorted(self._selection[0:2])
63 pos0, pos1 = sorted(self._selection[2:4])
64 for line in range(line0, line1 + 1):
67 '%s.%s' % (line, pos0),
68 '%s.%s' % (line, pos1)
72 def _select_end(self, ev):
79 names.delete(0, 'end')
80 sequences.delete('1.0', 'end')
82 filename = filedialog.askopenfilename()
84 for item in open(filename).read().split('\n>'):
85 lines = item.split('\n')
86 name = lines[0].lstrip('>').strip()
87 body = "".join(map(str.strip, lines[1:]))
89 names.insert('end', name)
90 sequences.insert('end', body+'\n')
91 line = int(sequences.index('end - 1 line').split('.')[0])
92 seqs.add((name, body, line))
95 length = len(list(seqs)[0][1])
98 for pos in range(length):
100 for name, seq, _ in seqs:
102 weights[char] = weights.get(char, 0) + 1
104 weights[char] = weights[char] * 10 // num_seqs
105 seq_weights.append(weights)
107 for name, seq, line in seqs:
108 for pos in range(len(seq)):
110 tag = 'id%d0' % seq_weights[pos].get(char, 0)
111 sequences.tag_add(tag, '%d.%d' % (line, pos))
113 for id in range(10+1):
114 w = (10 - id) * 256 / 10
115 color = '#%02x%02x%02x' % (w, w, w)
116 sequences.tag_configure('id%d0' % id, background=color)
120 root.option_add('*tearOff', False)
121 menu = tkinter.Menu(root)
122 menu_file = tkinter.Menu(menu)
123 menu_file.add_command(label='Open', command=open_file)
124 menu.add_cascade(menu=menu_file, label='File')
125 menu.add_command(label='Colorize', command=colorize)
128 main = ttk.PanedWindow(root, orient="horizontal")
129 names = tkinter.Listbox(main)
130 sequences = MegaText(main)
132 sequences['font'] = names['font'] = "Courier 10"
135 sequences.tag_clear('sel')
136 for line in names.curselection():
138 sequences.tag_add('sel', "%s.0" % line, "%s.end" % line)
139 sequences['inactiveselectbackground'] = 'blue'
140 names.bind('<<ListboxSelect>>', _select)
142 #sequences['spacing3'] = 1
143 sequences['exportselection'] = False
144 sequences['wrap'] = 'none'
145 #names['selectborderwidth'] = -1
146 names['highlightthickness'] = 0
147 names['activestyle'] = 'none'
148 names['exportselection'] = False
152 main.pack(side='right', fill='both', expand=True)
154 vscroll = Scrollbar(root, orient='vertical')
155 vscroll.attach(names)
156 vscroll.attach(sequences)
157 vscroll.pack(side='right', fill='y')