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

ttk.OptionMenu radiobuttons change variable value twice #89323

Closed
fhdrsdg mannequin opened this issue Sep 10, 2021 · 10 comments
Closed

ttk.OptionMenu radiobuttons change variable value twice #89323

fhdrsdg mannequin opened this issue Sep 10, 2021 · 10 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 bug and security fixes topic-tkinter type-bug An unexpected behavior, bug, or error

Comments

@fhdrsdg
Copy link
Mannequin

fhdrsdg mannequin commented Sep 10, 2021

BPO 45160
Nosy @ambv, @serhiy-storchaka, @miss-islington, @E-Paine
PRs
  • bpo-45160: Ttk optionmenu only set variable once #28291
  • [3.10] bpo-45160: Ttk optionmenu only set variable once (GH-28291) #29131
  • [3.9] bpo-45160: Ttk optionmenu only set variable once (GH-28291) #29132
  • bpo-45160: Fix refleak in test_ttk_guionly introduced in GH-28291 #29416
  • [3.9] bpo-45160: Fix refleak in test_ttk_guionly introduced in GH-28291 (GH-29416) #29420
  • [3.10] bpo-45160: Fix refleak in test_ttk_guionly introduced in GH-28291 (GH-29416) #29421
  • bpo-45160: Use non-deprecated methods for tracing #29425
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-10-21.21:03:34.062>
    created_at = <Date 2021-09-10.07:13:00.307>
    labels = ['type-bug', 'expert-tkinter', '3.9', '3.10', '3.11']
    title = 'ttk.OptionMenu radiobuttons change variable value twice'
    updated_at = <Date 2021-11-05.13:32:53.142>
    user = 'https://bugs.python.org/fhdrsdg'

    bugs.python.org fields:

    activity = <Date 2021-11-05.13:32:53.142>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-10-21.21:03:34.062>
    closer = 'lukasz.langa'
    components = ['Tkinter']
    creation = <Date 2021-09-10.07:13:00.307>
    creator = 'fhdrsdg'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45160
    keywords = ['patch']
    message_count = 10.0
    messages = ['401557', '401567', '401634', '404649', '404657', '404658', '404660', '405770', '405778', '405779']
    nosy_count = 5.0
    nosy_names = ['lukasz.langa', 'serhiy.storchaka', 'miss-islington', 'fhdrsdg', 'epaine']
    pr_nums = ['28291', '29131', '29132', '29416', '29420', '29421', '29425']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue45160'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @fhdrsdg
    Copy link
    Mannequin Author

    fhdrsdg mannequin commented Sep 10, 2021

    Found when trying to answer this SO question: https://stackoverflow.com/questions/53171384/tkinter-function-repeats-itself-twice-when-ttk-widgets-are-engaged

    The ttk.OptionMenu uses radiobuttons for the dropdown menu, whereas the tkinter.OptionMenu uses commands. Both of them use _setit as command, presumably first used for tkinter and then copied to the ttk implementation. The _setit does two things: changing the associated variable and calling the associated callback function. This is needed for the tkinter.OptionMenu since commands don't support changing a variable.

    However, for the ttk.OptionMenu an additional reference to the variable was added to the radiobutton following this issue: https://bugs.python.org/issue25684. This was needed to group radiobuttons, but now leads to the variable being changed twice: once by the radiobutton and once through _setit. When tracing the variable this leads to the tracing callback being called twice on only one change of the OptionMenu.

    The solution is to not use _setit for the radiobutton command but instead just use command=self._callback

    @fhdrsdg fhdrsdg mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.10 only security fixes 3.11 bug and security fixes 3.9 only security fixes topic-tkinter type-bug An unexpected behavior, bug, or error labels Sep 10, 2021
    @E-Paine
    Copy link
    Mannequin

    E-Paine mannequin commented Sep 10, 2021

    Thank you for reporting this issue (and doing your research!). I don't think we could use command=self._callback since this wouldn't pass the new value, but something like command=lambda val=val: self._callback(val) would work perfectly. Would you like to open a pull request for this (with accompanying news entry and test), or would you rather I did it?

    @fhdrsdg
    Copy link
    Mannequin Author

    fhdrsdg mannequin commented Sep 11, 2021

    Ah yes, you're right that `command=self._callback` doesn't pass the new value like it should.
    Your suggestion does, but throws a "TypeError: 'NoneType' object is not callable" when no command is given when creating the OptionMenu. The current `_setit` implementation uses a `if self.__callback:` check for that.

    Should we do something like this?

    for val in values:
        menu.add_radiobutton(label=val,
            variable=self._variable)
        if self._callback:
            menu.entryconfigure('last', command=lambda val=val: self._callback(val))

    I don't have any experience making pull requests. I might look into it at some point but for now it would be great if you could make it.

    @ambv
    Copy link
    Contributor

    ambv commented Oct 21, 2021

    New changeset add46f8 by E-Paine in branch 'main':
    bpo-45160: Ttk optionmenu only set variable once (GH-28291)
    add46f8

    @ambv
    Copy link
    Contributor

    ambv commented Oct 21, 2021

    New changeset 04485ac by Miss Islington (bot) in branch '3.9':
    bpo-45160: Ttk optionmenu only set variable once (GH-28291) (GH-29132)
    04485ac

    @ambv
    Copy link
    Contributor

    ambv commented Oct 21, 2021

    New changeset 98f157d by Miss Islington (bot) in branch '3.10':
    bpo-45160: Ttk optionmenu only set variable once (GH-28291) (GH-29131)
    98f157d

    @ambv ambv removed 3.7 (EOL) end of life 3.8 only security fixes labels Oct 21, 2021
    @ambv ambv closed this as completed Oct 21, 2021
    @ambv ambv removed 3.7 (EOL) end of life 3.8 only security fixes labels Oct 21, 2021
    @ambv ambv closed this as completed Oct 21, 2021
    @ambv
    Copy link
    Contributor

    ambv commented Oct 21, 2021

    Thanks for the fix, E. Paine, and fhdrsdg for the report! ✨ 🍰 ✨

    @ambv
    Copy link
    Contributor

    ambv commented Nov 5, 2021

    New changeset 54d1e3f by Łukasz Langa in branch 'main':
    bpo-45160: Fix refleak in test_ttk_guionly introduced in #72478 (GH-29416)
    54d1e3f

    @ambv
    Copy link
    Contributor

    ambv commented Nov 5, 2021

    New changeset 276a3a6 by Miss Islington (bot) in branch '3.9':
    bpo-45160: Fix refleak in test_ttk_guionly introduced in #72478 (GH-29416) (GH-29420)
    276a3a6

    @ambv
    Copy link
    Contributor

    ambv commented Nov 5, 2021

    New changeset 099a94f by Miss Islington (bot) in branch '3.10':
    bpo-45160: Fix refleak in test_ttk_guionly introduced in #72478 (GH-29416) (GH-29421)
    099a94f

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 bug and security fixes topic-tkinter type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant