<s> from tkinter import * <EOL> class ScrolledList : <EOL> default = "<STR_LIT>" <EOL> def __init__ ( self , master , ** options ) : <EOL> self . master = master <EOL> self . frame = frame = Frame ( master ) <EOL> self . frame . pack ( fill = "<STR_LIT>" , expand = <NUM_LIT:1> ) <EOL> self . vbar = vbar = Scrollbar ( frame , name = "<STR_LIT>" ) <EOL> self . vbar . pack ( side = "<STR_LIT:right>" , fill = "<STR_LIT:y>" ) <EOL> self . listbox = listbox = Listbox ( frame , exportselection = <NUM_LIT:0> , <EOL> background = "<STR_LIT>" ) <EOL> if options : <EOL> listbox . configure ( options ) <EOL> listbox . pack ( expand = <NUM_LIT:1> , fill = "<STR_LIT>" ) <EOL> vbar [ "<STR_LIT>" ] = listbox . yview <EOL> listbox [ "<STR_LIT>" ] = vbar . set <EOL> listbox . bind ( "<STR_LIT>" , self . click_event ) <EOL> listbox . bind ( "<STR_LIT>" , self . double_click_event ) <EOL> listbox . bind ( "<STR_LIT>" , self . popup_event ) <EOL> listbox . bind ( "<STR_LIT>" , self . up_event ) <EOL> listbox . bind ( "<STR_LIT>" , self . down_event ) <EOL> self . clear ( ) <EOL> def close ( self ) : <EOL> self . frame . destroy ( ) <EOL> def clear ( self ) : <EOL> self . listbox . delete ( <NUM_LIT:0> , "<STR_LIT:end>" ) <EOL> self . empty = <NUM_LIT:1> <EOL> self . listbox . insert ( "<STR_LIT:end>" , self . default ) <EOL> def append ( self , item ) : <EOL> if self . empty : <EOL> self . listbox . delete ( <NUM_LIT:0> , "<STR_LIT:end>" ) <EOL> self . empty = <NUM_LIT:0> <EOL> self . listbox . insert ( "<STR_LIT:end>" , str ( item ) ) <EOL> def get ( self , index ) : <EOL> return self . listbox . get ( index ) <EOL> def click_event ( self , event ) : <EOL> self . listbox . activate ( "<STR_LIT>" % ( event . x , event . y ) ) <EOL> index = self . listbox . index ( "<STR_LIT>" ) <EOL> self . select ( index ) <EOL> self . on_select ( index ) <EOL> return "<STR_LIT>" <EOL> def double_click_event ( self , event ) : <EOL> index = self . listbox . index ( "<STR_LIT>" ) <EOL> self . select ( index ) <EOL> self . on_double ( index ) <EOL> return "<STR_LIT>" <EOL> menu = None <EOL> def popup_event ( self , event ) : <EOL> if not self . menu : <EOL> self . make_menu ( ) <EOL> menu = self . menu <EOL> self . listbox . activate ( "<STR_LIT>" % ( event . x , event . y ) ) <EOL> index = self . listbox . index ( "<STR_LIT>" ) <EOL> self . select ( index ) <EOL> menu . tk_popup ( event . x_root , event . y_root ) <EOL> def make_menu ( self ) : <EOL> menu = Menu ( self . listbox , tearoff = <NUM_LIT:0> ) <EOL> self . menu = menu <EOL> self . fill_menu ( ) <EOL> def up_event ( self , event ) : <EOL> index = self . listbox . index ( "<STR_LIT>" ) <EOL> if self . listbox . selection_includes ( index ) : <EOL> index = index - <NUM_LIT:1> <EOL> else : <EOL> index = self . listbox . size ( ) - <NUM_LIT:1> <EOL> if index < <NUM_LIT:0> : <EOL> self . listbox . bell ( ) <EOL> else : <EOL> self . select ( index ) <EOL> self . on_select ( index ) <EOL> return "<STR_LIT>" <EOL> def down_event ( self , event ) : <EOL> index = self . listbox . index ( "<STR_LIT>" ) <EOL> if self . listbox . selection_includes ( index ) : <EOL> index = index + <NUM_LIT:1> <EOL> else : <EOL> index = <NUM_LIT:0> <EOL> if index >= self . listbox . size ( ) : <EOL> self . listbox . bell ( ) <EOL> else : <EOL> self . select ( index ) <EOL> self . on_select ( index ) <EOL> return "<STR_LIT>" <EOL> def select ( self , index ) : <EOL> self . listbox . focus_set ( ) <EOL> self . listbox . activate ( index ) <EOL> self . listbox . selection_clear ( <NUM_LIT:0> , "<STR_LIT:end>" ) <EOL> self . listbox . selection_set ( index ) <EOL> self . listbox . see ( index ) <EOL> def fill_menu ( self ) : <EOL> pass <EOL> def on_select ( self , index ) : <EOL> pass <EOL> def on_double ( self , index ) : <EOL> pass <EOL> def test ( ) : <EOL> root = Tk ( ) <EOL> root . protocol ( "<STR_LIT>" , root . destroy ) <EOL> class MyScrolledList ( ScrolledList ) : <EOL> def fill_menu ( self ) : self . menu . add_command ( label = "<STR_LIT>" ) <EOL> def on_select ( self , index ) : print ( "<STR_LIT>" , self . get ( index ) ) <EOL> def on_double ( self , index ) : print ( "<STR_LIT>" , self . get ( index ) ) <EOL> s = MyScrolledList ( root ) <EOL> for i in range ( <NUM_LIT:30> ) : <EOL> s . append ( "<STR_LIT>" % i ) <EOL> return root <EOL> def main ( ) : <EOL> root = test ( ) <EOL> root . mainloop ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
