Oct-07-2021, 02:56 PM
Hello,
Thanks so much for your time and help. I had a follow up if you don't mind.
The code below puts the class definition and list creaction in a separate function outside the main loop, RunSweep. I have the pause button commented out because it returns an error, and rightfully so, that sweep isn't defined. Is there a way to modify that function call to include the class as one of the arguments instead of using sweep.Function? Perhaps have a new button pup up when that function is running and sweep is defined?
Thanks so much for your time and help. I had a follow up if you don't mind.
from tkinter import Button, Tk class Sweep: def __init__(self, root, value_List, delay=1000): self.root = root self.value_List= value_List self.delay = delay self.running = False self.end_value = len(self.value_List) self.num = 0 def start(self): print("Start method called") if not self.running: self.running = True self._loop() def stop(self): print("Stop method called") if self.running: self.num = 0 self.running = False def _loop(self): if self.running: if self.end_value is not None and self.end_value <= self.num: self.running = False print("The End") self.num=0 return print(self.value_List[self.num]) self.num += 1 self.root.after(self.delay, self._loop) def CreateList(start, stop, numPts): SweepList=[] SweepList.append(start) stepInterval = (stop-start)/(numPts-1) for x in range(1,numPts): SweepList.append(start + x*stepInterval) return SweepList win = Tk() numList = CreateList(1000,10000,10) sweep = Sweep(win, numList, 1000) init_button = Button(win, text="Start", command=sweep.start) init_button.grid(row=0, column=1, pady=20, padx=10) pause_button = Button(win, text="Stop", command=sweep.stop) pause_button.grid(row=0, column=2, pady=20, padx=10) quit_button = Button(win, text="Quit", command=win.destroy) quit_button.grid(row=1, column=0, columnspan=3, pady=20, padx=10) win.mainloop()In my real application I am running this sweep function within a separate function which is called after a button push. I'll better illustrate in a moment. However, working in baby steps. The code above modifies your original class to contain the list to be swept through, and the _loop function to step through self.value_List. In this case I understand the button's commands are well defined because the class is initialized in the main loop.
The code below puts the class definition and list creaction in a separate function outside the main loop, RunSweep. I have the pause button commented out because it returns an error, and rightfully so, that sweep isn't defined. Is there a way to modify that function call to include the class as one of the arguments instead of using sweep.Function? Perhaps have a new button pup up when that function is running and sweep is defined?
from tkinter import Button, Tk class Sweep: def __init__(self, root, value_List, delay=1000): self.root = root self.value_List= value_List self.delay = delay self.running = False self.end_value = len(self.value_List) self.num = 0 def start(self): print("Start method called") if not self.running: self.running = True self._loop() def stop(self): print("Stop method called") if self.running: self.num = 0 self.running = False def _loop(self): if self.running: if self.end_value is not None and self.end_value <= self.num: self.running = False print("The End") self.num=0 return print(self.value_List[self.num]) self.num += 1 self.root.after(self.delay, self._loop) def CreateList(start, stop, numPts): SweepList=[] SweepList.append(start) stepInterval = (stop-start)/(numPts-1) for x in range(1,numPts): SweepList.append(start + x*stepInterval) return SweepList def RunSweep(start,stop,numPts,delay): numList = CreateList(start,stop,numPts) sweep = Sweep(win, numList, delay) sweep.start() win = Tk() init_button = Button(win, text="Start", command=lambda:RunSweep(1000,10000,10,1000)) init_button.grid(row=0, column=1, pady=20, padx=10) #pause_button = Button(win, text="Stop", command=sweep.stop) #pause_button.grid(row=0, column=2, pady=20, padx=10) quit_button = Button(win, text="Quit", command=win.destroy) quit_button.grid(row=1, column=0, columnspan=3, pady=20, padx=10) win.mainloop()