在一个类中增加'self'变量

问题描述 投票:-1回答:1

我创建了一个GUI,该GUI有许多复选框供用户单击以选择选项(在本示例中为28,但为7)。我想创建一个循环,在该循环中自动增加复选框的名称,而不是手动将其全部写入。我认为当我弄混“自我”变量时出现了问题。

我想转这个:

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()

变成这样,我可以创建函数:

但是这会导致错误“ SyntaxError:无法分配给函数调用”

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()

完整的GUI代码:

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()
python oop format increment
1个回答
0
投票

您不想命名变量self.checkboard01self.checkboard01等。而是将其存储在列表或字典中。然后,遍历这些值变得微不足道。这为您提供了一个具有38个值的变量,而不是38个不同的变量,这使您的代码更易于理解。

由于您正在创建棋盘格,因此最好使用带有行和列的字典。这样,您的函数可以看起来像这样:

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

这还具有使您创建38个小部件的代码行少得多的优点:

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)])

更改按钮的数量或更改按钮的配置意味着您只需要更改一两行,而无需更改数十行。

© www.soinside.com 2019 - 2024. All rights reserved.