diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py index ee2415d..4726eff 100644 --- a/Lib/tkinter/__init__.py +++ b/Lib/tkinter/__init__.py @@ -2283,7 +2283,7 @@ class BaseWidget(Misc): if kw: cnf = _cnfmerge((cnf, kw)) self.widgetName = widgetName - BaseWidget._setup(self, master, cnf) + self._setup(master, cnf) if self._tclCommands is None: self._tclCommands = [] classes = [(k, v) for k, v in cnf.items() if isinstance(k, type)] @@ -2638,6 +2638,12 @@ class Checkbutton(Widget): selectcolor, selectimage, state, takefocus, text, textvariable, underline, variable, width, wraplength.""" Widget.__init__(self, master, 'checkbutton', cnf, kw) + + def _setup(self, master, cnf): + super()._setup(master, cnf) + if 'variable' not in cnf: + cnf['variable'] = '%s-%d' % (self._name, id(self)) + def deselect(self): """Put the button in off-state.""" self.tk.call(self._w, 'deselect') diff --git a/Lib/tkinter/dialog.py b/Lib/tkinter/dialog.py index f61c5f7..c2c10fa 100644 --- a/Lib/tkinter/dialog.py +++ b/Lib/tkinter/dialog.py @@ -10,7 +10,7 @@ class Dialog(Widget): def __init__(self, master=None, cnf={}, **kw): cnf = _cnfmerge((cnf, kw)) self.widgetName = '__dialog__' - Widget._setup(self, master, cnf) + self._setup(master, cnf) self.num = self.tk.getint( self.tk.call( 'tk_dialog', self._w, diff --git a/Lib/tkinter/test/test_tkinter/test_widgets.py b/Lib/tkinter/test/test_tkinter/test_widgets.py index 81b52ea..48f5827 100644 --- a/Lib/tkinter/test/test_tkinter/test_widgets.py +++ b/Lib/tkinter/test/test_tkinter/test_widgets.py @@ -212,6 +212,19 @@ class CheckbuttonTest(AbstractLabelTest, unittest.TestCase): widget = self.create() self.checkParams(widget, 'onvalue', 1, 2.3, '', 'any string') + def test_unique_variables(self): + frames = [] + buttons = [] + for i in range(2): + f = tkinter.Frame(self.root) + f.pack() + frames.append(f) + for j in 'AB': + b = tkinter.Checkbutton(f, text=j) + b.pack() + buttons.append(b) + variables = [str(b['variable']) for b in buttons] + self.assertEqual(len(set(variables)), 4, variables) @add_standard_options(StandardOptionsTests) class RadiobuttonTest(AbstractLabelTest, unittest.TestCase): diff --git a/Lib/tkinter/tix.py b/Lib/tkinter/tix.py index d9c097a..b08037f 100644 --- a/Lib/tkinter/tix.py +++ b/Lib/tkinter/tix.py @@ -303,7 +303,7 @@ class TixWidget(tkinter.Widget): del cnf[k] self.widgetName = widgetName - Widget._setup(self, master, cnf) + self._setup(master, cnf) # If widgetName is None, this is a dummy creation call where the # corresponding Tk widget has already been created by Tix