python - Tkinter checkboxes created in loop -


i'm working on first tkinter project, , have used several stackoverflow answers , explanations (and other links lead to) basis trying understand how build application.

i structured application after reading question (and of links accepted answer): switch between 2 frames in tkinter

in 1 of frames, need create checkboxes using loop. found page helpful: how create multiple checkboxes list in loop in python tkinter

i having hard time getting of checkboxes checked default (my desired behavior).

the relevant part of code follows (python2.7):

import tkinter tk  class main(tk.tk):     def __init__(self, *args, **kwargs):         tk.tk.__init__(self, *args, **kwargs)         ...         ## gets set on different frame in application         self.files_list = []  class a(tk.frame):     def __init__(self, parent, controller):         tk.frame.__init__(self, parent)         self.controller = controller         ...         ## self.f_list contains values (a list of dictionaries) expecting on frame         self.f_list = controller.files_list          f in self.f_list:             self.file_name = tk.stringvar()             self.file_name.set(f['file'])             self.run_file = tk.intvar()             self.run_file.set(1)             cb = tk.checkbutton(self, text=self.file_name.get(), variable=self.run_file)             cb.pack() 

this produces list expect of "file names", each checkbox. however, last checkbox produced loop checked when run.

before calling pack method, put print statement print self.run_file.get() , each time through loop prints value of 1.

i tried changing loop several different ways :

## same behavior self.run_file = tk.variable() self.run_file.set(1)   ## same behavior self.run_file = tk.intvar(value=1)  ## no checkboxes set cb = tk.checkbutton(self, text=self.file_name.get(), variable=self.run_file.get()) 

i feel since value of self.file_name different each time through loop there no issue there. since last checkbox checked default, makes me feel value getting lost on previous checkboxes don't know how structure checkboxes or self.run_file variable each box checked default. i'm using self on variables in loop after reading question: python, tkinter : if there way check checkboxes default?

i have looked @ numerous different questions around topic, but, still can't come correct answer. can point me in right direction?

your problem self.run_file being overwritten @ each iteration in for loop. make sure intvar of each checkbox not overwritten, store them separately, example in list:

self.run_file_intvars = []  i, f in enumerate(self.f_list):     self.run_file_intvars.append(tk.intvar(value=1))     cb = tk.checkbutton(self, text=f['file'],                          variable=self.run_file_intvars[i])     cb.pack() 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -