示例#1
0
class MenuDemo(object):

    def __init__(self):
        self._frame = urwid.Frame(urwid.Filler(urwid.Text([
             ("\n\n"
             "  ████████   ██                    ██                            ██   ██████  ██       ██\n"
             " ██░░░░░░   ░██                   ░██                           ░██  ██░░░░██░██      ░██\n"
             "░██        ██████  ██████   ██████░██        ██████  ██████     ░██ ██    ░░ ░██      ░██\n"
             "░█████████░░░██░  ░░░░░░██ ░░██░░█░██       ██░░░░██░░██░░█  ██████░██       ░██      ░██\n"
             "░░░░░░░░██  ░██    ███████  ░██ ░ ░██      ░██   ░██ ░██ ░  ██░░░██░██       ░██      ░██\n"
             "       ░██  ░██   ██░░░░██  ░██   ░██      ░██   ░██ ░██   ░██  ░██░░██    ██░██      ░██\n"
             " ████████   ░░██ ░░████████░███   ░████████░░██████ ░███   ░░██████ ░░██████ ░████████░██\n"
             "░░░░░░░░     ░░   ░░░░░░░░ ░░░    ░░░░░░░░  ░░░░░░  ░░░     ░░░░░░   ░░░░░░  ░░░░░░░░ ░░ \n"
             "StarLordCLI :: version 0.0.1                                                 ")
            ], align='center')))
        self.loop = urwid.MainLoop(self._frame, const_PALETTE, unhandled_input=self.keyHandler)
        # Creation du menu
        self._frame.set_header(self.buildMenu())

        # creation de la bar de status
        self.status_bar = urwid.AttrWrap(urwid.Text("Type help for instructions, :q to quit."), "status_bar")
        self.command_prompt = CommandPrompt(self)
        self.footer = urwid.Pile([urwid.AttrMap(self.status_bar, 'footer'), self.command_prompt])
        self._frame.set_footer(self.footer)

        # Focus sur le menu
        self._frame.set_focus('header')

    def run(self):
        self.loop.run()

    def _messageExit(self, message):
        # We print the menu data in the middle of the screen
        new_widget = urwid.Filler(urwid.Text(message, align='center'))
        self._frame.set_body(new_widget)
        self.loop.draw_screen()
        self._frame.set_focus('header')


    def menu_cb(self, menu_data):
        self._messageExit("Menu selected: %s/%s" % menu_data)

    def exit_cb(self, menu_data):
        self._messageExit("Exiting throught 'Quitter' menu item")
        time.sleep(1)
        raise urwid.ExitMainLoop()

    def buildMenu(self):
        """Construction du menu"""
        self.menu = Menu(self.loop)
        _menu1 = "Fichier"
        self.menu.addMenu(_menu1, "Help", self.menu_cb) # Adding a menu is quite easy
        self.menu.addMenu(_menu1, "Item 2", self.menu_cb) # Here the callback is always the same,
        self.menu.addMenu(_menu1, "Item 3", self.menu_cb) # but you use different ones in real life :)
        self.menu.addMenu(_menu1, "Quitter (C-x)", self.exit_cb, 'ctrl x') # You can also add a shortcut
        _menu2 = "Menu 2"
        self.menu.addMenu(_menu2, "Item 1", self.menu_cb)
        self.menu.addMenu(_menu2, "Item 2", self.menu_cb)
        self.menu.addMenu(_menu2, "Item 3", self.menu_cb)
        return self.menu

    def keyHandler(self, input):
        """We leave if user press a quit char"""
        #if input in ('esc','q','Q'):
        #    raise urwid.ExitMainLoop()
        if input == ':':
            self._frame.set_focus('footer')
            self.command_prompt.set_caption(':')
        if input == '/':
            self._frame.set_focus('footer')
            self.command_prompt.set_caption('/')
        # add change focus on tab key
        if input == 'tab':
            foc = self._frame.get_focus()
            if foc == 'header':
                self._frame.set_focus('body')
            if foc == 'body':
                self._frame.set_focus('header')
        else:
            return self.menu.checkShortcuts(input) #needed to manage shortcuts
示例#2
0
class MainMenu(object):

    def __init__(self, App):
        self._App = App
        
    def build(self):
        self._Menu = Menu(self._App.loop)
        
        menu1 = "File"
        self._Menu.addMenu(menu1, "Load Trajectory", self.choiceLoadTrajectory)
        self._Menu.addMenu(menu1, "Exit (Ctrl+X)", self.choiceExit, 'ctrl x')
        menu2 = "Trajectory"
        self._Menu.addMenu(menu2, "Play", self.choicePlayTrajectory)
        self._Menu.addMenu(menu2, "Next", self.choiceNextPosition) 
        self._Menu.addMenu(menu2, "Previous", self.choicePrevPosition)
        self._Menu.addMenu(menu2, "Rewind", self.choiceRewindTrajectory)
        menu3 = "Help"
        self._Menu.addMenu(menu3, "ROS", self.menu_cb)
        self._Menu.addMenu(menu3, "About App", self.menu_cb)

        return self._Menu

    def choiceLoadTrajectory(self, menu_data):
        new_widget = MyFileDialog(self._App)
        self._App.loop.widget = new_widget  
        self._App.bottom_bar.addMessage("Trajectory loaded.")
        
    def choicePlayTrajectory(self, menu_data):
        self._App._showMessage("Executing trajectory.")
        self._App.bottom_bar.addMessage("Waiting.")
        
        global _tp
        _tp.playTrajectory()
        
        self._App.bottom_bar.addMessage("Ready.")
        
    def choiceNextPosition(self, menu_data):
        global _tp, _conn
        p = _tp._Trajectory.nextPosition()
        _conn.sendPosition(p)
        
    def choicePrevPosition(self, menu_data):
        global _tp, _conn
        p = _tp._Trajectory.previousPosition()
        _conn.sendPosition(p)
            
    def choiceRewindTrajectory(self, menu_data):
        global _tp
        _tp._Trajectory.rewind()

    def choiceExit(self, menu_data): 
        global _conn
        _conn.stop()
        self._App._exit()
        
    def menu_cb(self, menu_data):
        self._App._showMessage("Menu selected: %s/%s" % menu_data)
示例#3
0
class MenuDemo(object):
    def __init__(self):
        _frame = urwid.Frame(
            urwid.Filler(urwid.Text('Menu demo', align='center')))
        self.loop = urwid.MainLoop(_frame,
                                   const_PALETTE,
                                   unhandled_input=self.keyHandler)
        _frame.set_header(self.buildMenu())
        _frame.set_focus('header')

    def run(self):
        self.loop.run()

    def _messageExit(self, message):
        #We print the menu data in the middle of the screen
        new_widget = urwid.Filler(urwid.Text(message, align='center'))
        self.loop.widget = new_widget
        self.loop.draw_screen()
        #5 seconds pause
        time.sleep(5)
        #see you
        raise urwid.ExitMainLoop()

    def menu_cb(self, menu_data):
        self._messageExit("Menu selected: %s/%s" % menu_data)

    def exit_cb(self, menu_data):
        self._messageExit("Exiting throught 'Exit' menu item")

    def buildMenu(self):
        self.menu = Menu(self.loop)
        _menu1 = "Menu 1"
        self.menu.addMenu(_menu1, "Item 1",
                          self.menu_cb)  #Adding a menu is quite easy
        self.menu.addMenu(_menu1, "Item 2",
                          self.menu_cb)  #Here the callback is always the same,
        self.menu.addMenu(
            _menu1, "Item 3",
            self.menu_cb)  #but you use different ones in real life :)
        self.menu.addMenu(_menu1, "Exit (C-x)", self.exit_cb,
                          'ctrl x')  #You can also add a shortcut
        _menu2 = "Menu 2"
        self.menu.addMenu(_menu2, "Item 1", self.menu_cb)
        self.menu.addMenu(_menu2, "Item 2", self.menu_cb)
        self.menu.addMenu(_menu2, "Item 3", self.menu_cb)
        return self.menu

    def keyHandler(self, input):
        """We leave if user press a quit char"""
        if input in ('esc', 'q', 'Q'):
            raise urwid.ExitMainLoop()
        else:
            return self.menu.checkShortcuts(input)  #needed to manage shortcuts