Search code examples
pythonpython-3.xobjecttkinterrepr

Rename object internal repr, name // tkinter problem


How can I rename the internal repr/name for an object? So for example <tkinter.Checkbutton object .!notebook2.!frame.!checkbutton> to <tkinter.Checkbutton object .!notebook2.!frame.!checkbutton01>.

I need to do that cause I have the problem that tkinter detects two checkboxes as one and the same. It shows both of them but if I click one of both it also toggles the other one and if I have a ~obj_name~.select() it toggles both of them.

The two checkboxes:

<tkinter.Checkbutton object .!checkbutton>
<tkinter.Checkbutton object .!notebook2.!frame.!checkbutton>

Code:

from tkinter import *
from tkinter import ttk

win = win = Tk()
win.minsize(950, 450)
win.maxsize(950, 450)

chb = []
shop_chb = []

chb.append(Checkbutton(win, text="test123"))
chb[0].place(x=830, y=10)

tabs_setting = ttk.Notebook(win, width=925, height=60)
tabs_setting.place(x=10, y=310)
tab_mp = Frame(tabs_setting, width=500, height=275, bg="lightgrey")
tab_mp.place(x=2, y=4)
tabs_setting.add(tab_mp, text="Marktplätze")

shop_chb.append(Checkbutton(tab_mp, text=test678))
shop_chb[0].grid(column=0, row=0)

win.mainloop()

This is of curse only a really simple model of the problem. But it shows the problem. If you toggle one checkbox both get toggled. That's why I had the idea to rename the internal object name / repr. Is that possible or is there another solution to this problem?


Solution

  • What you are asking for is not the proper solution to the problem you are having.

    Checkbuttons are designed to be associated with one of the special tkinter variables (StringVar, IntVar, etc). If you don't give one, tkinter will create one for you using the name of the widget. Since you didn't give your widgets an explicit name, the last part of the widget named is "!checkbutton", and that's the name that tkinter uses for the variable. Thus, both checkbuttons end up sharing the same variable.

    If you were to give your checkbuttons variables, your problem will go away.

    ...
    var1 = IntVar(value=0)
    chb.append(Checkbutton(win, text="test123", variable=var1))
    ...
    var2 = IntVar(value=0)
    shop_chb.append(Checkbutton(tab_mp, text="test678", variable=var2))
    ...