Performance – Python Plot shows live SQL data on Tkinter Canvas


I have written a code where I have a simple graphical user interface with a Tkinter canvas. In this canvas I draw a Matplot. The Matplot is updated every second with false sensor data that I simulate in an SQLite database (only for tests at this time).

I achieved most of my goals for this test case. But some things that I would like to have reviewed:

  1. The performance does not seem to be really good. The window is delayed if you drag it over the desk. I think this is caused by the redrawing of the canvas. I tried using threads but I could not interrupt my mainloop when updating the canvas.
  2. My Code seems quite messy (I'm a beginner and I'm just learning to code with good style). All the criticisms and ideas to do it better are welcome.

So here is my code so far:

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
of matplotlib.figure import figure
import tkinter as tk
import numpy as np
import matplotlib.pyplot as plt
Import matplotlib.animation as animation.
import sqlite3
from datetime import datetime
of random import randint

MainApplication class (tk.Frame):
def __init __ (self, parent, * args, ** kwargs):
tk.Frame .__ init __ (self, parent, * args, ** kwargs)
self.parent = father

root.update_idletasks ()

f = Figure (figsize = (15,5), dpi = 55)
x = 1
ax = f.add_subplot (111)
line, = ax.plot (x, np.sin (x))

To encourage (i):
# Open database
conn = sqlite3.connect (& # 39; Sensor_Data.db & # 39;)
c = conn.cursor ()
# Create some false sensor data
NowIs = datetime.now ()
Temperature = randint (0, 100)
Humidity = randint (0, 100)
# Add data to the database
c = conn.cursor ()
# Insert a row of data
c.execute ("insert in the values ​​of Sensor_Stream_1 (Date, Temperature, Humidity) (?,?,?)",
(Now it is, temperature, humidity)
# Save (confirm) the changes
conn.commit ()
# Select data from the database
c.execute ("SELECT the temperature FROM Sensor_Stream_1 LIMIT 30 DISPLACEMENT (SELECT ACCOUNT (*) FROM Sensor_Stream_1) -30")
# Gives a list of all temperature values
x = 1
Temperatures = []

            To record in c.fetchall ():
Temperatures.append (str (x) + & # 39;, & # 39; + str (record[0]))
x + = 1
# Setting the graph with the values ​​of X and Y
xList = []
            yList = []

            For each line in temperatures:
if len (eachLine)> 1:
x, y = eachLine.split (& # 39;, & # 39;)
xList.append (int (x))
yList.append (int (y))

ax.clear ()

ax.plot (xList, yList)

ax.set_ylim (0,100)
ax.set_xlim (1.30)

ax.tick_params (axis = & # 39; both & # 39 ;, what = & # 39; major & # 39 ;, labelsize = 12)

ax.grid (b = None, which = & # 39; major & # 39 ;, axis = & # 39; both & # 39 ;, ** kwargs)


label = tk.Label (root, text = "Temperature / Humidity"). pack (side = "top", fill = "both", expand = True)

canvas = FigureCanvasTkAgg (f, master = root)
canvas.get_tk_widget (). pack (side = "left", fill = "both", expand = True)

root.ani = animation.FuncAnimation (f, animate, range = 1000)

yes __name__ == "__main__":
root = tk.Tk ()
MainApplication (root) .pack (side = "top", fill = "both", expand = True)
root.mainloop ()