Apr-02-2018, 12:23 PM
(Mar-28-2018, 04:11 PM)woooee Wrote: It is a design problem. A while loop hogs the processor as it is always running, which doesn't allow for anything else to happen. You should seriously consider using a class for this so you can use instance objects for rftagID and anything else you want to keep. This also allows you to use after() instead of a while loop, which means you can do other things while scanCardLoop() is running.
""" A simple example that uses a counter to print to the screen from a function repeatedly called by after{} You can also enter data into the Entry while the counter function is running to show that other things can happen simultaneously """ import sys if sys.version_info[0] < 3: import Tkinter as tk ## Python 2.x else: import tkinter as tk ## Python 3.x class TestClass(): def __init__(self): self.top = tk.Tk() self.top.title("Test of After") self.top.geometry("200x150+10+10") tk.Label(self.top, text="Timer Test ").pack(side="left" ) entry_1 = tk.Entry(self.top, width=10) entry_1.pack(side="left") entry_1.focus_set() ## self.ctr is an instance object, so retains it's value self.ctr = 1 self.printit() self.top.mainloop() def printit(self): print("print 1", self.ctr) self.ctr += 1 ## simulate if a condition is True if self.ctr < 100: self.top.after(100, self.printit) else: print("\nloop ended") self.top.quit() ##==================================================================== if __name__ == '__main__': CT=TestClass() ## tkinter has exited. This shows how to get the value of a variable. print("\ncounter's value is %d" % (CT.ctr))
I ended up removing the infinite while loop and used the after() method instead. Works fine now thanks!