'Tkinker and Matlab Integration
I am reaching out for some help for a personal project that I am creating that uses Tkinker and Matlablib to plot a real-time heart signal. Currently, I was able to create my GUI and display a subplot with some buttons.

Problem Currently, I cannot for the life me gigure out how to have a Real-time signal display on the graph while having the buttons on my "ECG Graph" window be able to still be able to be interactive.
From my research, I was able to find some import libraries and concepts that I would use in order to implement this such as threading. Is there something that I am not understanding with my code or missing or is my idea wrong and there is a better way to implement this? Update: I was able to get the graph to somewhat graph however the program crashes when every the start button is pressed on the GUI module.
Current work Imports
import threading
import time
import tkinter as tk
from tkinter import filedialog as fd
from tkinter import ttk, Menu
from tkinter.messagebox import showerror, showinfo
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from Imports import text_imports
from random import randint
Code inside a class named graph with the following initialization. NOTE: THe start button when clicked will start the on_click function in its class
CLASS GRAPH:
class Graph(tk.Tk):
def __init__(self):
super().__init__()
self.init_window()
self.init_widgets()
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
self.ax.title.set_text('ECG Monitoring')
self.ax.set_xlabel("Time [mS]")
self.ax.set_ylabel("Amplitude [V/mV]")
self.ax.grid()
self.graph = FigureCanvasTkAgg(self.fig, master=self)
self.graph.get_tk_widget().pack(side="top", fill='both', expand=True)
def init_window(self):
# creates the title and appearance of the window
def init_widgets(self):
# creates the widgets for the GUI
def button_handler(self):
self.change_state()
threading.Thread(target=self.plotter(), daemon=True).start()
def change_state(self):
if self.continuePlotting:
self.continuePlotting = False
self.pause['state'] = 'disabled'
self.start['state'] = 'normal'
else:
self.continuePlotting = True
self.start['state'] = 'disabled'
self.pause['state'] = 'normal'
def plotter(self):
print("inside plotter")
while self.continuePlotting:
print("inside plotter(s) while statement")
self.ax.cla()
self.ax.grid()
self.dpts = data_points()
self.ax.plot(range(10), self.dpts, marker='o', color='orange')
self.graph.draw()
time.sleep(1)
@staticmethod
def screen_shot():
print("inside screenshot")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
