同时运行更多循环?

时间:2016-09-22 06:33:28

标签: python multithreading python-2.7 loops tkinter

首先,我不得不说我是Python的初学者。这是我创建课程的第一步。现在我的问题。On that picture,我提出了我的代码应该运作的内容。

  1. 启动程序(运行)
  2. 检查应该打开哪些仪器。有三种 可能性,您可以为一个,两个或所有三个单位供电 同时。 (电力#A / B / C)
  3. 此外,每增加一个功率单元,您就可以开启 温度传感器(温度#A,B,C)
  4. 取决于运行Loop#A,#B,#C或 例如两个单位#B,#C或者也许全部三个。这实际上是我的问题和问题。怎么能同时也在 同时,运行两个甚至三个循环?
  5. 我希望你能给我一些建议,帮助我解决问题。

    下面我将放置我的代码,以便您可以参考并支付注意我可以在代码中更改或改进的内容。

    import Tkinter
    import visa, time, datetime
    from ttk import *
    from tkFileDialog import *
    import tkMessageBox, os
    import numpy as np
    
    rm = visa.ResourceManager()
    inst = rm.list_resources()
    
    class Interface:
    
        def __init__(self, parent):
            self.parent = parent
            parent.title("Cycling")
            local_directory = os.path.dirname(os.path.realpath(__file__))
            self.dataname = "/does/not/exist"
    
        # Frame
            self.frame_instruments = Tkinter.Frame(parent, bg='', colormap='new')
            self.frame_settings = Tkinter.Frame(parent, bg='', colormap='new')
            self.frame_image = Tkinter.Frame(parent, bg='', colormap='new')
    
        # Labelframe
            self.lframe_instrumet_a = Tkinter.LabelFrame(self.frame_instruments, text="Choose an Instruments - #A", padx=8, pady=9)
            self.lframe_settings_a = Tkinter.LabelFrame(self.frame_instruments, text="Settings - #A", padx=7, pady=11)
    
            self.lframe_instrumet_b = Tkinter.LabelFrame(self.frame_instruments, text="Choose an Instruments - #B", padx=8, pady=9)
            self.lframe_settings_b = Tkinter.LabelFrame(self.frame_instruments, text="Settings - #B", padx=7, pady=11)
    
            self.lframe_instrumet_c = Tkinter.LabelFrame(self.frame_instruments, text="Choose an Instruments - #C", padx=8, pady=9)
            self.lframe_settings_c = Tkinter.LabelFrame(self.frame_instruments, text="Settings - #C", padx=7, pady=11)
    
        # Combobox
            #A
            self.choices_power_supply_a_var = Tkinter.StringVar()
            self.choices_power_supply_a_var.set(inst[0])
            self.combo_power_supply_a = Combobox(self.lframe_instrumet_a, values=inst, textvariable=self.choices_power_supply_a_var)
    
            self.choices_multimeter_a_var = Tkinter.StringVar()
            self.choices_multimeter_a_var.set(inst[0])
            self.combo_multimeter_a = Combobox(self.lframe_instrumet_a, values=inst, textvariable=self.choices_multimeter_a_var)
    
            #B
            self.choices_power_supply_b_var = Tkinter.StringVar()
            self.choices_power_supply_b_var.set(inst[0])
            self.combo_power_supply_b = Combobox(self.lframe_instrumet_b, values=inst, textvariable=self.choices_power_supply_b_var)
    
            self.choices_multimeter_b_var = Tkinter.StringVar()
            self.choices_multimeter_b_var.set(inst[0])
            self.combo_multimeter_b = Combobox(self.lframe_instrumet_b, values=inst, textvariable=self.choices_multimeter_b_var)
    
            #C
            self.choices_power_supply_c_var = Tkinter.StringVar()
            self.choices_power_supply_c_var.set(inst[0])
            self.combo_power_supply_c = Combobox(self.lframe_instrumet_c, values=inst, textvariable=self.choices_power_supply_c_var)
    
            self.choices_multimeter_c_var = Tkinter.StringVar()
            self.choices_multimeter_c_var.set(inst[0])
            self.combo_multimeter_c = Combobox(self.lframe_instrumet_c, values=inst, textvariable=self.choices_multimeter_c_var)
    
        # Menu
            menu = Tkinter.Menu(parent, tearoff=0)
            parent.config(menu=menu)
            filemenu = Tkinter.Menu(parent, tearoff=0)
            menu.add_cascade(label='File', menu=filemenu)
            filemenu.add_command(label='Create New File...', command=lambda: self.save_file())
            helpmenu = Tkinter.Menu(menu, tearoff=0)
            menu.add_cascade(label='?', menu=helpmenu)
            helpmenu.add_command(label='About', command=lambda: self.about())
    
        # Label
            #A
            self.label_power_supply_a = Tkinter.Label(self.lframe_instrumet_a, text="Power Supply 2200: ")
            self.label_multimeter_a = Tkinter.Label(self.lframe_instrumet_a, text="Multimeter 2700: ")
            self.label_voltage_range_a = Tkinter.Label(self.lframe_settings_a, text='Voltage Range [V]')
            self.label_over_voltage_a = Tkinter.Label(self.lframe_settings_a, text='Over Voltage Protection [V]')
            self.label_voltage_set_a = Tkinter.Label(self.lframe_settings_a, text='Set Voltage [V]')
            self.label_heat_set_a = Tkinter.Label(self.lframe_settings_a, text='Heat.Set [A]')
            self.label_meas_set_a = Tkinter.Label(self.lframe_settings_a, text='Meas. Set [A]')
            self.label_set_time_a = Tkinter.Label(self.lframe_settings_a, text='Time Limit [s]')
            self.label_set_delay_a = Tkinter.Label(self.lframe_settings_a, text='Measurement Delay [s]')
            self.label_set_repeat_a = Tkinter.Label(self.lframe_settings_a, text='Repeat')
    
            #B
            self.label_power_supply_b = Tkinter.Label(self.lframe_instrumet_b, text="Power Supply 2200: ")
            self.label_multimeter_b = Tkinter.Label(self.lframe_instrumet_b, text="Multimeter 2700: ")
            self.label_voltage_range_b = Tkinter.Label(self.lframe_settings_b, text='Voltage Range [V]')
            self.label_over_voltage_b = Tkinter.Label(self.lframe_settings_b, text='Over Voltage Protection [V]')
            self.label_voltage_set_b = Tkinter.Label(self.lframe_settings_b, text='Set Voltage [V]')
            self.label_heat_set_b = Tkinter.Label(self.lframe_settings_b, text='Heat.Set [A]')
            self.label_meas_set_b = Tkinter.Label(self.lframe_settings_b, text='Meas. Set [A]')
            self.label_set_time_b = Tkinter.Label(self.lframe_settings_b, text='Time Limit [s]')
            self.label_set_delay_b = Tkinter.Label(self.lframe_settings_b, text='Measurement Delay [s]')
            self.label_set_repeat_b = Tkinter.Label(self.lframe_settings_b, text='Repeat')
    
            #C
            self.label_power_supply_c = Tkinter.Label(self.lframe_instrumet_c, text="Power Supply 2200: ")
            self.label_multimeter_c = Tkinter.Label(self.lframe_instrumet_c, text="Multimeter 2700: ")
            self.label_voltage_range_c = Tkinter.Label(self.lframe_settings_c, text='Voltage Range [V]')
            self.label_over_voltage_c = Tkinter.Label(self.lframe_settings_c, text='Over Voltage Protection [V]')
            self.label_voltage_set_c = Tkinter.Label(self.lframe_settings_c, text='Set Voltage [V]')
            self.label_heat_set_c = Tkinter.Label(self.lframe_settings_c, text='Heat.Set [A]')
            self.label_meas_set_c = Tkinter.Label(self.lframe_settings_c, text='Meas. Set [A]')
            self.label_set_time_c = Tkinter.Label(self.lframe_settings_c, text='Time Limit [s]')
            self.label_set_delay_c = Tkinter.Label(self.lframe_settings_c, text='Measurement Delay [s]')
            self.label_set_repeat_c = Tkinter.Label(self.lframe_settings_c, text='Repeat')
    
        # Checkbutton
            #A
            self.temp_sensor_a_var = Tkinter.IntVar()
            self.check_temp_sensor_a = Checkbutton(self.lframe_instrumet_a, text="Temperatursensor - #A", variable=self.temp_sensor_a_var,
                                                   onvalue=1, offvalue=0)
            self.instrument_var_a = Tkinter.IntVar()
            self.check_instrument_a = Checkbutton(self.lframe_settings_a, text="Instrument - #A", variable=self.instrument_var_a,
                                                   onvalue=1, offvalue=0)
            #B
            self.temp_sensor_b_var = Tkinter.IntVar()
            self.check_temp_sensor_b = Checkbutton(self.lframe_instrumet_b, text="Temperatursensor - #B", variable=self.temp_sensor_b_var,
                                                   onvalue=1, offvalue=0)
            self.instrument_var_b = Tkinter.IntVar()
            self.check_instrument_b = Checkbutton(self.lframe_settings_b, text="Instrument - #B", variable=self.instrument_var_b,
                                                   onvalue=1, offvalue=0)
            #C
            self.temp_sensor_c_var = Tkinter.IntVar()
            self.check_temp_sensor_c = Checkbutton(self.lframe_instrumet_c, text="Temperatursensor - #C", variable=self.temp_sensor_c_var,
                                                   onvalue=1, offvalue=0)
            self.instrument_var_c = Tkinter.IntVar()
            self.check_instrument_c = Checkbutton(self.lframe_settings_c, text="Instrument - #C", variable=self.instrument_var_c,
                                                   onvalue=1, offvalue=0)
        # Entry/Spinbox
            #A
            self.voltage_range_a_var = Tkinter.IntVar()
            self.spin_voltage_range_a = Tkinter.Spinbox(self.lframe_settings_a, textvariable=self.voltage_range_a_var, width=5,
                                                        from_=0, to=30, justify='right')
            self.over_voltage_a_var = Tkinter.IntVar()
            self.spin_over_voltage_a = Tkinter.Spinbox(self.lframe_settings_a, textvariable=self.over_voltage_a_var, width=5,
                                                       from_=0, to=30, justify='right')
            self.heat_set_a_var = Tkinter.IntVar()
            self.entry_heat_set_a = Tkinter.Entry(self.lframe_settings_a, textvariable=self.heat_set_a_var, width=7, justify='right')
    
            self.meas_set_a_var = Tkinter.IntVar()
            self.entry_meas_set_a = Tkinter.Entry(self.lframe_settings_a, textvariable=self.meas_set_a_var, width=7, justify='right')
    
            self.voltage_set_a_var = Tkinter.IntVar()
            self.entry_voltage_set_a = Tkinter.Entry(self.lframe_settings_a, textvariable=self.voltage_set_a_var, width=7, justify='right')
    
            self.time_set_a_var = Tkinter.IntVar()
            self.entry_time_set_a = Tkinter.Entry(self.lframe_settings_a, textvariable=self.time_set_a_var, width=7, justify='right')
    
            self.delay_set_a_var = Tkinter.IntVar()
            self.entry_delay_set_a = Tkinter.Entry(self.lframe_settings_a, textvariable=self.delay_set_a_var, width=7, justify='right')
    
            self.repeat_set_a_var = Tkinter.IntVar()
            self.entry_repeat_set_a = Tkinter.Entry(self.lframe_settings_a, textvariable=self.repeat_set_a_var, width=7, justify='right')
    
            #B
            self.voltage_range_b_var = Tkinter.IntVar()
            self.spin_voltage_range_b = Tkinter.Spinbox(self.lframe_settings_b, textvariable=self.voltage_range_b_var, width=5,
                                                        from_=0, to=30, justify='right')
            self.over_voltage_b_var = Tkinter.IntVar()
            self.spin_over_voltage_b = Tkinter.Spinbox(self.lframe_settings_b, textvariable=self.over_voltage_b_var, width=5,
                                                       from_=0, to=30, justify='right')
            self.heat_set_b_var = Tkinter.IntVar()
            self.entry_heat_set_b = Tkinter.Entry(self.lframe_settings_b, textvariable=self.heat_set_b_var, width=7, justify='right')
    
            self.meas_set_b_var = Tkinter.IntVar()
            self.entry_meas_set_b = Tkinter.Entry(self.lframe_settings_b, textvariable=self.meas_set_b_var, width=7, justify='right')
    
            self.voltage_set_b_var = Tkinter.IntVar()
            self.entry_voltage_set_b = Tkinter.Entry(self.lframe_settings_b, textvariable=self.voltage_set_b_var, width=7, justify='right')
    
            self.time_set_b_var = Tkinter.IntVar()
            self.entry_time_set_b = Tkinter.Entry(self.lframe_settings_b, textvariable=self.time_set_b_var, width=7, justify='right')
    
            self.delay_set_b_var = Tkinter.IntVar()
            self.entry_delay_set_b = Tkinter.Entry(self.lframe_settings_b, textvariable=self.delay_set_b_var, width=7, justify='right')
    
            self.repeat_set_b_var = Tkinter.IntVar()
            self.entry_repeat_set_b = Tkinter.Entry(self.lframe_settings_b, textvariable=self.repeat_set_b_var, width=7, justify='right')
    
            #C
            self.voltage_range_c_var = Tkinter.IntVar()
            self.spin_voltage_range_c = Tkinter.Spinbox(self.lframe_settings_c, textvariable=self.voltage_range_c_var, width=5,
                                                        from_=0, to=30, justify='right')
            self.over_voltage_c_var = Tkinter.IntVar()
            self.spin_over_voltage_c = Tkinter.Spinbox(self.lframe_settings_c, textvariable=self.over_voltage_c_var, width=5,
                                                       from_=0, to=30, justify='right')
            self.heat_set_c_var = Tkinter.IntVar()
            self.entry_heat_set_c = Tkinter.Entry(self.lframe_settings_c, textvariable=self.heat_set_c_var, width=7, justify='right')
    
            self.meas_set_c_var = Tkinter.IntVar()
            self.entry_meas_set_c = Tkinter.Entry(self.lframe_settings_c, textvariable=self.meas_set_c_var, width=7, justify='right')
    
            self.voltage_set_c_var = Tkinter.IntVar()
            self.entry_voltage_set_c = Tkinter.Entry(self.lframe_settings_c, textvariable=self.voltage_set_c_var, width=7, justify='right')
    
            self.time_set_c_var = Tkinter.IntVar()
            self.entry_time_set_c = Tkinter.Entry(self.lframe_settings_c, textvariable=self.time_set_c_var, width=7, justify='right')
    
            self.delay_set_c_var = Tkinter.IntVar()
            self.entry_delay_set_c = Tkinter.Entry(self.lframe_settings_c, textvariable=self.delay_set_c_var, width=7, justify='right')
    
            self.repeat_set_c_var = Tkinter.IntVar()
            self.entry_repeat_set_c = Tkinter.Entry(self.lframe_settings_c, textvariable=self.repeat_set_c_var, width=7, justify='right')
    
        # Button
            self.button_run = Tkinter.Button(self.frame_settings, text="Run", command=lambda: self.on_run(), width=25)
            self.button_stop = Tkinter.Button(self.frame_settings, text="Stop", command=lambda: self.on_stop(), width=25)
            self.button_quit = Tkinter.Button(self.frame_settings, text="Quit", command=lambda: self.on_quit(), width=25)
    
        # Grid
            parent.resizable(False, False)
            parent.grid_columnconfigure(0, weight=0)
    
            # Instrument #A
            self.frame_instruments.grid(row=0, column=0)
    
            self.lframe_instrumet_a.grid(row=0, column=0)
            self.label_power_supply_a.grid(row=0, column=0, sticky='W')
            self.label_multimeter_a.grid(row=1, column=0, sticky='W')
            self.combo_power_supply_a.grid(row=0, column=1)
            self.combo_multimeter_a.grid(row=1, column=1)
            self.check_temp_sensor_a.grid(row=2, column=1)
    
            self.lframe_settings_a.grid(row=0, column=2)
            self.label_voltage_range_a.grid(row=0, column=2, sticky='W')
            self.spin_voltage_range_a.grid(row=0, column=3)
            self.label_over_voltage_a.grid(row=1, column=2, sticky='W')
            self.label_heat_set_a.grid(row=2, column=2, sticky='W')
            self.spin_over_voltage_a.grid(row=1, column=3)
            self.entry_heat_set_a.grid(row=2, column=3)
            self.label_voltage_set_a.grid(row=0, column=4, sticky='W')
            self.label_meas_set_a.grid(row=1, column=4, sticky='W')
            self.label_set_time_a.grid(row=2, column=4, sticky='W')
            self.entry_voltage_set_a.grid(row=0, column=5)
            self.entry_meas_set_a.grid(row=1, column=5)
            self.entry_time_set_a.grid(row=2, column=5)
            self.label_set_delay_a.grid(row=0, column=6, sticky='W')
            self.label_set_repeat_a.grid(row=1, column=6, sticky='W')
            self.entry_delay_set_a.grid(row=0, column=7)
            self.entry_repeat_set_a.grid(row=1, column=7)
            self.check_instrument_a.grid(row=2, column=6, sticky='W')
    
            # Instrument #B
            self.lframe_instrumet_b.grid(row=3)
            self.label_power_supply_b.grid(row=3, column=0)
            self.label_multimeter_b.grid(row=4, column=0)
            self.combo_power_supply_b.grid(row=3, column=1)
            self.combo_multimeter_b.grid(row=4, column=1)
            self.check_temp_sensor_b.grid(row=5, column=1)
    
            self.lframe_settings_b.grid(row=3, column=2)
            self.label_voltage_range_b.grid(row=3, column=2, sticky='W')
            self.spin_voltage_range_b.grid(row=3, column=3)
            self.label_over_voltage_b.grid(row=4, column=2, sticky='W')
            self.label_heat_set_b.grid(row=5, column=2, sticky='W')
            self.spin_over_voltage_b.grid(row=4, column=3)
            self.entry_heat_set_b.grid(row=5, column=3)
            self.label_voltage_set_b.grid(row=3, column=4, sticky='W')
            self.label_meas_set_b.grid(row=4, column=4, sticky='W')
            self.label_set_time_b.grid(row=5, column=4, sticky='W')
            self.entry_voltage_set_b.grid(row=3, column=5)
            self.entry_meas_set_b.grid(row=4, column=5)
            self.entry_time_set_b.grid(row=5, column=5)
            self.label_set_delay_b.grid(row=3, column=6, sticky='W')
            self.label_set_repeat_b.grid(row=4, column=6, sticky='W')
            self.entry_delay_set_b.grid(row=3, column=7)
            self.entry_repeat_set_b.grid(row=4, column=7)
            self.check_instrument_b.grid(row=5, column=6, sticky='W')
    
             # Instrument #C
            self.lframe_instrumet_c.grid(row=6)
            self.label_power_supply_c.grid(row=6, column=0)
            self.label_multimeter_c.grid(row=7, column=0)
            self.combo_power_supply_c.grid(row=6, column=1)
            self.combo_multimeter_c.grid(row=7, column=1)
            self.check_temp_sensor_c.grid(row=8, column=1)
    
            self.lframe_settings_c.grid(row=6, column=2)
            self.label_voltage_range_c.grid(row=6, column=2, sticky='W')
            self.spin_voltage_range_c.grid(row=6, column=3)
            self.label_over_voltage_c.grid(row=7, column=2, sticky='W')
            self.label_heat_set_c.grid(row=8, column=2, sticky='W')
            self.spin_over_voltage_c.grid(row=7, column=3)
            self.entry_heat_set_c.grid(row=8, column=3)
            self.label_voltage_set_c.grid(row=6, column=4, sticky='W')
            self.label_meas_set_c.grid(row=7, column=4, sticky='W')
            self.label_set_time_c.grid(row=8, column=4, sticky='W')
            self.entry_voltage_set_c.grid(row=6, column=5)
            self.entry_meas_set_c.grid(row=7, column=5)
            self.entry_time_set_c.grid(row=8, column=5)
            self.label_set_delay_c.grid(row=6, column=6, sticky='W')
            self.label_set_repeat_c.grid(row=7, column=6, sticky='W')
            self.entry_delay_set_c.grid(row=6, column=7)
            self.entry_repeat_set_c.grid(row=7, column=7)
            self.check_instrument_c.grid(row=8, column=6, sticky='W')
    
            # Button
            self.frame_settings.grid(row=8)
            self.button_run.grid(row=8, column=0)
            self.button_stop.grid(row=8,column=1)
            self.button_quit.grid(row=8, column=2)
    
    
        # Function
        def about(self):
            tkMessageBox.showinfo("About", "About" %chr(64))
    
        def on_quit(self):
            self.parent.quit()
            self.parent.destroy()
            self.fout.close()
    
        def save_file(self):
            self.file_opt = self.options = {}
            self.options['filetypes'] = [('Text Files', '.txt')]
            self.dataname = asksaveasfilename(**self.file_opt)
            self.datamode = 'a'
    
            try:
                self.fout = open(self.dataname, self.datamode)
            except Exception, e:
                strError = "Output file open error: "+ str(e)
                tkMessageBox.showerror("Error", strError)
        def on_stop(self):
            self.button_quit.configure(state='active')
    
        def on_run(self):
            self.button_quit.configure(state='disable')
            create_header = Header(gui.fout, self.selected_instruments_query(),self.dataname, self.voltage_range_a_var.get(),
                                   self.voltage_set_a_var.get(), self.delay_set_a_var.get(), self.over_voltage_a_var.get(),
                                   self.meas_set_a_var.get(), self.repeat_set_a_var.get(), self.heat_set_a_var.get(),
                                   self.temp_sensor_a_var.get(), self.voltage_range_b_var.get(),
                                   self.voltage_set_b_var.get(), self.delay_set_b_var.get(), self.over_voltage_b_var.get(),
                                   self.meas_set_b_var.get(), self.repeat_set_b_var.get(), self.heat_set_b_var.get(),
                                   self.temp_sensor_b_var.get(),self.voltage_range_c_var.get(),
                                   self.voltage_set_c_var.get(), self.delay_set_c_var.get(), self.over_voltage_c_var.get(),
                                   self.meas_set_c_var.get(), self.repeat_set_c_var.get(), self.heat_set_c_var.get(),
                                   self.temp_sensor_c_var.get())
    
        def selected_instruments_query(self):
            selected_power_query = [self.instrument_var_a.get(), self.instrument_var_b.get(), self.instrument_var_c.get()]
            selected_multimeter_query = [self.temp_sensor_a_var.get(), self.temp_sensor_b_var.get(), self.temp_sensor_c_var.get()]
            selected_instruments_query = [selected_power_query,selected_multimeter_query]
            return selected_instruments_query
    
    
    class Header:
        WRITE = 0
        def __init__(self,fout,selected_instruments,dataname,voltage_range_a_var,voltage_set_a_var, delay_set_a_var,
                     over_voltage_a_var,meas_set_a_var, repeat_set_a_var, heat_set_a_var, time_set_a_var, voltage_range_b_var,
                     voltage_set_b_var, delay_set_b_var,over_voltage_b_var,meas_set_b_var, repeat_set_b_var,
                     heat_set_b_var, time_set_b_var, voltage_range_c_var,voltage_set_c_var, delay_set_c_var,
                     over_voltage_c_var,meas_set_c_var, repeat_set_c_var, heat_set_c_var, time_set_c_var):
    
            self.fout = fout
            self.selected_instruments = selected_instruments
            self.dataname = dataname
            self.voltage_range_a_var = voltage_range_a_var
            self.voltage_set_a_var = voltage_set_a_var
            self.delay_set_a_var = delay_set_a_var
            self.over_voltage_a_var = over_voltage_a_var
            self.meas_set_a_var = meas_set_a_var
            self.repeat_set_a_var = repeat_set_a_var
            self.heat_set_a_var = heat_set_a_var
            self.time_set_a_var = time_set_a_var
            self.voltage_range_b_var = voltage_range_b_var
            self.voltage_set_b_var = voltage_set_b_var
            self.delay_set_b_var = delay_set_b_var
            self.over_voltage_b_var = over_voltage_b_var
            self.meas_set_b_var = meas_set_b_var
            self.repeat_set_b_var = repeat_set_b_var
            self.heat_set_b_var = heat_set_b_var
            self.time_set_b_var = time_set_b_var
            self.voltage_range_c_var = voltage_range_c_var
            self.voltage_set_c_var = voltage_set_c_var
            self.delay_set_c_var = delay_set_c_var
            self.over_voltage_c_var = over_voltage_c_var
            self.meas_set_c_var = meas_set_c_var
            self.repeat_set_c_var = repeat_set_c_var
            self.heat_set_c_var = heat_set_c_var
            self.time_set_c_var = time_set_c_var
            print self.selected_instruments
            # Header involve data only once
            if Header.WRITE == 0:
                self.header_file()
                Header.WRITE = 1
    
        def header_file(self):
            t = datetime.datetime.now()
            curr_datetime = t.timetuple()
            yr = str(curr_datetime[0])
            curr_date = "%02d."%int(yr[2:]) + "%02d."%curr_datetime[1] + "%02d."%curr_datetime[2]
            curr_time = "%02d:"%curr_datetime[3] + "%02d:"%curr_datetime[4] + "%02d"%curr_datetime[5]
            time_data = curr_date + " " + curr_time
            separate_line = "*"*120
    
            str_out_time = "Filename: \t%s \nTime/Date: \t%s \n\nInstrument - #A\n\n" % (self.dataname, time_data)
            self.fout.write(str_out_time)
            str_out_a = "Voltage Range: \t%d\tSet Voltage: \t%d\tMeasurement Delay: \t%d\n" \
                        "OVP: \t\t%d\tMeas.Set: \t%d\tRepeat: \t\t%d\n" \
                        "Heat.Set: \t%d\tTime Limit: \t%d\n\n\n" % (self.voltage_range_a_var,
                        self.voltage_set_a_var, self.delay_set_a_var, self.over_voltage_a_var,
                        self.meas_set_a_var, self.repeat_set_a_var, self.heat_set_a_var,
                        self.time_set_a_var)
    
            str_out_b = "Instrument - #B\n\nVoltage Range: \t%d\tSet Voltage: \t%d\tMeasurement Delay: \t%d\n" \
                        "OVP: \t\t%d\tMeas.Set: \t%d\tRepeat: \t\t%d\n" \
                        "Heat.Set: \t%d\tTime Limit: \t%d\n\n\n"  % (self.voltage_range_b_var,
                        self.voltage_set_b_var, self.delay_set_b_var, self.over_voltage_b_var,
                        self.meas_set_b_var, self.repeat_set_b_var, self.heat_set_b_var,
                        self.time_set_b_var)
            str_out_c = "Instrument - #C\n\nVoltage Range: \t%d\tSet Voltage: \t%d\tMeasurement Delay: \t%d\n" \
                        "OVP: \t\t%d\tMeas.Set: \t%d\tRepeat: \t\t%d\n" \
                        "Heat.Set: \t%d\tTime Limit: \t%d\n\n\n%s\n" % (self.voltage_range_c_var,
                        self.voltage_set_c_var, self.delay_set_c_var, self.over_voltage_c_var,
                        self.meas_set_c_var, self.repeat_set_c_var, self.heat_set_c_var,
                        self.time_set_c_var, separate_line)
    
            # Print header file depending of the instruments
            for condition, string in zip(self.selected_instruments[0], [str_out_a, str_out_b, str_out_c]):
                if condition:
                    self.fout.write(string)
    
    
    if __name__ == '__main__':
        root = Tkinter.Tk()
        gui = Interface(root)
        root.mainloop()
    

1 个答案:

答案 0 :(得分:1)

一个可怕的简化示例,说明如何使用(和切换)三个不同的循环,并行运行(在本例中)打印三个不同的单词。

使用Tkinters的after()方法,这些循环可以在主循环中运行

enter image description here

from Tkinter import *

class MultiLoop:

    def __init__(self):

        self.master = Tk()
        self.run1 = False
        self.run2 = False
        self.run3 = False

        button1 = Button(text="Toggle Monkey", command = self.togglemonkey).pack()
        button2 = Button(text="Toggle eats", command = self.toggleeats).pack()
        button3 = Button(text="Toggle banana", command = self.togglebanana).pack()
        # first call:
        self.master.after(0, self.loop1)
        self.master.after(0, self.loop2)
        self.master.after(0, self.loop3)

        self.master.mainloop()

    # The three loops
    def loop1(self):
        if self.run1 == True:
            print("Monkey")
        else:
            pass
        # schedule next run
        self.master.after(100, self.loop1)

    def loop2(self):
        if self.run2 == True:
            print("eats")
        else:
            pass
        # schedule next run
        self.master.after(1000, self.loop2)

    def loop3(self):
        if self.run3 == True:
            print("banana")
        else:
            pass
        # schedule next run
        self.master.after(2000, self.loop3)

    # The toggle functions
    def togglemonkey(self):
        self.run1 = True if self.run1 == False else False

    def toggleeats(self):
        self.run2 = True if self.run2 == False else False

    def togglebanana(self):
        self.run3 = True if self.run3 == False else False


MultiLoop()