Skip to content

tkinter breaks when mixing tk.Checkbutton and ttk.Checkbutton #116484

Closed

Description

@BartolHrg

Bug report

Bug description:

When mixing tk.Checkbutton and ttk.Checkbutton (and maybe custom class Checkbutton)
tkinter behaves weirdly (calling method of one object changes the other too).
It is because of name collision.

import tkinter as tk
from tkinter import ttk
wn = tk.Tk()
tk.Checkbutton(wn)
ttk.Checkbutton(wn)
print(tk.Checkbutton(wn).winfo_name())
print(ttk.Checkbutton(wn).winfo_name())

this example prints

!checkbutton2
!checkbutton2

This happens because of tk.Checkbutton._setup overriden method
which is not called in ttk.Checkbutton.
I think that that method can be erased, which would solve the problem,
but I'm not sure why it is there in the first place.

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs

Activity

  1. added
    type-bug
    on Mar 7, 2024
  2. terryjreedy commented on Mar 8, 2024

    @terryjreedy
    Member

    If you do not pass a name to the widget constructor, tkinter generates a default name, which is the lowercase widget name plus sequence number for that widget class, starting with '1'. Since those are the second widgets for each class, the names are correct; no bug. If you delete the first tk Checkbutton and add a 2nd ttk Checkbutton before the print, you will see 1 and 3. I think this should be closed. @serhiy-storchaka ?

    If you pack a widget in a frame packed in root and print the widget rather than its specific .winfo_name, you get a dotted name like .!frame.!checkbutton2. If you need to use both tk and ttk widgets of the same type in the same window or frame and need to use the names of both types of widgets, which together is very rare, you can pass name='whatever' to the calls for one type or both.

  3. serhiy-storchaka commented on Mar 8, 2024

    @serhiy-storchaka
    Member

    Thank you for your report @BartolHrg. It shows the flaw in the solution for #73588.

    Checkbutton and ttk.Checkbutton use different naming code for default widget names. The latter uses the common naming code: children names are unique withing the parent widget. But the former needs globally unique widgets.

    The solution is to use different templates for generating names, so the results are different even if the class name and the counter are the same.

  4. BartolHrg commented on Mar 9, 2024

    @BartolHrg
    Author

    @terryjreedy generated names are not based directly on class but on the previous equal names.
    e.g. if you had class Checkbutton(tk.Frame): ... you could use that class together with ttk.Checkbutton with no problems
    (BaseWidget._setup generates a name and, if it already exists, adds a number at the end)

    Another solution, since the method tk.Checkbutton._setup can't be erased, could be to make a similar method in ttk.Checkbutton.
    However, that still doesn't solve the issue if someone makes a custom class named Checkbutton (that can be solved by renaming the class).

  5. terryjreedy commented on Mar 15, 2024

    @terryjreedy
    Member

    class Checkbutton(tk.Checkbutton): would inherit Checkbutton._setup, as revised.
    class Checkbutton(tk.Widget); would inherit BaseWidget._setup and clash unless it defined its own non-clashing ._setup. The patch adds an explanation for the necessity of doing this. Anyone replacing a built-in class should read its code.

  6. added a commit that references this issue on Mar 16, 2024
  7. added 2 commits that reference this issue on Mar 16, 2024
  8. added 2 commits that reference this issue on Mar 16, 2024
  9. added a commit that references this issue on Mar 20, 2024
  10. added a commit that references this issue on Mar 25, 2024
  11. added a commit that references this issue on Apr 17, 2024
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

    topic-tkintertype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions