vendredi 17 juin 2016

Checkbuttons on every notebook tabs Tkinter Python

On every Tkinter notebook tab, there is a list of checkbuttons and the variables get saved to their corresponding v[ ] (i.e. cb.append(Checkbuttons(.., variables = v[x],..)).

For now, I am encountering this error:

File "/home/pass/OptionsInterface.py", line 27, in __init__
self.ntbk_render(f = self.f1, ntbkLabel="Options",cb = optsCb, msg = optMsg)
File "/home/pass/OptionsInterface.py", line 59, in ntbk_render
text = msg[x][1], command = self.cb_check(v, opt)))
File "/home/pass/OptionsInterface.py", line 46, in cb_check
opt[ix]=(v[ix].get())
IndexError: list assignment index out of range

And I think the error is coming here. I don't know how to access the values of the checkbutton variables.

def cb_check(self, v = [], cb = [], opt = []):
    for ix in range(len(cb)):
      opt[ix]=(v[ix].get())
    print opt

Here are some snippets:

     self.ntbk = ttk.Notebook(self.master)

    self.f1 = ttk.Frame(self.ntbk)
    self.f2 = ttk.Frame(self.ntbk)
    self.f3 = ttk.Frame(self.ntbk)
    self.f4 = ttk.Frame(self.ntbk)
    self.rowTracker = 0

    self.ntbk.add(self.f1, text = "Tab1")
    self.ntbk.add(self.f2, text = "Tab2")
    self.ntbk.add(self.f3, text = "Tab3")
    self.ntbk.add(self.f4, text = "Tab4")
    self.ntbk.grid()
    optMsg, pluginMsg, classifierMsg, logMsg = [], [], [], []
    optsCb, pluginCb, classCb, logCb = [],[],[],[]

    self.ntbk_render(f = self.f1, ntbkLabel="Options",cb = optsCb, msg = optMsg)
    self.ntbk_render(f = self.f2, ntbkLabel="Plugins",cb = pluginCb, msg = pluginMsg)
    self.ntbk_render(f = self.f3, ntbkLabel="Classifiers",cb = classCb, msg = classifierMsg)
    self.ntbk_render(f = self.f4, ntbkLabel="Logging",cb = logCb, msg = logMsg)

    self.btn = Button(master, text="Ok", command = self.closeWindow)
    self.btn.grid(row = self.rowTracker+1, sticky = "se")
    self.master.grid()

  def cb_check(self, v = [], cb = [], opt = []):
    for ix in range(len(cb)):
      opt[ix]=(v[ix].get())
    print opt

  def ntbk_render(self, f=None, ntbkLabel="", cb = [], msg = []):
    v = []
    opt = []
    msg = get_thug_args(word = ntbkLabel, argList = msg) #Allows to get the equivalent list (2d array) 
      #to serve as texts for their corresponding checkboxes

    for x in range(len(msg)):
      v.append(IntVar())
      off_value = 0
      on_value = 1
      cb.append(Checkbutton(f, variable = v[x], onvalue = on_value, offvalue = off_value,
        text = msg[x][1], command = self.cb_check(v, opt)))
      cb[x].grid(row=self.rowTracker + x, column=0, sticky='w')
      opt.append(off_value)
      cb[-1].deselect()

After solving the error, I want to get all the values of the checkbutton variables of each tab after pressing the button Ok at the bottom. Any tips on how to do it will help!




Aucun commentaire:

Enregistrer un commentaire