Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/file/2125782780c6/sandbox/ttk.py
Дата изменения: Unknown
Дата индексирования: Mon Feb 4 01:17:59 2013
Кодировка:
allpy: 2125782780c6 sandbox/ttk.py

allpy

view sandbox/ttk.py @ 653:2125782780c6

50% speed up for blocks_finder due to local application of remove_contained_preblocks. For every preblock that is going to be extended by all the groups, I first check that the resulting preblocks don't overlap. Due to this local check we get slight advantage in speed.
author Boris Burkov <BurkovBA@gmail.com>
date Sun, 12 Jun 2011 15:36:07 +0400
parents ef6692b5ffbc
children 61e5d8e146c7
line source
1 from tkinter import ttk, filedialog
2 import tkinter
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'
19 result = None
20 for widget in self._attached_widgets:
21 result = getattr(widget, command)(*args, **kw)
22 return result
24 def _set(self, sender, *args, **kw):
25 command = self._orient + 'view'
26 self.set(*args, **kw)
27 for widget in self._attached_widgets:
28 if widget is sender:
29 continue
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'
51 return result
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):
65 self.tag_add(
66 'vselection',
67 '%s.%s' % (line, pos0),
68 '%s.%s' % (line, pos1)
69 )
70 return "break"
72 def _select_end(self, ev):
73 self._select(ev)
74 del self._selection
75 return "break"
77 def open_file():
78 global seqs
79 names.delete(0, 'end')
80 sequences.delete('1.0', 'end')
82 filename = filedialog.askopenfilename()
83 seqs = set()
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))
94 def colorize():
95 length = len(list(seqs)[0][1])
96 num_seqs = len(seqs)
97 seq_weights = []
98 for pos in range(length):
99 weights = {}
100 for name, seq, _ in seqs:
101 char = seq[pos]
102 weights[char] = weights.get(char, 0) + 1
103 for char in weights:
104 weights[char] = weights[char] * 10 // num_seqs
105 seq_weights.append(weights)
107 for name, seq, line in seqs:
108 for<