Run More Loops At The Same Time?
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.
Solution 1:
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 *
classMultiLoop:
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 loopsdefloop1(self):
if self.run1 == True:
print("Monkey")
else:
pass# schedule next run
self.master.after(100, self.loop1)
defloop2(self):
if self.run2 == True:
print("eats")
else:
pass# schedule next run
self.master.after(1000, self.loop2)
defloop3(self):
if self.run3 == True:
print("banana")
else:
pass# schedule next run
self.master.after(2000, self.loop3)
# The toggle functionsdeftogglemonkey(self):
self.run1 = Trueif self.run1 == FalseelseFalsedeftoggleeats(self):
self.run2 = Trueif self.run2 == FalseelseFalsedeftogglebanana(self):
self.run3 = Trueif self.run3 == FalseelseFalse
MultiLoop()
Post a Comment for "Run More Loops At The Same Time?"