Hi I have my code here that is connected in my tkinter GUI and I've read that tkinter freezes when there's a loop in it since it's also a loop. So what I'm trying to do here is to give this function some time like 45 seconds then break the loop after that time passes. I've also tried threading but I do know how to implement it properly that's why I came up with this solution. But after I tried this one the loop doesn't break after 45 seconds. What am I doing wrong? Will this one work? Thank you very much in advance. I did watch a lot of tutorials about threading and queues but it's very complicated for a beginner like me so if this one is possible to work please help me. Thank you once again!
import serial
port = '/dev/ttyUSB0'
balance = 0
timeout = time.time() + 45
#get Pulse from Arduino
def getPulse():
try:
ser = serial.Serial(port, 9600)
while True:
read = ser.readline()
try:
value = int(read, 10)
if (value != 0):
print(value)
break
except ValueError:
pass
finally:
return value
#Update Balance
def balanceCounter(coins):
total = 0
total += coins
return total
def getCoins():
pulse = getPulse()
coinvalue = 0
if pulse == 1:
coinvalue = 1
print("1 Inserted")
elif pulse == 5:
coinvalue = 5
print("5 Inserted")
elif pulse == 10:
coinvalue = 10
print("10 Inserted")
return coinvalue
def mainCounter(numpages):
global balance
#input target Sample Amount
targetValue = numpages
tbalance = 0
try:
while tbalance <= int(targetValue):
coins = getCoins()
balance = balanceCounter(coins)
tbalance += balance
print("Total balance : "+str(tbalance))
if tbalance >= int(targetValue) or time.time() > timeout:
break
finally:
return tbalance