New submission from Javier Dehesa:

When you build a Tkinter interface with variables, if tkinter.Tk.mainloop is 
called from a different function that the one creating the variable objects, 
then in some cases the widgets associated with the variables will not be set to 
the right value. I have not been able to find a consistent pattern, but I have 
some examples.

The following script works correctly:
```
import tkinter as tk
from tkinter import ttk

def make_var_cb(root):
    v = tk.BooleanVar(root, True)
    cb = ttk.Checkbutton(root, text='Checkbutton', variable=v)
    cb.pack()
    root.mainloop()

if __name__ == '__main__':
    root = tk.Tk()
    make_var_cb(root)
```

But the following does not (the result is shown in the attached image):
```
import tkinter as tk
from tkinter import ttk

def make_var_cb(root):
    v = tk.BooleanVar(root, True)
    cb = ttk.Checkbutton(root, text='Checkbutton', variable=v)
    cb.pack()

if __name__ == '__main__':
    root = tk.Tk()
    make_var_cb(root)
    root.mainloop()
```

However, the following _does_ work again:
```

def make_var(root):
    return tk.BooleanVar(root, True)

def make_cb(root, v):
    return ttk.Checkbutton(root, text='Checkbutton', variable=v)

if __name__ == '__main__':
    root = tk.Tk()
    v = make_var(root)
    cb = make_cb(root, v)
    cb.pack()
    root.mainloop()
```

----------
components: Tkinter, Windows
files: checkbutton_bad.png
messages: 296100
nosy: Javier Dehesa, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: Widget variable binding does not work if mainloop is called from a 
different function
type: behavior
versions: Python 3.5
Added file: http://bugs.python.org/file46954/checkbutton_bad.png

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue30678>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to