Timofey Goritsky
Timofey Goritsky

Reputation: 89

Stop a working function with a button (Python, Tkinter)

I have a little situation with Tkinter. I have a piece of code that constantly receives raw EMG data from Thalmic`s Myo Armband and writes that data (plus the name of a gesture) to a csv file. I designed a little tkinter interface to make it look more user-friendly. What I need is to write a function that will stop the previously started recording function. Also I have a problem with the label, that shows the number of rows in a csv file, but it is another question. Here is the code:

from gesture_classificator import MyoRaw
import csv
import tkinter
import tkinter.messagebox
import sys


root = tkinter.Tk()
root.title("Recording data")
m = MyoRaw(sys.argv[1] if len(sys.argv) >= 2 else None)

v = tkinter.StringVar()
entry = tkinter.Entry(root, textvariable=v)
entry.grid(row=0, column=0) 
v.set("")


def process_emg(emg, times):
    name = v.get()
    with open('own_test.csv', 'a+') as file:
        writing = csv.writer(file)
        writing.writerow(emg+(name,))


def show_row_count():
    with open('own_test.csv', 'r') as return_me_the_row_count:
        reading = csv.reader(return_me_the_row_count)
        data = list(reading)
        row_count = len(data)
        return row_count


def start_recording():
    m.add_emg_handler(process_emg)
    m.connect()


def stop_recording():
    ???

B1 = tkinter.Button(root, text="Start the recording", command=start_recording)
B2 = tkinter.Button(root, text="Stop the recording", command=stop_recording)
rows_number = tkinter.Label(root, text=show_row_count)
rows_number.config(text=show_row_count)
B1.grid(row=1, column=0)
B2.grid(row=1, column=1)
rows_number.grid(row=0, column=1)
root.mainloop()

try:
    while True:
        m.run(1)
except SystemExit:
    pass
finally:
    m.disconnect()

Upvotes: 0

Views: 450

Answers (1)

Alexander Leithner
Alexander Leithner

Reputation: 3432

According to the MyoRaw code, you would probably like the function MyoRaw.disconnect(), which you would use like this:

def stop_recording():
  m.disconnect()

In such cases it would be easier and faster for you to check the documentation and/or the code of the framework you are using.

Upvotes: 1

Related Questions