当前位置: 首页>>代码示例>>Python>>正文


Python Menu.get_selected_index方法代码示例

本文整理汇总了Python中menu.Menu.get_selected_index方法的典型用法代码示例。如果您正苦于以下问题:Python Menu.get_selected_index方法的具体用法?Python Menu.get_selected_index怎么用?Python Menu.get_selected_index使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在menu.Menu的用法示例。


在下文中一共展示了Menu.get_selected_index方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: SelectionMenu

# 需要导入模块: from menu import Menu [as 别名]
# 或者: from menu.Menu import get_selected_index [as 别名]
class SelectionMenu(MenuNode):
    """
    MenuNode representing a regular menu. Start returns the 
    selected MenuNode when stopped or on the press of the enter button.
    """

    def __init__(self, display, title, button_control, children,
                 disable_back=False, led_control=None):
        if not children:
            raise ValueError("Children can't be empty")
        super(self.__class__, self).__init__(display, title, button_control,
                                             children, disable_back)
        self._led_control = led_control
        self.menu = None

    def setup(self):
        self.menu = Menu([str(child) for child in self.children],
                    self.display, self.title, led_control=self._led_control,
                    blinking_leds=[self._led_control.ENTER])
        self.menu.setup()
    
    def _update(self):
        self.menu.update()
        if self._button_control.is_pressed(buttons.ENTER):
            return MenuNode.ENTER, self.menu.get_selected_index()
        elif self._button_control.is_pressed(buttons.LEFT):
            self.menu.move_selection_left()
        elif self._button_control.is_pressed(buttons.RIGHT):
            self.menu.move_selection_right()
        return MenuNode.NO_NAVIGATION, None

    def stop(self):
        self.menu.stop()
开发者ID:malcx95,项目名称:RaspPiAlarmclock,代码行数:35,代码来源:menu_node.py

示例2: AlarmApplication

# 需要导入模块: from menu import Menu [as 别名]
# 或者: from menu.Menu import get_selected_index [as 别名]
class AlarmApplication(MenuNode):
    """The menu option that manages the alarms"""

    def __init__(self, display, led_control, alarm_list, button_control):
        super(self.__class__, self).__init__(display, 'Alarms', button_control)
        self._led_control = led_control
        self._button_control = button_control
        self.alarm_list = alarm_list
        self.menu = None

    def setup(self):
        # Save the alarm list since we might have just exited an editor.
        self.alarm_list.save()

        all_alarms = self.alarm_list.get_all_alarms_with_activated()
        self.children = [AlarmEditor(self.display,
                                     self._led_control,
                                     alarm,
                                     self._button_control)
                        for alarm, _ in all_alarms]

        icons = [display.ON if activated else display.OFF
                 for _, activated in all_alarms] + ['+']
        options = self._get_options()
        self.menu = Menu(options, self.display, 
                    'Alarms', 
                    led_control=self._led_control,
                    icons=icons, 
                    blinking_leds=[self._led_control.ENTER,
                                    self._led_control.SET,
                                    self._led_control.DELETE])
        self.menu.setup()

    def _new_option_selected(self):
        return self.menu.get_selected_index() == self.alarm_list.num_alarms()

    def _update(self):
        self.menu.update()
        if self._button_control.is_pressed(buttons.ENTER):

            if self._new_option_selected():
                # insert new alarm editor at this index (the last)
                al = self._get_placeholder_alarm()
                editor = AlarmEditor(self.display, self._led_control,
                                     al, self._button_control)
                self.alarm_list.add_alarm(al, True)
                self.children.append(editor)

            return MenuNode.ENTER, self.menu.get_selected_index()

        elif self._button_control.is_pressed(buttons.RIGHT):
            self.menu.move_selection_right()
        elif self._button_control.is_pressed(buttons.LEFT):
            self.menu.move_selection_left()
        elif self._button_control.is_pressed(buttons.SET):
            self._set_pressed()
        elif self._button_control.is_pressed(buttons.DELETE):
            self._delete_pressed()
        return MenuNode.NO_NAVIGATION, None

    def _get_options(self):
        return [str(al) + ' - ON' if on else str(al) + ' - OFF'
                for al, on in 
                self.alarm_list.get_all_alarms_with_activated()] + \
                ['New alarm...']
    
    def _delete_pressed(self):
        self.menu.stop()
        all_alarms = self.alarm_list.get_all_alarms_with_activated()
        alarm, activated = all_alarms[self.menu.get_selected_index()]
        self.alarm_list.delete_alarm(alarm, activated)
        self.setup()

    def _set_pressed(self):
        all_alarms = self.alarm_list.get_all_alarms_with_activated()
        alarm, activated = all_alarms[self.menu.get_selected_index()]
        self.alarm_list.set_alarm_activated(alarm, not activated, activated)
        icon = display.ON if not activated else display.OFF
        self.menu.set_icon_at(icon, self.menu.get_selected_index())
        self.menu.update_options(self._get_options())

    def _get_placeholder_alarm(self):
        today = datetime.now()
        al = alarm.Alarm(7, 0, today.weekday(), 0)
        al.increment_weekday(1)
        return al
    
    def stop(self):
        self.menu.stop()
开发者ID:malcx95,项目名称:RaspPiAlarmclock,代码行数:91,代码来源:alarm_app.py


注:本文中的menu.Menu.get_selected_index方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。