, Listbox, "" , , , .
, "" , Listbox, splitlines, , , string, , Listbox Listbox.bind('<<ListboxSelect>>', self._reselection_fxn).
class Multiline_Single_Selector(object):
def __init__(self, itemlist, ..):
lb_splitlines = self._parse_strings(itemlist)
self.my_Listbox.insert(0, *lb_splitlines)
self.my_Listbox.bind('<<ListboxSelect>>', self._reselect)
def _parse_strings(self, string_list):
'''Accepts a list of strings and breaks each string into a series of lines,
logs the sets, and stores them in the item_roster and string_register attributes.
Returns the split strings to be inserted into a Listbox.'''
self.index_sets = index_sets = []
self.string_register = register = {}
all_lines = []
line_number = 0
for item in string_list:
lines = item.splitlines()
all_lines.extend(lines)
register[line_number] = item
qty = len(lines)
if qty == 1:
index_sets.append((line_number, line_number))
else:
element_range = line_number, line_number + qty - 1
index_sets.extend([element_range] * qty)
line_number += qty
return all_lines
def _reselect(self, event=None):
"Called whenever the Listbox selection changes."
selection = self.my_Listbox.curselection()
if not selection:
return
lines_st, lines_ed = self.index_sets[selection[0]]
self.my_Listbox.selection_set(lines_st, lines_ed)
def _recall(self, event=None):
"Get the complete string for the currently selected item."
selection = self.my_Listbox.curselection()
if selection:
return self.string_register[selection[0]]
return None
Listbox, . , line-wrap \n -parsing Listbox, .
, , _recall , . None, .
, , . .