I wrote a GUI to control a measurement device and its data acquisition.
An simplified sketch of the code would look like this:
def start_measurement():
#creates text file (say "test.txt") and write lines with data to it continuously
def stop_measurement():
#stops the acquisition process. The text file is saved.
startButton = Button(root, text = "start", command = start_measurement)
endButton = Button(root, text = "end", command = stop_measurement)
Additionally I have a function which analyzes the output text file in real time, i.e. it reads the text file continuously while it is being written through an infinite while loop:
def analyze():
file_position = 0
while True:
with open ("test.txt", 'r') as f:
f.seek(file_position)
for line in f:
#readlines an do stuff
fileposition = f.tell()
Now logically I want to start the analyze function when I press the START Button and end the analyze function, i.e break out of the while loop, when pressing the END Button. My idea was to put a flag, which initializes the while loop and when the END Button is pressed the flag value changes and you break out of the while loop. Then just put the analyze function in the start measurement function.
So kind of like this:
def analyze():
global initialize
initialize = True
file_position = 0
while True:
if initialize:
with open ("test.txt", 'r') as f:
f.seek(file_position)
for line in f:
#readlines an do stuff
fileposition = f.tell()
else: break
def start_measurement():
#creates text file (say "test.txt") and writes lines with data to it
analyze()
def stop_measurement():
#stops the acquisition process
initialize = False
startButton = Button(root, text = "start", command = start_measurement)
endButton = Button(root, text = "end", command = stop_measurement)
So this was my naive newbie idea. But the problem is when i hit the START Button the END Button gets disabled because I am entering the infinite while loop I guess and i can't stop the process. I know this is kind of vague but maybe someone has an idea on how to handle this problem? I also thought of using threads but could not make it work. I don't know if this would be a good approach.