0

I created a GUI that has many checkboxes for the user to click for options (about 28 but 7 for this example). Instead of writing it all out manually, I would like to create a loop where it auto-increments the name of the checkboxes. I think the issue comes up when I am messing with the 'self' variable.

I want to turn this:

if self.checkboard01.get() ==1:
    self.counter01 = self.counter01 + 1
    board_num = 0
    channel = 1
    self.mcc_temp = self.getTempMcc(board_num, channel)
    self.mcc_list01.append(self.mcc_temp)
    self.time_list01.append(self.counter01)
    self.mcc_temp = round(self.mcc_temp, 2)
    tkinter.Label(window, text = self.mcc_temp,bg = 'burlywood',font=("", 12), padx = 120).grid(row =8, column = 4)
    self.plot()

if self.checkboard02.get() ==1:
    self.counter02 = self.counter02 + 1
    board_num = 0
    channel = 2
    self.mcc_temp = self.getTempMcc(board_num, channel)
    self.mcc_list02.append(self.mcc_temp)
    self.time_list02.append(self.counter02)
    self.mcc_temp = round(self.mcc_temp, 2)
    tkinter.Label(window, text = self.mcc_temp,bg = 'burlywood',font=("", 12), padx = 120).grid(row =9, column = 4)
    self.plot()

if self.checkboard03.get() ==1:
    self.counter03 = self.counter03 + 1
    board_num = 0
    channel = 3
    self.mcc_temp = self.getTempMcc(board_num, channel)
    self.mcc_list03.append(self.mcc_temp)
    self.time_list03.append(self.counter03)
    self.mcc_temp = round(self.mcc_temp, 2)
    tkinter.Label(window, text = self.mcc_temp,bg = 'burlywood',font=("", 12), padx = 120).grid(row =10, column = 4)
    self.plot()

Into something like this so I can create function:

But this results in an error "SyntaxError: can't assign to function call"

for a in range(0,8):
    for e in range(7,15):
        if '{}'.format(self.checkboard0 + a).get() ==1:
            '{}'.format(self.counter0 + a) = '{}'.format(self.counter0 + a) + 1
            board_num = 0
            channel = a
            self.mcc_temp = self.getTempMcc(board_num, channel)
            self.mcc_list0a.append(self.mcc_temp)
            '{}'.format(self.time_list0+a).append('{}'.format(self.counter0+a))
            self.mcc_temp = round(self.mcc_temp, 2)
            tkinter.Label(window, text = self.mcc_temp,bg = 'burlywood',font=("", 12), padx = 120).grid(row =e, column = 4)
            self.plot()

Full GUI Code:

import tkinter
from mcculw import ul
from mcculw.enums import TempScale
from mcculw.enums import InterfaceType
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

class TempLogger:

    def __init__(self, window):

        window.title("TempLogger")


##### Create Board Labels

        tkinter.Label(window, text = "Board #0",font=("", 20), padx = 75).grid(row =4, column = 4)
        tkinter.Label(window, text = "Board #1",font=("", 20), padx = 75).grid(row =4, column = 7)
        tkinter.Label(window, text = "Board #2",font=("", 20), padx = 75).grid(row =4, column = 10)
        tkinter.Label(window, text = "Board #3",font=("", 20), padx = 75).grid(row =4, column = 13)

##### Create Enable checkboxes  

        self.checkboard0 = tkinter.IntVar()
        self.checkboard1 = tkinter.IntVar()
        self.checkboard2 = tkinter.IntVar()
        self.checkboard3 = tkinter.IntVar()
        self.en1 = tkinter.Checkbutton(window, variable = self.checkboard0, text = 'Enable',font=("", 12), onvalue = 1, offvalue = 0).grid(row = 5, column = 4)
        self.en2 = tkinter.Checkbutton(window, variable = self.checkboard1, text = 'Enable',font=("", 12), onvalue = 1, offvalue = 0).grid(row = 5, column = 7)
        self.en3 = tkinter.Checkbutton(window, variable = self.checkboard2, text = 'Enable',font=("", 12), onvalue = 1, offvalue = 0).grid(row = 5, column = 10)
        self.en4 = tkinter.Checkbutton(window, variable = self.checkboard3, text = 'Enable',font=("", 12), onvalue = 1, offvalue = 0).grid(row = 5, column = 13)

####### Create Channel labels

        for a in range(4,14,3):
            tkinter.Label(window, text = "Temp (C)",font=("", 12), pady = 15).grid(row =6, column = a)

        for e in range(3,13,3):
            tkinter.Label(window, text = "CH",font=("", 12), pady = 15).grid(row =6, column = e)
            tkinter.Label(window, text = "0",font=("", 12), pady = 25).grid(row =7, column = e)
            tkinter.Label(window, text = "1",font=("", 12), pady = 25).grid(row =8, column = e)
            tkinter.Label(window, text = "2",font=("", 12), pady = 25).grid(row =9, column = e)
            tkinter.Label(window, text = "3",font=("", 12), pady = 25).grid(row =10, column = e)
            tkinter.Label(window, text = "4",font=("", 12), pady = 25).grid(row =11, column = e)
            tkinter.Label(window, text = "5",font=("", 12), pady = 25).grid(row =12, column = e)
            tkinter.Label(window, text = "6",font=("", 12), pady = 25).grid(row =13, column = e)
            tkinter.Label(window, text = "7",font=("", 12), pady = 25).grid(row =14, column = e)

###### Create checkboxes for channels

        self.checkboard00 = tkinter.IntVar()
        self.checkboard01 = tkinter.IntVar()
        self.checkboard02 = tkinter.IntVar()
        self.checkboard03 = tkinter.IntVar()
        self.checkboard04 = tkinter.IntVar()
        self.checkboard05 = tkinter.IntVar()
        self.checkboard06 = tkinter.IntVar()
        self.checkboard07 = tkinter.IntVar()
        self.checkboard10 = tkinter.IntVar()
        self.checkboard11 = tkinter.IntVar()
        self.checkboard12 = tkinter.IntVar()
        self.checkboard13 = tkinter.IntVar()
        self.checkboard14 = tkinter.IntVar()
        self.checkboard15 = tkinter.IntVar()
        self.checkboard16 = tkinter.IntVar()
        self.checkboard17 = tkinter.IntVar()
        self.checkboard20 = tkinter.IntVar()
        self.checkboard21 = tkinter.IntVar()
        self.checkboard22 = tkinter.IntVar()
        self.checkboard23 = tkinter.IntVar()
        self.checkboard24 = tkinter.IntVar()
        self.checkboard25 = tkinter.IntVar()
        self.checkboard26 = tkinter.IntVar()
        self.checkboard27 = tkinter.IntVar()
        self.checkboard30 = tkinter.IntVar()
        self.checkboard31 = tkinter.IntVar()
        self.checkboard32 = tkinter.IntVar()
        self.checkboard33 = tkinter.IntVar()
        self.checkboard34 = tkinter.IntVar()
        self.checkboard35 = tkinter.IntVar()
        self.checkboard36 = tkinter.IntVar()
        self.checkboard37 = tkinter.IntVar()
        self.ch00 = tkinter.Checkbutton(window, variable = self.checkboard00,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 7, column = 5)
        self.ch01 = tkinter.Checkbutton(window, variable = self.checkboard01,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 8, column = 5)
        self.ch02 = tkinter.Checkbutton(window, variable = self.checkboard02,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 9, column = 5)
        self.ch03 = tkinter.Checkbutton(window, variable = self.checkboard03,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 10, column = 5)
        self.ch04 = tkinter.Checkbutton(window, variable = self.checkboard04,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 11, column = 5)
        self.ch05 = tkinter.Checkbutton(window, variable = self.checkboard05,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 12, column = 5)
        self.ch06 = tkinter.Checkbutton(window, variable = self.checkboard06,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 13, column = 5)
        self.ch07 = tkinter.Checkbutton(window, variable = self.checkboard07,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 14, column = 5)
        self.ch10 = tkinter.Checkbutton(window, variable = self.checkboard10,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 7, column = 8)
        self.ch11 = tkinter.Checkbutton(window, variable = self.checkboard11,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 8, column = 8)
        self.ch12 = tkinter.Checkbutton(window, variable = self.checkboard12,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 9, column = 8)
        self.ch13 = tkinter.Checkbutton(window, variable = self.checkboard13,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 10, column = 8)
        self.ch14 = tkinter.Checkbutton(window, variable = self.checkboard14,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 11, column = 8)
        self.ch15 = tkinter.Checkbutton(window, variable = self.checkboard15,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 12, column = 8)
        self.ch16 = tkinter.Checkbutton(window, variable = self.checkboard16,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 13, column = 8)
        self.ch17 = tkinter.Checkbutton(window, variable = self.checkboard17,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 14, column = 8)
        self.ch20 = tkinter.Checkbutton(window, variable = self.checkboard20,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 7, column = 11)
        self.ch21 = tkinter.Checkbutton(window, variable = self.checkboard21,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 8, column = 11)
        self.ch22 = tkinter.Checkbutton(window, variable = self.checkboard22,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 9, column = 11)
        self.ch23 = tkinter.Checkbutton(window, variable = self.checkboard23,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 10, column = 11)
        self.ch24 = tkinter.Checkbutton(window, variable = self.checkboard24,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 11, column = 11)
        self.ch25 = tkinter.Checkbutton(window, variable = self.checkboard25,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 12, column = 11)
        self.ch26 = tkinter.Checkbutton(window, variable = self.checkboard26,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 13, column = 11)
        self.ch27 = tkinter.Checkbutton(window, variable = self.checkboard27,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 14, column = 11)
        self.ch30 = tkinter.Checkbutton(window, variable = self.checkboard30,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 7, column = 14)
        self.ch31 = tkinter.Checkbutton(window, variable = self.checkboard31,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 8, column = 14)
        self.ch32 = tkinter.Checkbutton(window, variable = self.checkboard32,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 9, column = 14)
        self.ch33 = tkinter.Checkbutton(window, variable = self.checkboard33,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 10, column = 14)
        self.ch34 = tkinter.Checkbutton(window, variable = self.checkboard34,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 11, column = 14)
        self.ch34 = tkinter.Checkbutton(window, variable = self.checkboard35,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 12, column = 14)
        self.ch36 = tkinter.Checkbutton(window, variable = self.checkboard36,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 13, column = 14)
        self.ch37 = tkinter.Checkbutton(window, variable = self.checkboard37,font=("", 12), onvalue = 1, offvalue = 0, padx = 10, pady = 10).grid(row = 14, column = 14)

##### Create temperature placeholders

        for a in range(7,15):
            tkinter.Label(window, text = "",bg = 'burlywood',font=("", 12), padx = 170).grid(row =a, column = 4)

        for e in range(7,15):
            tkinter.Label(window, text = "",bg = 'burlywood',font=("", 12), padx = 170).grid(row =e, column = 7)

        for i in range(7,15):
            tkinter.Label(window, text = "",bg = 'burlywood',font=("", 12), padx = 170).grid(row =i, column = 10)

        for o in range(7,15):
            tkinter.Label(window, text = "",bg = 'burlywood',font=("", 12), padx = 170).grid(row =o, column = 13)

###### Create Start/Stop/Clear

        self.start_btn = tkinter.Button(window, text = "START", font=('', 12), fg = 'green', command = self.start).grid(row = 15, column = 7) 
        self.stop_btn = tkinter.Button(window, text = "STOP", font=('', 12), fg = 'red', command = self.stop).grid(row = 15, column = 8)
        self.clear_btn = tkinter.Button(window, text = "Clear Plot", font=('', 12), fg = 'red', command = self.clear).grid(row = 15, column = 4)

        tkinter.Label(window, text = 'Refresh rate in seconds',font=("", 12), padx = 30).grid(row = 15, column = 10)
        self.refresh_var = tkinter.StringVar()
        self.refresh = tkinter.Entry(window, text = 'Refresh rate in seconds', font = ('', 12), textvariable = self.refresh_var, width = 3, bg = "burlywood").grid(row = 15, column = 11)

###### Create empty lists for plotting    

        self.counter00 = -1    
        self.mcc_list00, self.time_list00 = [], []
        self.counter01 = -1    
        self.mcc_list01, self.time_list01 = [], []
        self.counter02 = -1    
        self.mcc_list02, self.time_list02 = [], []
        self.counter03 = -1    
        self.mcc_list03, self.time_list03 = [], []
        self.counter04 = -1    
        self.mcc_list04, self.time_list04 = [], []
        self.counter05 = -1    
        self.mcc_list05, self.time_list05 = [], []
        self.counter06 = -1    
        self.mcc_list06, self.time_list06 = [], []
        self.counter07 = -1    
        self.mcc_list07, self.time_list07 = [], []
        self.counter10 = -1    
        self.mcc_list10, self.time_list10 = [], []
        self.counter11 = -1    
        self.mcc_list11, self.time_list11 = [], []
        self.counter12 = -1    
        self.mcc_list12, self.time_list12 = [], []
        self.counter13 = -1    
        self.mcc_list13, self.time_list13 = [], []
        self.counter14 = -1    
        self.mcc_list14, self.time_list14 = [], []
        self.counter15 = -1    
        self.mcc_list15, self.time_list15 = [], []
        self.counter16 = -1    
        self.mcc_list16, self.time_list16 = [], []
        self.counter17 = -1    
        self.mcc_list17, self.time_list17 = [], []
        self.counter20 = -1    
        self.mcc_list20, self.time_list20 = [], []
        self.counter21 = -1    
        self.mcc_list21, self.time_list21 = [], []
        self.counter22 = -1    
        self.mcc_list22, self.time_list22 = [], []
        self.counter23 = -1    
        self.mcc_list23, self.time_list23 = [], []
        self.counter24 = -1    
        self.mcc_list24, self.time_list24 = [], []
        self.counter25 = -1    
        self.mcc_list25, self.time_list25 = [], []
        self.counter26 = -1    
        self.mcc_list26, self.time_list26 = [], []
        self.counter27 = -1    
        self.mcc_list27, self.time_list27 = [], []
        self.counter30 = -1    
        self.mcc_list30, self.time_list30 = [], []
        self.counter31 = -1    
        self.mcc_list31, self.time_list31 = [], []
        self.counter32 = -1    
        self.mcc_list32, self.time_list32 = [], []
        self.counter33 = -1    
        self.mcc_list33, self.time_list33 = [], []
        self.counter34 = -1    
        self.mcc_list34, self.time_list34 = [], []
        self.counter35 = -1    
        self.mcc_list35, self.time_list35 = [], []
        self.counter36 = -1    
        self.mcc_list36, self.time_list36 = [], []
        self.counter37 = -1    
        self.mcc_list37, self.time_list37 = [], []
        self.lgd = []

###### Create Setup functions

    def start(self):
        self.stop_button = 'False'
        self.flow()

    def stop(self):
        self.stop_button = 'True'
        return self.stop_button

    def clear(self):
        self.counter00 = -1    
        self.mcc_list00, self.time_list00 = [], []
        self.counter01 = -1    
        self.mcc_list01, self.time_list01 = [], []
        self.counter02 = -1    
        self.mcc_list02, self.time_list02 = [], []
        self.counter03 = -1    
        self.mcc_list03, self.time_list03 = [], []
        self.counter04 = -1    
        self.mcc_list04, self.time_list04 = [], []
        self.counter05 = -1    
        self.mcc_list05, self.time_list05 = [], []
        self.counter06 = -1    
        self.mcc_list06, self.time_list06 = [], []
        self.counter07 = -1    
        self.mcc_list07, self.time_list07 = [], []
        self.counter10 = -1    
        self.mcc_list10, self.time_list10 = [], []
        self.counter11 = -1    
        self.mcc_list11, self.time_list11 = [], []
        self.counter12 = -1    
        self.mcc_list12, self.time_list12 = [], []
        self.counter13 = -1    
        self.mcc_list13, self.time_list13 = [], []
        self.counter14 = -1    
        self.mcc_list14, self.time_list14 = [], []
        self.counter15 = -1    
        self.mcc_list15, self.time_list15 = [], []
        self.counter16 = -1    
        self.mcc_list16, self.time_list16 = [], []
        self.counter17 = -1    
        self.mcc_list17, self.time_list17 = [], []
        self.counter20 = -1    
        self.mcc_list20, self.time_list20 = [], []
        self.counter21 = -1    
        self.mcc_list21, self.time_list21 = [], []
        self.counter22 = -1    
        self.mcc_list22, self.time_list22 = [], []
        self.counter23 = -1    
        self.mcc_list23, self.time_list23 = [], []
        self.counter24 = -1    
        self.mcc_list24, self.time_list24 = [], []
        self.counter25 = -1    
        self.mcc_list25, self.time_list25 = [], []
        self.counter26 = -1    
        self.mcc_list26, self.time_list26 = [], []
        self.counter27 = -1    
        self.mcc_list27, self.time_list27 = [], []
        self.counter30 = -1    
        self.mcc_list30, self.time_list30 = [], []
        self.counter31 = -1    
        self.mcc_list31, self.time_list31 = [], []
        self.counter32 = -1    
        self.mcc_list32, self.time_list32 = [], []
        self.counter33 = -1    
        self.mcc_list33, self.time_list33 = [], []
        self.counter34 = -1    
        self.mcc_list34, self.time_list34 = [], []
        self.counter35 = -1    
        self.mcc_list35, self.time_list35 = [], []
        self.counter36 = -1    
        self.mcc_list36, self.time_list36 = [], []
        self.counter37 = -1    
        self.mcc_list37, self.time_list37 = [], []


    def config_first_detected_device(self, board_num):
        devices = ul.get_daq_device_inventory(InterfaceType.ANY)
        if len(devices) > 0:
            device = devices[0]
            ul.create_daq_device(board_num, device)
            return True
        return False

    def getTempMcc(self, board_num, channel):
        board_num = board_num
        channel = channel
        try:
            self.config_first_detected_device(board_num)
            value = ul.t_in(board_num, channel, TempScale.CELSIUS)
            return value
        finally:
            ul.release_daq_device(board_num)

    def plot (self):
        fig = Figure(figsize=(20,8))
        a = fig.add_subplot(111)
        graph = FigureCanvasTkAgg(fig, master = window)
        graph.get_tk_widget().grid(row = 18, column = 2,columnspan = 18, rowspan = 18, padx = 18)
        a.set_title ("Temperature Graph", fontsize=16)
        a.set_ylabel("Temp (C)", fontsize=14)
        a.set_xlabel("Time (sec)", fontsize=14)

        if self.checkboard00.get() ==1:
            a.plot(self.time_list00, self.mcc_list00, label = '00')

        if self.checkboard01.get() ==1:
            a.plot(self.time_list01, self.mcc_list01, label = '01')

        if self.checkboard02.get() ==1:
            a.plot(self.time_list02, self.mcc_list02, label = '02')

        if self.checkboard03.get() ==1:
            a.plot(self.time_list03, self.mcc_list03, label = '03')

        if self.checkboard04.get() ==1:
            a.plot(self.time_list04, self.mcc_list04, label = '04')

        if self.checkboard05.get() ==1:
            a.plot(self.time_list05, self.mcc_list05, label = '05')

        if self.checkboard06.get() ==1:
            a.plot(self.time_list06, self.mcc_list06, label = '06')

        if self.checkboard07.get() ==1:
            a.plot(self.time_list07, self.mcc_list07, label = '07')

        if self.checkboard10.get() ==1:
            a.plot(self.time_list10, self.mcc_list10, label = '10')

        if self.checkboard11.get() ==1:
            a.plot(self.time_list11, self.mcc_list11, label = '11')

        if self.checkboard12.get() ==1:
            a.plot(self.time_list12, self.mcc_list12, label = '12')

        if self.checkboard13.get() ==1:
            a.plot(self.time_list13, self.mcc_list13, label = '13')

        if self.checkboard14.get() ==1:
            a.plot(self.time_list14, self.mcc_list14, label = '14')

        if self.checkboard15.get() ==1:
            a.plot(self.time_list15, self.mcc_list15, label = '15')

        if self.checkboard16.get() ==1:
            a.plot(self.time_list16, self.mcc_list16, label = '16')

        if self.checkboard17.get() ==1:
            a.plot(self.time_list17, self.mcc_list17, label = '17')

        if self.checkboard20.get() ==1:
            a.plot(self.time_list20, self.mcc_list20, label = '20')

        if self.checkboard21.get() ==1:
            a.plot(self.time_list21, self.mcc_list21, label = '21')

        if self.checkboard22.get() ==1:
            a.plot(self.time_list22, self.mcc_list22, label = '22')

        if self.checkboard23.get() ==1:
            a.plot(self.time_list23, self.mcc_list23, label = '23')

        if self.checkboard24.get() ==1:
            a.plot(self.time_list24, self.mcc_list24, label = '24')

        if self.checkboard25.get() ==1:
            a.plot(self.time_list25, self.mcc_list25, label = '25')

        if self.checkboard26.get() ==1:
            a.plot(self.time_list26, self.mcc_list26, label = '26')

        if self.checkboard27.get() ==1:
            a.plot(self.time_list27, self.mcc_list27, label = '27')

        if self.checkboard30.get() ==1:
            a.plot(self.time_list30, self.mcc_list30, label = '30')

        if self.checkboard31.get() ==1:
            a.plot(self.time_list31, self.mcc_list31, label = '31')

        if self.checkboard32.get() ==1:
            a.plot(self.time_list32, self.mcc_list32, label = '32')

        if self.checkboard33.get() ==1:
            a.plot(self.time_list33, self.mcc_list33, label = '33')

        if self.checkboard34.get() ==1:
            a.plot(self.time_list34, self.mcc_list34, label = '34')

        if self.checkboard35.get() ==1:
            a.plot(self.time_list35, self.mcc_list35, label = '35')

        if self.checkboard36.get() ==1:
            a.plot(self.time_list36, self.mcc_list36, label = '36')

        if self.checkboard37.get() ==1:
            a.plot(self.time_list37, self.mcc_list37, label = '37')

        a.legend(loc = 'upper right')
        graph.draw()


#### Create executing function

    def flow(self):
        if self.stop_button == 'False':

            if self.checkboard0.get() == 1:

                if self.checkboard00.get() ==1:
                    self.counter00 = self.counter00 + 1
                    board_num = 0
                    channel = 0
                    self.mcc_temp = self.getTempMcc(board_num, channel)
                    self.mcc_list00.append(self.mcc_temp)
                    self.time_list00.append(self.counter00)
                    self.mcc_temp = round(self.mcc_temp, 2)
                    tkinter.Label(window, text = self.mcc_temp,bg = 'burlywood',font=("", 12), padx = 120).grid(row =7, column = 4)
                    self.plot()

                if self.checkboard01.get() ==1:
                    self.counter01 = self.counter01 + 1
                    board_num = 0
                    channel = 1
                    self.mcc_temp = self.getTempMcc(board_num, channel)
                    self.mcc_list01.append(self.mcc_temp)
                    self.time_list01.append(self.counter01)
                    self.mcc_temp = round(self.mcc_temp, 2)
                    tkinter.Label(window, text = self.mcc_temp,bg = 'burlywood',font=("", 12), padx = 120).grid(row =8, column = 4)
                    self.plot()

                if self.checkboard02.get() ==1:
                    self.counter02 = self.counter02 + 1
                    board_num = 0
                    channel = 2
                    self.mcc_temp = self.getTempMcc(board_num, channel)
                    self.mcc_list02.append(self.mcc_temp)
                    self.time_list02.append(self.counter02)
                    self.mcc_temp = round(self.mcc_temp, 2)
                    tkinter.Label(window, text = self.mcc_temp,bg = 'burlywood',font=("", 12), padx = 120).grid(row =9, column = 4)
                    self.plot()

                if self.checkboard03.get() ==1:
                    self.counter03 = self.counter03 + 1
                    board_num = 0
                    channel = 3
                    self.mcc_temp = self.getTempMcc(board_num, channel)
                    self.mcc_list03.append(self.mcc_temp)
                    self.time_list03.append(self.counter03)
                    self.mcc_temp = round(self.mcc_temp, 2)
                    tkinter.Label(window, text = self.mcc_temp,bg = 'burlywood',font=("", 12), padx = 120).grid(row =10, column = 4)
                    self.plot()

                if self.checkboard04.get() ==1:
                    self.counter04 = self.counter04 + 1
                    board_num = 0
                    channel = 4
                    self.mcc_temp = self.getTempMcc(board_num, channel)
                    self.mcc_list04.append(self.mcc_temp)
                    self.time_list04.append(self.counter04)
                    self.mcc_temp = round(self.mcc_temp, 2)
                    tkinter.Label(window, text = self.mcc_temp,bg = 'burlywood',font=("", 12), padx = 120).grid(row =11, column = 4)
                    self.plot()
        window.after(int(self.refresh_var.get()) *1, self.flow)

window = tkinter.Tk()
templogger = TempLogger(window)
window.mainloop()
18
  • 2
    It has nothing to do with self. The expression '{}'.format(self.counter0 + a) = '{}'.format(self.counter0 + a) + 1 results in a str object. It makes no sense to assign to a string object to begin with, furthermore, the compiler actually catches that you are trying to assign to a function call, which similarly makes no sense. You probably just want getattr and setattr, to dynamically retrieve/set an attribute using a strin, so attribute = 'counter0'+a then setattr(self, attribute, getattr(self, attribute) + 1) Commented Dec 5, 2019 at 22:54
  • I am having trouble following. I'm still learning python at the moment. I replaced what you suggested with I had but now I am having issues with the expression: '{}'.format(self.checkboard0 + a).get() ==1 Commented Dec 5, 2019 at 23:23
  • @Nycbros What is that expression? I don’t think that makes much sense. What do you think .format() does, exactly? Also, can you share the entirety of your program? Commented Dec 5, 2019 at 23:24
  • I was thinking that format was used kind of like declaring a variable. Before that expression I had: if self.checkboard03.get() ==1: where I would get the Boolean value of the checkbox. I even tried 'self.checkboard0{}'.format(a).get() ==1 Commented Dec 5, 2019 at 23:25
  • @Nycbros no, .format formats a str object. And since str objects don't have a .get() method, it will throw an error. str objects are not source code. And you shouldn't be dynamically generating code for this sort of thing to begin with. Note, Python doesn't have variable declarations. Commented Dec 5, 2019 at 23:27

1 Answer 1

1

You don't want to name your variables self.checkboard01, self.checkboard01, etc. Instead, store them in a list or dictionary. Then it becomes trivial to iterate over the values. This gives you one variable with 38 values rather than 38 distinct variables, making your code much easier to understand.

Since you're creating a checkerboard, using a dictionary with the row and column makes the most sense. With that, your function can look something like this:

for a in range(0,8):
    for e in range(7,15):
        widget = self.checkboard[(a,e)]
        value = checkbutton.get()
        ...

This also has the advantage of letting you create your 38 widgets far fewer lines of code:

self.checkboard = {}
self.vars = {}
for a in range(0, 8):
    for e in range(7,15):
        vars[(a,e)] = tkinter.IntVar()
        self.checkboard[(a,e)] = tkinter.Checkbutton(..., variable=vars[(a,e)])

Changing the number of buttons or changing the configuration of the buttons means you only have to change one or two lines rather than dozens.

Note: there's nothing special about using (a,e) as the key. If you prefer to use a string like "checkerboard00", you can do that as well:

for a in range(0, 8):
    for e in range(7,15):
        key = "checkerboard%s%s" % (a,e)
        self.checkeboard[key] = ...

You can then use self.checkerboard["checkerboard00"] to get the upperleft corner. For me, using a tuple is a bit easier to use than building up an encoded string. The point is to use a dictionary, a be consistent with how you define the key.

Sign up to request clarification or add additional context in comments.

11 Comments

This makes so much sense to me!
I can follow how to create a dictionary now but how can I call it or use it. Lets say I wanted the value from checkboard05?
@Nycbros: I don't know how you map checkerboard05 to a row and column coordinate. The point is, use whatever indexing makes sense for you, then use that same index everywhere: self.checkboard[(5,5)] or self.checkerboard['55'] or whatever makes the most sense.
I guess the issue I am having is getting the input value from the check box. I have self.check_vars[(0,a)] = tkinter.IntVar() self.checkboard[(0,a)] = tkinter.Checkbutton(window, variable=self.check_vars[0,a]).grid(row = e, column = 5) How do I index checkboard00... like this: self.check_vars[0,0].get() or self.check_vars[(0,0)].get()
@Nycbros If you create self.check_vars[(0,1)], then to get the value you use self.check_vars[(0,a)].get(). You're over-thinking the problem. The point is, use a dictionary, and for the key use a unique identifier. You can use (0,a) or "checkbutton00" or anything else. To me, using a tuple of (row, column) makes sense, but it doesn't have to be that way.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.