Прежде всего, полученный вами вывод на печать не является кодом ошибки. Если вы печатаете объект IntVar
напрямую, вы получаете такое значение, но если вы хотите напечатать значение, хранящееся в IntVar
, вам нужно вызвать метод get()
.
Теперь, чтобы получить подробный формат вывода, как вы и просили, вы можете использовать список таких списков для каждого оператора.
class Application(Frame):
def __init__(self, master=None):
self.createWidgets()
def createWidgets(self):
operator = int(6) #assigned for testing
lookingFor = ['op1','op2','op3','op4'] #assigned for testing
self.checkList = [[] for idx in range(operator)] #creating an empty list of lists to store Checkbox variables
cx,cy=0,0
for x in range(0, operator): ##creating left Lables
cy+=1
cx+=1
label = Label(root)
labelName = 'Operator#'
label["text"] = str(labelName)+str(cy)
label.grid(row=cx+1,column=0)
cx,cy=0,0
for y in lookingFor: ##creating top labels
cy+=1
label = Label(root)
label["text"] = str(y)
label.grid(row=cx+1,column=cy)
cx,cy=0,0
for y in range(0, operator):
cx+=1
for x in lookingFor: #creating checkboxes
var = IntVar()
c = Checkbutton(root, variable = var)
cy+=1
c.grid(row=cx+1,column=cy)
self.checkList[y].append(var)
cy=0
self.button = Button(root)
self.button["text"] = 'Done'
self.button["command"] = self.states
self.button.grid(row=0,column=0)
def states(self):
#This function should print out the state of each button
#as well as its corresponding 'Op#' and 'operator#'
#So the program can later decide which operations are performed
#when the program passes through each operator
for i, lst in enumerate(self.checkList, 1):
for j, op in enumerate(lst, 1):
print("Variable status = {}, Op{}, operator#{}".format(op.get(), j, i))
Это печатает вывод как:
Variable status = 1, Op1, operator#1
Variable status = 0, Op2, operator#1
Variable status = 0, Op3, operator#1
Variable status = 0, Op4, operator#1
Variable status = 0, Op1, operator#2
Variable status = 1, Op2, operator#2
Variable status = 0, Op3, operator#2
Variable status = 0, Op4, operator#2
Variable status = 0, Op1, operator#3
Variable status = 0, Op2, operator#3
Variable status = 0, Op3, operator#3
Variable status = 1, Op4, operator#3
Variable status = 0, Op1, operator#4
Variable status = 0, Op2, operator#4
Variable status = 0, Op3, operator#4
Variable status = 1, Op4, operator#4
Variable status = 0, Op1, operator#5
Variable status = 0, Op2, operator#5
Variable status = 0, Op3, operator#5
Variable status = 0, Op4, operator#5
Variable status = 0, Op1, operator#6
Variable status = 1, Op2, operator#6
Variable status = 0, Op3, operator#6
Variable status = 0, Op4, operator#6