Exemplo n.º 1
0
    def add_child(self, key, json, **kwargs):
        font = kwargs.get('font', json.get('font', None))

        if isinstance(font, dict):
            font = Font(family=font.get('family'),
                        size=font.get('size'),
                        weight=font.get('weight', 'normal'),
                        slant=font.get('slant', 'roman'),
                        underline=font.get('underline', False),
                        overstrike=font.get('overstrike', False))

        kwargs = {
            'bd':
            kwargs.get('bd', json.get('bd', self.cget('bd'))),
            'font':
            font,
            'index':
            kwargs.get('index', json.get('index', self.next_index)),
            'tearoff':
            kwargs.get('tearoff', json.get('tearoff', 0)),
            'foreground':
            kwargs.get('foreground',
                       json.get('foreground', self.cget('foreground'))),
            'background':
            kwargs.get('background',
                       json.get('background', self.cget('background'))),
            'activeforeground':
            kwargs.get(
                'activeforeground',
                json.get('activeforeground', self.cget('activeforeground'))),
            'activebackground':
            kwargs.get(
                'activebackground',
                json.get('activebackground', self.cget('activebackground'))),
        }

        func = SubMenu if 'children' in json else MenuItem
        self.items[key] = func(self, json, **kwargs)

        if self.tearoff is None:
            self.tearoff = 0

        index = self.items[key].index
        if index is not None:
            for _, child in self.get_children.items():
                if child.index >= index:
                    child.index += 1
            self.items[key].index = index
Exemplo n.º 2
0
class MenuItem:
    def __init__(self, parent, json, **kwargs):
        self.parent = parent
        self.has_children = False

        self.font = json.get('font')
        self.type = json.get('type', 'item')
        self.label = json.get('label')
        self.state = json.get('state')
        self.value = json.get('value')
        self.index = kwargs.get('index')
        self.command = json.get('command')
        self.onvalue = json.get('onvalue', True)
        self.offvalue = json.get('offvalue', False)
        self.variable = json.get('variable')
        self.compound = json.get('compound', 'left')
        self.underline = json.get('underline', 0)
        self.foreground = json.get('foreground')
        self.background = json.get('background')
        self.accelerator = json.get('accelerator')
        self.activeforeground = json.get('activeforeground')
        self.activebackground = json.get('activebackground')

        if isinstance(self.font, dict):
            self.font = Font(family=self.font.get('family'),
                             size=self.font.get('size'),
                             weight=self.font.get('weight', 'normal'),
                             slant=self.font.get('slant', 'roman'),
                             underline=self.font.get('underline', False),
                             overstrike=self.font.get('overstrike', False))
        self.image = PhotoImage(
            file=path.join(getcwd(), 'tkmenus', 'images',
                           json.get('image', 'blank-21x16.png')))

        kwargs = {
            'font': self.font,
            'label': self.label,
            'state': self.state,
            'index': self.index,
            'command': self.command,
            'variable': self.variable,
            'compound': self.compound,
            'underline': self.underline,
            'foreground': self.foreground,
            'background': self.background,
            'accelerator': self.accelerator,
            'activeforeground': self.activeforeground,
            'activebackground': self.activebackground,
        }

        if self.command:
            cmd = self.command.split('.')

            if len(cmd) == 1:
                file = 'main'
                command = cmd[0]
            else:
                file, command = cmd

            module = import_module(file)
            self.command = getattr(module, command, None)

        menu_type = json.get('type')
        if menu_type == 'separator':
            func = parent.insert_separator if self.index is not None else parent.add_separator
            func(index=self.index, background=self.background)
        elif menu_type == 'checkbutton':
            func = parent.insert_checkbutton if self.index is not None else parent.add_checkbutton
            func(**{
                **kwargs,
                **{
                    'onvalue': self.onvalue,
                    'offvalue': self.offvalue
                }
            })
        elif menu_type == 'radiobutton':
            func = parent.insert_radiobutton if self.index is not None else parent.add_radiobutton
            func(**{**kwargs, **{'value': self.value}})
        else:
            func = parent.insert_command if self.index is not None else parent.add_command
            func(**{
                **kwargs,
                **{
                    'image': self.image,
                    'command': self.command
                }
            })

    def cget(self, attribute):
        return getattr(self, attribute, None)

    def config(self, **kwargs):
        for attribute, value in kwargs.items():
            setattr(self, attribute, value)

        if self.index == 0 and self.parent.tearoff:
            self.index = 1

        args = dict(kwargs)
        args.pop('bd', None)
        args.pop('relief', None)
        if self.type != 'separator':
            self.parent.entryconfig(self.index, **args)
        else:
            self.parent.entryconfig(self.index,
                                    background=kwargs.get('background'))