Closed
Labels
Description
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
- gh-116484: Fix collisions between Checkbutton and ttk.Checkbutton default names #116495
- [3.12] gh-116484: Fix collisions between Checkbutton and ttk.Checkbutton default names (GH-116495) #116901
- [3.11] gh-116484: Fix collisions between Checkbutton and ttk.Checkbutton default names (GH-116495) #116902
Activity
terryjreedy commentedon Mar 8, 2024
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 passname='whatever'to the calls for one type or both.serhiy-storchaka commentedon Mar 8, 2024
Thank you for your report @BartolHrg. It shows the flaw in the solution for #73588.
Checkbuttonandttk.Checkbuttonuse 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 commentedon Mar 9, 2024
@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 withttk.Checkbuttonwith no problems(
BaseWidget._setupgenerates a name and, if it already exists, adds a number at the end)Another solution, since the method
tk.Checkbutton._setupcan't be erased, could be to make a similar method inttk.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 commentedon Mar 15, 2024
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.gh-116484: Fix collisions between Checkbutton and ttk.Checkbutton def…
pythongh-116484: Fix collisions between Checkbutton and ttk.Checkbutt…
pythongh-116484: Fix collisions between Checkbutton and ttk.Checkbutt…
[3.12] gh-116484: Fix collisions between Checkbutton and ttk.Checkbut…
[3.11] gh-116484: Fix collisions between Checkbutton and ttk.Checkbut…
pythongh-116484: Fix collisions between Checkbutton and ttk.Checkbutt…
pythongh-116484: Fix collisions between Checkbutton and ttk.Checkbutt…
pythongh-116484: Fix collisions between Checkbutton and ttk.Checkbutt…