I wrote a GUI program using thread and Tkinter .I used thread since it keeps on checking for Arduino input on Port 27.
def main():
t = Test()
t.go()
try:
join_threads(t.threads)
except KeyboardInterrupt:
print "\nKeyboardInterrupt catched."
print "Terminate main thread."
print "If only daemonic threads are left, terminate whole program."
class Test(object):
def __init__(self):
self.running = True
self.threads = []
self.root=Tk()
self.Rval = IntVar()
self.Rval.set(2)
self.root.title("RFID EM LOCK CONTROLLER")
self.variable=StringVar()
self.variable2=StringVar()
self.var2=StringVar()
self.var3=StringVar()
self.i=0
self.root.resizable(0,0)
self.your_label=Label(self.root,textvariable=self.variable,width=40,height=5,bg="Black",fg="Green")
self.lframe = Frame(self.root,width=300,height=200,padx=0)
self.lframe.pack()
self.root.wm_iconbitmap(bitmap = "icon.ico")
def foo(self):
ser=serial.Serial("COM27",9600)
while(self.running):
self.var2= ser.readline()
v = self.var2[0:8];
print v
if self.Isexist(v):
ser.write('A')
self.var2="Valid Card\n"+"Card Number: "+v;
else:
ser.write('B')
self.var2="InValid Card\n"+"Card Number: "+v;
def grid(self):
self.your_label.pack()
def update_label(self):
self.i=self.i+1
self.variable.set(str(self.var2))
self.variable2.set(str(self.var2))
self.root.after(20,self.update_label)
def get_user_input(self):
self.grid()
self.root.after(20,self.update_label)
self.root.mainloop()
def go(self):
t1 = threading.Thread(target=self.foo)
t2 = threading.Thread(target=self.get_user_input)
# Make threads daemonic, i.e. terminate them when main thread
# terminates. From: http://stackoverflow.com/a/3788243/145400
t1.daemon = True
t2.daemon = True
t1.start()
t2.start()
self.threads.append(t1)
self.threads.append(t2)
def join_threads(threads):
"""
Join threads in interruptable fashion.
From http://stackoverflow.com/a/9790882/145400
"""
for t in threads:
while t.isAlive():
t.join(5)
if __name__ == "__main__":
main()
The problem with above code is that it hangs when i set application icon using self.root.wm_iconbitmap(bitmap = "icon.ico")
on windows 8.1 prox64 . I am using python 2.7 with tkinter. without application icon it works .
How to sort out this problem ?