I have two separate columns of checkbuttons in tkinter in python with identical labels.
My goal is to disable those in one of the columns when the same item is selected in another column.
I can do this outside of classes and functions, but am having trouble calling the variables within the functions.
When I plug in static values for the dynamic parameters and remove the loop on the command function creation to disable the buttons, it works perfectly. Whenever I use the loop, however, I receive an "AttributeError: 'tool' object has no attribute 'dsblr0'" response.
How can I access the loop-created functions, such that I can disable the boxes in the second column whenever the same items are checked in the first column?
Any help is greatly appreciated!
My current code:
from tkinter import *
from tkinter import messagebox
buttonnames = []
class tool(Frame):
def get_button_names(self):
self.buttonnames=['b1','b2','b3','b4','b5']
global buttonnames
for item in self.buttonnames:
buttonnames.append(item)
def __init__(self, parent):
'''
Constructor
'''
Frame.__init__(self, parent)
self.parent=parent
self.get_button_names()
self.display_new_window()
for i in range(len(buttonnames)):
exec('''def dsblr{0}(self):
self.bcb{0}.config(state=DISABLED if self.var{0}.get() else NORMAL)'''.format(i,))
def display_new_window(self):
""" Transpose tools
"""
self.parent.title("Tool")
self.parent.grid_rowconfigure(0, weight=1)
self.parent.grid_columnconfigure(0, weight=1)
for i,column in enumerate(self.buttonnames):
exec('self.var{0}=IntVar()'.format(i,))
exec('self.bvar{0}=IntVar()'.format(i,))
exec('self.cb{0} = Checkbutton(self.parent, text=column, variable=self.var{0},command=self.dsblr{0})'.format(i,))
exec("self.cb{0}.grid(row=i+1,column=0,sticky='w')".format(i,))
exec('self.bcb{0} = Checkbutton(self.parent, text=column, variable=self.bvar{0})'.format(i,))
exec("self.bcb{0}.grid(row=i+1,column=1,sticky='w')".format(i,))
def main():
root=Tk()
d=tool(root)
root.mainloop()
if __name__=="__main__":
main()