Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tkinter OptionMenu conform to standard widget options, #101284

Open
AceScottie opened this issue Jan 24, 2023 · 0 comments
Open

tkinter OptionMenu conform to standard widget options, #101284

AceScottie opened this issue Jan 24, 2023 · 0 comments
Labels
expert-tkinter stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@AceScottie
Copy link

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.)

@AceScottie AceScottie added the type-feature A feature request or enhancement label Jan 24, 2023
@AceScottie AceScottie changed the title tkinter OptionMeny conform to standard widget options, tkinter OptionMenu conform to standard widget options, Jan 24, 2023
@arhadthedev arhadthedev added stdlib Python modules in the Lib dir expert-tkinter labels Feb 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
expert-tkinter stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants