Revision e7ba6b9e examples/consoles.py

View differences:

examples/consoles.py
17 17
class Console( Frame ):
18 18
    "A simple console on a host."
19 19
       
20
    def __init__( self, parent, net, node, height=10, width=32 ):
20
    def __init__( self, parent, net, node, height=10, width=32, title='Node' ):
21 21
        Frame.__init__( self, parent )
22 22

  
23 23
        self.net = net
24 24
        self.node = node
25 25
        self.prompt = node.name + '# '
26
        self.height, self.width = height, width
26
        self.height, self.width, self.title = height, width, title
27 27
                
28 28
        # Initialize widget styles
29 29
        self.buttonStyle = { 'font': 'Monaco 7' }
......
48 48
    def makeWidgets( self ):
49 49
        "Make a label, a text area, and a scroll bar."
50 50

  
51
        def newTerm( net=self.net, node=self.node ):
51
        def newTerm( net=self.net, node=self.node, title=self.title ):
52 52
            "Pop up a new terminal window for a node."
53
            net.terms += makeTerms( [ node ] )
53
            net.terms += makeTerms( [ node ], title )
54 54
        label = Button( self, text=self.node.name, command=newTerm, 
55 55
            **self.buttonStyle )
56 56
        label.pack( side='top', fill='x' )
......
63 63

  
64 64
    def bindEvents( self ):
65 65
        "Bind keyboard and file events."
66
        # The text widget handles regular key presses, but we
67
        # use special handlers for the following:
66 68
        self.text.bind( '<Return>', self.handleReturn )
67 69
        self.text.bind( '<Control-c>', self.handleInt )
68
        # self.text.bind( '<KeyPress>', self.handleKey )
69 70
        # This is not well-documented, but it is the correct
70 71
        # way to trigger a file event handler from Tk's
71 72
        # event loop!
......
78 79
        self.text.mark_set( 'insert', 'end' )
79 80
        self.text.see( 'insert' )
80 81
    
81
    def handleKey( self, event  ):
82
        "Handle a regular key press."
83
        self.append( event.char )
84
    
85 82
    def handleReturn( self, event ):
86 83
        "Handle a carriage return."
87 84
        cmd = self.text.get( 'insert linestart', 'insert lineend' )
......
104 101
        data = self.node.monitor()
105 102
        self.append( data )
106 103
        if not self.node.waiting:
107
            # Print prompt, just for the heck of it
104
            # Print prompt
108 105
            self.append( self.prompt )
109 106
            
110 107
    def waitOutput( self ):
......
128 125
        self.menubar = self.createMenuBar()
129 126
        cframe = self.cframe = Frame( self )
130 127
        self.consoles = {}  # consoles themselves
131
        for name in 'hosts', 'switches', 'controllers':
128
        titles = { 
129
            'hosts': 'Host', 
130
            'switches': 'Switch',
131
            'controllers': 'Controller'
132
        }
133
        for name in titles:
132 134
            nodes = getattr( net, name )
133
            frame, consoles = self.createConsoles( cframe, nodes, width )
135
            frame, consoles = self.createConsoles( 
136
                cframe, nodes, width, titles[ name ] )
134 137
            self.consoles[ name ] = Object( frame=frame, consoles=consoles )
135 138
        self.selected = None
136 139
        self.select( 'hosts' )
......
140 143
        # Close window gracefully
141 144
        Wm.wm_protocol( self.top, name='WM_DELETE_WINDOW', func=self.quit )
142 145
        
143
    def createConsoles( self, parent, nodes, width ):
146
    def createConsoles( self, parent, nodes, width, title ):
144 147
        "Create a grid of consoles in a frame."
145 148
        f = Frame( parent )
146 149
        # Create consoles
147 150
        consoles = []
148 151
        index = 0
149 152
        for node in nodes:
150
            console = Console( f, net, node )
153
            console = Console( f, net, node, title=title )
151 154
            consoles.append( console )
152 155
            row = index / width
153 156
            column = index % width

Also available in: Unified diff