2

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.

1
  • 1
    You should create a thread which runs the infinite loop. Commented Jul 15, 2020 at 14:28

1 Answer 1

2

Every time you work with gui's and you create a loop or a process that takes a long time the gui freezes unless you use the gui's mechanism or threads, in tkinter you can use the "after" method in kivy you use Clock.

You can use this in combination with threads, but with threads you must know what you are doing or your gui will behave erratically.

So here I made a basic tkinter working example for you, that uses tkinter "after" method to avoid freezing the interface.

I use the flag as you mentioned so it resembles what you want.

from tkinter import Tk, mainloop
from tkinter.ttk import Button, Label, Frame


counter = 0
stop_counter = False

def start(label):
    global counter
    global stop_counter
    counter += 1
    label.config(text=counter)
    if not stop_counter:
        label.after(1000, start, label)
    else:
        stop_counter = False

def stop():
    global stop_counter
    stop_counter = True

win = Tk()

lbl = Label(win, text='0', font=("Lucida Grande", 20))
lbl.pack()
frm = Frame()
frm.pack()
btn = Button(frm, text='Start Counter', command=lambda label=lbl: start(label))
btn.pack(side='left')
btn = Button(frm, text='Stop Counter', command=stop)
btn.pack(side='right')


mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.