I am recreating the game Wordle for practice.
I am trying to find a function that allows me to destroy all widgets in my tkinter window.
root.destroy() isn't a solution because it just closes the whole window.
I want to use this function to, whenever we win the game, restart the whole program without having to manually do it.
Program:
from tkinter import *
from tkinter import ttk
import tkinter as tk
from english_words import english_words_alpha_set
import time
guess = "bored"
guess = guess.upper()
word=""
a=0
def callback1(var, index, mode):
root.entry2.configure(state="normal")
root.entry2.focus()
root.entry1.configure(state="disabled")
root.hasfocus = "entry2"
def callback2(var, index, mode):
root.entry3.configure(state="normal")
root.entry3.focus()
root.entry2.configure(state="disabled")
root.hasfocus = "entry3"
def callback3(var, index, mode):
root.entry4.configure(state="normal")
root.entry4.focus()
root.entry3.configure(state="disabled")
root.hasfocus = "entry4"
def callback4(var, index, mode):
root.entry5.configure(state="normal")
root.entry5.focus()
root.entry4.configure(state="disabled")
root.hasfocus = "entry5"
def callback5(var, index, mode):
root.entry5.configure(state="disabled")
root.hasfocus = "none"
def newRow():
boxes = [root.box1, root.box2, root.box3, root.box4, root.box5]
word = root.name1.get() + root.name2.get() + root.name3.get() + root.name4.get() + root.name5.get()
word=word.upper()
root.greenBoxes=0
def checkBg():
if word[a] == guess[a]:
root.greenBoxes=root.greenBoxes+1
print("greenBoxes=", root.greenBoxes)
return '#538D4E'
elif word[a] in guess:
return '#B59F3B'
else:
return '#3A3A3C'
root.entry1.destroy()
root.entry2.destroy()
root.entry3.destroy()
root.entry4.destroy()
root.entry5.destroy()
a=0
while a < 5:
boxes[a].configure(text=word[a], bg=checkBg(), fg = "white")
a=a+1
if root.greenBoxes == 5: #if player won:
root.WinMsg = Label(root, height=2, width=28, bg='#2d2d2f', fg='white', text='You\'ve won! The word was \"' + guess[0] + guess[1:5].lower() +"\"", font="Calibri 20")
root.WinMsg.place(x=65, y=100) #say that he won
#*destroy all widgets*
root.CreateWindow() #remake all widgets (to start a new game)
return
root.rows = root.rows+1
if root.rows < 6: #if player still have any try
root.d=root.d+70
root.createBoxes()
root.createEntry()
else: #if player lost
root.GameOverMsg = Label(root, height=2, width=28, bg='#2d2d2f', fg='white', text='You\'ve lost! The word was \"' + guess[0] + guess[1:5].lower() +"\"", font="Calibri 20")
root.GameOverMsg.place(x=65, y=100) #says that he lost
#*destroy all widgets*
root.CreateWindow() #remake all widgets (to start a new game)
return
class root(Tk):
def __init__(self):
super(root, self).__init__()
self.title("Wordle")
self.minsize(580,465)
self.windowBG = '#121213'
self.CreateWindow()
def CreateWindow(self):
self.configure(bg=self.windowBG)
self.hasfocus = ""
self.d=0
self.rows=0
self.greenBoxes = 0
self.createBoxes()
self.createEntry()
self.backspace = Button(self, text="Backspace", width=8, font ="Calibri 18", command=self.do_backspace)
self.backspace.place(x=465, y=62)
self.enter = Button(self, text="Enter", width=6, font ="Calibri 18", command=self.do_enter)
self.enter.place(x=480, y=125)
def createBoxes(self):
self.box1 = tk.Label(self, text="", font="Calibri", height=2, width=6, bg="#3A3A3C")
self.box1.place(x=70, y=30+self.d)
self.box2 = tk.Label(self, text="", font="Calibri", height=2, width=6, bg="#3A3A3C")
self.box2.place(x=150, y=30+self.d)
self.box3 = tk.Label(self, text="", font="Calibri", height=2, width=6, bg="#3A3A3C")
self.box3.place(x=230, y=30+self.d)
self.box4 = tk.Label(self, text="", font="Calibri", height=2, width=6, bg="#3A3A3C")
self.box4.place(x=310, y=30+self.d)
self.box5 = tk.Label(self, text="", font="Calibri", height=2, width=6, bg="#3A3A3C")
self.box5.place(x=390, y=30+self.d)
def do_backspace(self):
if self.hasfocus == "entry1":
pass
elif self.hasfocus == "entry2":
self.entry1.configure(state="normal")
self.entry1.delete(0, 'end')
self.entry1.configure(state="normal")
self.entry1.focus()
self.hasfocus="entry1"
self.entry2.configure(state="disabled")
elif self.hasfocus == "entry3":
self.entry2.configure(state="normal")
self.entry2.delete(0, 'end')
self.entry2.configure(state="normal")
self.entry2.focus()
self.hasfocus="entry2"
self.entry3.configure(state="disabled")
elif self.hasfocus == "entry4":
self.entry3.configure(state="normal")
self.entry3.delete(0, 'end')
self.entry3.configure(state="normal")
self.entry3.focus()
self.hasfocus="entry3"
self.entry4.configure(state="disabled")
elif self.hasfocus == "entry5":
self.entry4.configure(state="normal")
self.entry4.delete(0, 'end')
self.entry4.configure(state="normal")
self.entry4.focus()
self.hasfocus="entry4"
self.entry5.configure(state="disabled")
else:
self.entry5.configure(state="normal")
self.entry5.delete(0, 'end')
self.entry5.configure(state="normal")
self.entry5.focus()
self.hasfocus="entry5"
def do_enter(self):
if len(self.name1.get() + self.name2.get() + self.name3.get() + self.name4.get() + self.name5.get()) == 5:
newRow()
else:
self.FiveLetterWord = Label(self, height=2, width=22, bg='#2d2d2f', fg='white', text='Word must be of 5 letters', font="Calibri 20", background=self.windowBG,highlightbackground='#3A3A3C', highlightthickness=2)
self.FiveLetterWord.place(x=100, y=100)
self.update_idletasks()
self.after(3000)
self.FiveLetterWord.destroy()
def createEntry(self):
self.name1 = StringVar()
self.entry1 = ttk.Entry(self, width=2, textvariable = self.name1, font="Calibri 15", justify='center')
self.entry1.place(x=90, y=40+self.d)
self.name2 = StringVar()
self.entry2 = ttk.Entry(self, width=2, textvariable = self.name2, font="Calibri 15", justify='center')
self.entry2.place(x=170, y=40+self.d)
self.entry2.configure(state="disabled")
self.name3 = StringVar()
self.entry3 = ttk.Entry(self, width=2, textvariable = self.name3, font="Calibri 15", justify='center')
self.entry3.place(x=250, y=40+self.d)
self.entry3.configure(state="disabled")
self.name4 = StringVar()
self.entry4 = ttk.Entry(self, width=2, textvariable = self.name4, font="Calibri 15", justify='center')
self.entry4.place(x=330, y=40+self.d)
self.entry4.configure(state="disabled")
self.name5 = StringVar()
self.entry5 = ttk.Entry(self, width=2, textvariable = self.name5, font="Calibri 15", justify='center')
self.entry5.place(x=410, y=40+self.d)
self.entry5.configure(state="disabled")
self.entry1.focus() ; hasfocus = "entry1"
self.name1.trace_add("write", callback1)
self.name2.trace_add("write", callback2)
self.name3.trace_add("write", callback3)
self.name4.trace_add("write", callback4)
self.name5.trace_add("write", callback5)
root=root()
root.mainloop()
You can use winfo_children to get a list of all child widgets of the root window, and then you can iterate over the list to destroy each child.
for child in root.winfo_children():
child.destroy()
From that point on, you have a root window with nothing in it.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
First of all, I have to say that I'm a beginner in Python. These are my first steps in creating a class. And now my question.On that picture, I presented what my code should work.
Start the program (Run)
Check what instruments should be power on. There are three
possibilities and you can power one, two or all three Units at the
same time. (Power #A/B/C)
Furthermore, with each power unit additional you can turn on
Temperatur sensor (Temp. #A,B,C)
Depending on which units are involved to run a Loop #A, #B, #C or
for example two units #B,#C or perhaps all three.This is actually my Problem and question. How can be able to simultaneously also at
the same time, run two or maybe even three loops ?
I hope you can give me some advice and help in solving my problem.
Below I will put my code so you can refer and pay Attention what I could change or improve in my code.
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()
A terrifyingly simplified example of how you can use (and toggle) three different loops, running in parallel, (in this case) printing three different words.
Using Tkinters's after() method, these loops can run inside the main loop:
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()