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 breaks when mixing tk.Checkbutton and ttk.Checkbutton #116484

Closed
BartolHrg opened this issue Mar 7, 2024 · 4 comments
Closed

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

BartolHrg opened this issue Mar 7, 2024 · 4 comments
Labels
topic-tkinter type-bug An unexpected behavior, bug, or error

Comments

@BartolHrg
Copy link

BartolHrg commented Mar 7, 2024

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

@BartolHrg BartolHrg added the type-bug An unexpected behavior, bug, or error label Mar 7, 2024
@terryjreedy
Copy link
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.

@serhiy-storchaka
Copy link
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.

@BartolHrg
Copy link
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).

@terryjreedy
Copy link
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.

serhiy-storchaka added a commit that referenced this issue Mar 16, 2024
…ault names (GH-116495)

Change automatically generated tkinter.Checkbutton widget names to
avoid collisions with automatically generated tkinter.ttk.Checkbutton
widget names within the same parent widget.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 16, 2024
…on default names (pythonGH-116495)

Change automatically generated tkinter.Checkbutton widget names to
avoid collisions with automatically generated tkinter.ttk.Checkbutton
widget names within the same parent widget.
(cherry picked from commit c61cb50)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 16, 2024
…on default names (pythonGH-116495)

Change automatically generated tkinter.Checkbutton widget names to
avoid collisions with automatically generated tkinter.ttk.Checkbutton
widget names within the same parent widget.
(cherry picked from commit c61cb50)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this issue Mar 16, 2024
…ton default names (GH-116495) (GH-116901)

Change automatically generated tkinter.Checkbutton widget names to
avoid collisions with automatically generated tkinter.ttk.Checkbutton
widget names within the same parent widget.
(cherry picked from commit c61cb50)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this issue Mar 16, 2024
…ton default names (GH-116495) (GH-116902)

Change automatically generated tkinter.Checkbutton widget names to
avoid collisions with automatically generated tkinter.ttk.Checkbutton
widget names within the same parent widget.
(cherry picked from commit c61cb50)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
vstinner pushed a commit to vstinner/cpython that referenced this issue Mar 20, 2024
…on default names (pythonGH-116495)

Change automatically generated tkinter.Checkbutton widget names to
avoid collisions with automatically generated tkinter.ttk.Checkbutton
widget names within the same parent widget.
adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
…on default names (pythonGH-116495)

Change automatically generated tkinter.Checkbutton widget names to
avoid collisions with automatically generated tkinter.ttk.Checkbutton
widget names within the same parent widget.
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
…on default names (pythonGH-116495)

Change automatically generated tkinter.Checkbutton widget names to
avoid collisions with automatically generated tkinter.ttk.Checkbutton
widget names within the same parent widget.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-tkinter type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants