Thanks to visit codestin.com
Credit goes to github.com

Skip to content

tkinter OptionMenu conform to standard widget options, #101284

Description

@AceScottie

The current implementation of tkinter.OptionMenu creates the widget in a manner different from all other tkinter widgets.

def __init__(self, master, variable, value, *values, **kwargs):

Here is a proposed method to deal with this widget.

    def __init__(self, master=None, cnf={}, **kw):
        """Construct an optionmenu widget with the parent MASTER,
        Supports kwarg configurations from MenuButton.
        """
        values = kw.pop('values', [])
        value = kw.pop('value', None)
        kw['borderwidth'] = kw.get('borderwidth', 2)
        variable = kw['textvariable'] = kw.pop('variable', StringVar())
        variable.set(value)
        kw['indicatoron'] = kw.get('indicatoron', 1)
        kw['relief'] = kw.get('relief', RAISED)
        kw['anchor'] = kw.get('anchor', "c")
        kw['highlightthickness'] = kw.get('highlightthickness', 2)
        callback = kw.pop('command', None)

        Widget.__init__(self, master, "menubutton", cnf, kw)
        self.widgetName = 'tk_optionMenu'
        menu = self.__menu = Menu(self, name="menu", tearoff=0)
        self.menuname = menu._w
        menu.add_command(label=value,
                 command=_setit(variable, value, callback))
        for v in values:
            menu.add_command(label=v,
                     command=_setit(variable, v, callback))
        self["menu"] = menu

I have replace the kw dict with dict.pop and dict.get methods to allow users to override the standard values while keeping the style the same.
I have also conformed the init args to be the same as standard widgets using master, cnf and kw with default values.

This should be a drop in replacement to the OptionMenu.init method.
(FYI i dont know how to do PRs yet and the doc string probably needs editing.)

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-tkintertype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions