Tkinter, error maximum recursion depth exceeded - python

I am having trouble writting a tkinter application with matplotlib that updates dinamicaly. I create a plot and use it inside the tkinter window. Then the plot is updated every 50ms with a tk.after() method, which works fine in other applications I have tryed so far. But in my application everything seems to be working until after some time (a minute or so) I get an error:
RecursionError: maximum recursion depth exceeded while calling a Python object
The code is:
import matplotlib
#matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
# implement the default mpl key bindings
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import sys
import time
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
import numpy as np
import matplotlib.pyplot as plt
import math
class MainWindow():
#----------------
def __init__(self, root):
self.index=0
self.root=root
self.fig, self.ax = plt.subplots()
self.line, = self.ax.plot(np.random.randn(100))
#plt.show(block=False)
# a tk.DrawingArea
self.canvas = FigureCanvasTkAgg(self.fig, master=root)
self.canvas.show()
self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
self.toolbar = NavigationToolbar2TkAgg(self.canvas, root)
self.toolbar.update()
self.canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
self.canvas.mpl_connect('key_press_event', self.on_key_event)
self.display = Tk.Label(root, text="") # we need this Label as a variable!
self.display.pack()
self.button1 = Tk.Button(master=root, text='Quit', command=self._quit)
self.button1.pack(side=Tk.BOTTOM)
global w
w=2*math.pi
self.button2 = Tk.Button(master=root, text='Increase frecuency', command=self.button2_event)
self.button2.pack(side=Tk.BOTTOM)
#A simple clock
global miliseconds
global t
t=time.time()
self.update_clock()
print('going to the next stop')
self.root.mainloop()
def on_key_event(event):
print('you pressed %s' % event.key)
key_press_handler(event, self.canvas, toolbar)
def _quit(self):
self.root.quit() # stops mainloop
self.root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
def button2_event(self):
global t
global w
w+=2*3.1416
def update_clock(self):
global t
mili=str(math.floor(1000*(t-time.time())))+' ms'
t=time.time()
now = time.strftime("%H:%M:%S"+'-'+mili)
self.display.configure(text=now)
N=100
y=np.sin(4*math.pi*(np.array(range(N))/N)+t*w)
x=range(N)
self.line.set_ydata(y)
self.line.set_xdata(x)
self.ax.relim()
self.ax.autoscale()
#☺fig.canvas.update()
#fig.canvas.flush_events()
self.canvas.show()
self.canvas.flush_events()
self.root.after(50, self.update_clock()) #<------ERROR HERE!------
root = Tk.Tk()
root.wm_title("Embedding in TK")
MainWindow(root)

Change self.root.after(50, self.update_clock()) to self.root.after(50, self.update_clock), after
after(delay_ms, callback=None, *args)
Registers an alarm callback that is called after a given time.

Related

Matplotlib FuncAnimation fails to run and is bypassed in tkinter GUI

I am trying to have two daemonic threads run alongside my tkinter main thread. One of the threads generates data and the other thread runs FuncAnimation that plots the data that is generated. However, I am having issues with getting FuncAnimation to run, I can see that whenever I click the button both print statements are shown in the console meaning FuncAnimation appears to have been skipped or ran but exited due to some missing or bad parameter. I checked the matplotlib documentation to ensure I'm not missing anything and I think I have created all the objects and called the right methods to make this happen but something is obviously amiss. Any help is greatly appreciated.
import tkinter as tk
import time
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
from matplotlib import animation
import threading
root = tk.Tk()
class dynamicChart():
def __init__(self):
self.x = []
self.y = []
self.buildChart()
def buildChart(self):
self.figure = Figure(figsize=(5, 5), dpi=100)
ax = self.figure.add_subplot(111)
self.scatter = ax.scatter(self.x,self.y)
canvas = FigureCanvasTkAgg(self.figure, root)
canvas.draw()
canvas.get_tk_widget().pack(pady=10)
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack()
def animate(self,i):
print("Enter animate")
self.scatter.set_offsets([self.x, self.y])
def Ani(self):
print("Enter Ani")
self.animation = animation.FuncAnimation(self.figure, self.animate, interval = 2000)
print("Exit Ani")
class genData():
def __init__(self):
self.chart = dynamicChart()
def generate(self):
X = 2
Y = 2
i = 1
while True:
time.sleep(1)
X = X*i
Y = Y + i
self.chart.x.append(X)
self.chart.y.append(Y)
i+=1
StartDataGen_CreateFigureAxes = genData()
genData = threading.Thread(target = StartDataGen_CreateFigureAxes.generate, daemon = True)
genData.start()
ani = threading.Thread(target = StartDataGen_CreateFigureAxes.chart.Ani, daemon = True)
button = tk.Button(root, text="Graph It", command = lambda: ani.start())
button.pack()
root.mainloop()

Behavior of passed subplot reference different in direct reference and as list

I am trying to write a function to update a matplotlib chart following a combobox selection. I don't understand a difference in behavior. In the full program and in the streamlined version below, I create a subplot "a" and can pass it to a function ("tester") and it will modify the subplots attributes as expected. But if I pass the subplot as an entry in a lambda function to a Comboboxselected entry, it does not work. Why the difference?
When the following is run, the x-axis of the plot reads "XAXIS RESET BY TESTER", as expected given the call to tester(). When a new combobox entry is selected, update_chart() prints the choice in the combobox correctly, prints the accompanying test parameter "ZERO" correctly, but the x-axis on the chart does not change. Why the difference in behaviors?
The following stand-alone code reproduces the problem:
# -*- coding: utf-8 -*-
from tkinter import ttk
import tkinter as tk
from tkinter.scrolledtext import ScrolledText
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,
NavigationToolbar2TkAgg)
from matplotlib.figure import Figure
def make_chart_page(nb, chartPage):
labelFrameChart = tk.LabelFrame(chartPage, text="Chart control:")
labelFrameChart.pack(side="left", fill="y", padx=5, pady=5)
comboChart = ttk.Combobox(labelFrameChart, values=["Foos", "Bars", "Widgets"], width=25)
comboChart.pack(anchor="w", padx=5)
f = Figure(figsize=(7,5), dpi=100)
a = f.add_subplot(111)
canvas = FigureCanvasTkAgg(f, chartPage)
canvas.show()
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
data = [ "ZERO", a ]
comboChart.bind("<<ComboboxSelected>>", lambda event, args=data: callback(event, args))
tester(a)
def tester(a):
a.set_xlabel("XAXIS RESET BY TESTER")
def update_chart(combo, var0, chart):
print ("Combobox choice is: ", combo)
print ("Args[0] is: ", var0)
chart.set_xlabel("Xaxis reset by Update_Chart")
def callback(eventObject, args):
update_chart(eventObject.widget.get(), args[0], args[1])
def demo():
root = tk.Tk()
nb = ttk.Notebook(root)
chartPage = ttk.Frame(nb)
make_chart_page(nb, chartPage)
nb.add(chartPage, text='Charts')
nb.pack(expand=1, fill="both")
root.mainloop()
if __name__ == "__main__":
demo()
Thank you,
Randy
You simply need to redraw the canvas again by calling canvas.draw_idle().
def make_chart_page(nb, chartPage):
...
global canvas
canvas = FigureCanvasTkAgg(f, chartPage)
...
def update_chart(combo, var0, chart):
print("Combobox choice is: ", combo)
print("Args[0] is: ", var0)
chart.set_xlabel("Xaxis reset by Update_Chart")
canvas.draw_idle()

Why does Python Tkinter Canvas draw causes a freeze? [duplicate]

I have written a piece of code where I have a simple GUI with an canvas. On this canvas I draw a Matplot. The Matplot is updated every second with data from an SQ Lite DB which I fill with some fake Sensor information (just for testing at the moment).
My Problem was that the redrawing of the canvas causes my window/gui to lag every second. I even tried to update the plot in another thread. But even there I get an lag.
With my newest Code i got most of my things working. Threading helps to prevent my GUI/Window from freezing while the Canvas is updated.
The last thing I miss is to make it Thread safe.
This is the message I get:
RuntimeError: main thread is not in main loop
Here is my newest working code with threading:
from tkinter import *
import random
from random import randint
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import time
import threading
from datetime import datetime
continuePlotting = False
def change_state():
global continuePlotting
if continuePlotting == True:
continuePlotting = False
else:
continuePlotting = True
def data_points():
yList = []
for x in range (0, 20):
yList.append(random.randint(0, 100))
return yList
def app():
# initialise a window and creating the GUI
root = Tk()
root.config(background='white')
root.geometry("1000x700")
lab = Label(root, text="Live Plotting", bg = 'white').pack()
fig = Figure()
ax = fig.add_subplot(111)
ax.set_ylim(0,100)
ax.set_xlim(1,30)
ax.grid()
graph = FigureCanvasTkAgg(fig, master=root)
graph.get_tk_widget().pack(side="top",fill='both',expand=True)
# Updated the Canvas
def plotter():
while continuePlotting:
ax.cla()
ax.grid()
ax.set_ylim(0,100)
ax.set_xlim(1,20)
dpts = data_points()
ax.plot(range(20), dpts, marker='o', color='orange')
graph.draw()
time.sleep(1)
def gui_handler():
change_state()
threading.Thread(target=plotter).start()
b = Button(root, text="Start/Stop", command=gui_handler, bg="red", fg="white")
b.pack()
root.mainloop()
if __name__ == '__main__':
app()
Here the idea without a thread:
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from 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
from random import randint
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
root.update_idletasks()
f = Figure(figsize=(5,5), dpi=100)
x=1
ax = f.add_subplot(111)
line = ax.plot(x, np.sin(x))
def animate(i):
# Open Database
conn = sqlite3.connect('Sensor_Data.db')
c = conn.cursor()
# Create some fake 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 into Sensor_Stream_1 (Date, Temperature, Humidity) values (?, ?, ?)",
(NowIs, Temperature, Humidity))
# Save (commit) the changes
conn.commit()
# Select Data from the Database
c.execute("SELECT Temperature FROM Sensor_Stream_1 LIMIT 10 OFFSET (SELECT COUNT(*) FROM Sensor_Stream_1)-10")
# Gives a list of all temperature values
x = 1
Temperatures = []
for record in c.fetchall():
Temperatures.append(str(x)+','+str(record[0]))
x+=1
# Setting up the Plot with X and Y Values
xList = []
yList = []
for eachLine in Temperatures:
if len(eachLine) > 1:
x, y = eachLine.split(',')
xList.append(int(x))
yList.append(int(y))
ax.clear()
ax.plot(xList, yList)
ax.set_ylim(0,100)
ax.set_xlim(1,10)
ax.grid(b=None, which='major', axis='both', **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, interval=1000)
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root).pack(side="top", fill="both", expand=True)
root.mainloop()
Here is my DB Schema:
CREATE TABLE `Sensor_Stream_1` (
`Date` TEXT,
`Temperature` INTEGER,
`Humidity` INTEGER
);
Your GUI process must not run in any thread. only the dataacquisition must be threaded.
When needed , the data acquired are transfered to the gui process (or the gui process notified from new data available) . I may need to use a mutex to share data resource between acquisition thread and gui (when copying)
the mainloop will look like :
running = True
while running:
root.update()
if data_available:
copydata_to_gui()
root.quit()
I had the same problem with tkinter and using pypubsub events was my solution.
As comments above suggested, you have to run your calculation in another thread, then send it to the gui thread.
import time
import tkinter as tk
import threading
from pubsub import pub
lock = threading.Lock()
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.label = tk.Label(root, text="Temperature / Humidity")
self.label.pack(side="top", fill="both", expand=True)
def listener(self, plot_data):
with lock:
"""do your plot drawing things here"""
self.label.configure(text=plot_data)
class WorkerThread(threading.Thread):
def __init__(self):
super(WorkerThread, self).__init__()
self.daemon = True # do not keep thread after app exit
self._stop = False
def run(self):
"""calculate your plot data here"""
for i in range(100):
if self._stop:
break
time.sleep(1)
pub.sendMessage('listener', text=str(i))
if __name__ == "__main__":
root = tk.Tk()
root.wm_geometry("320x240+100+100")
main = MainApplication(root)
main.pack(side="top", fill="both", expand=True)
pub.subscribe(main.listener, 'listener')
wt = WorkerThread()
wt.start()
root.mainloop()
This function is called every second, and it is outside the normal refresh.
def start(self,parent):
self.close=False
self.Refresh(parent)
def Refresh(self,parent):
'''your code'''
if(self.close == False):
frame.after( UpdateDelay*1000, self.Refresh, parent)
The function is called alone, and everything that happens inside it does not block the normal operation of the interface.

Pickable matplotlib plot in tkk frame

I would like to have a GUI where one part of the interface contains a plot and the rest of the window some tools to work around the plot.
I would like to use mpl_connect to connect the matplotlib canvas with the tkk frame so that I can choose points in the plot to work with.
This was my try, which cowardly refuses to do what I think it should do:
import Tkinter as tk
import ttk
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2TkAgg)
import matplotlib.pyplot as plt
import numpy as np
class Frame_examples_program():
def __init__(self):
self.window = tk.Tk()
self.window.title("Amazing GUI 5000")
self.create_widgets()
def create_widgets(self):
self.window['padx'] = 10
self.window['pady'] = 10
# - - - - - - - - - - - - - - - - - - - - -
# Frame
frame1 = ttk.Frame(self.window, relief=tk.RIDGE)
frame1.grid(row=0, column=0, sticky=tk.E + tk.W + tk.N + tk.S, padx=0, pady=0)
frame2 = ttk.Frame(self.window, relief=tk.RIDGE)
frame2.grid(row=1, column=0, sticky=tk.E + tk.W + tk.N + tk.S, padx=0, pady=0)
self.PlotFrame(frame1, frame2)
class PlotFrame():
# The plot
def __init__(self, parent1, parent2):
self.parent1 = parent1
self.parent2 = parent2
canvas = self.plot()
self.plot_toolbar(canvas)
def plot(self):
# the actual plot
fig, ax = plt.subplots()
plt.imshow(np.ones((100,100)),picker=True)
canvas = FigureCanvasTkAgg(fig, self.parent1)
canvas.mpl_connect('button_press_event', self.onclick)
return(canvas)
def plot_toolbar(self, canvas):
# the tool bar to the plot
toolbar = NavigationToolbar2TkAgg(canvas, self.parent2)
toolbar.update()
canvas.get_tk_widget().grid(row=1, column=1)
canvas.draw()
def onclick(self, event):
# the devilish thing that does nothing!
print('WOHOOOO')
# Create the entire GUI program
program = Frame_examples_program()
# Start the GUI event loop
program.window.mainloop()
As you'll see when you run this, the matplotlib toolbar nicely works, but I just can't call the onclick event! Why?
The PlotFrame instance that is created via self.PlotFrame(frame1, frame2) is not stored anywhere and hence garbage collected. At the point where you expect the callback to happen this instance does not exist in memory anymore.
Solution: Make sure to keep a reference to the PlotFrame at all time, e.g.
self.myplot = self.PlotFrame(frame1, frame2)
Note that this is a more or less general rule: You would almost never instantiate a class without storing it anywhere. In case you do and don't run into trouble, that would mostly be sign that the class is not needed at all.

Button instance has no __call__ method

i want to make app in python so i have just started, i want to click button and graph should be displayed !!!
But its not allowing me to do so.
code snippet:
from Tkinter import *
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
def schart2(stock_sym):
df
df.astype(float)
labels = ['day0','day1','day2','day3','day4','day5','day6','day7','day8','day9','day10','day11','day12','day13','day14','day15','day16','day17','day18','day19','day20','day21','day22','day23','day24','day25','day26','day27','day28','day29','day30']
df.columns=labels
plt.subplots_adjust(left=0.1, bottom=0.15, right=0.99, top=0.95)
ax=plt.imshow(df, cmap=plt.cm.get_cmap("RdPu"), interpolation="nearest",aspect="auto")
plt.xticks(range(len(labels)), labels, rotation=90, va="top", ha="center")
plt.colorbar()
plt.title("Return rate Retention")
plt.xlabel("Retention Days")
plt.ylabel("Total Days")
fig=ax.get_figure()
plt.show()
class StockChart(Frame):
def __init__(self, stock_sym=''):
Frame.__init__(self, parent=None)
self.pack(expand=YES, fill=BOTH)
self.create_widgets(stock_sym)
self.makeWidgets(stock_sym)
def create_widgets(self, stock_sym):
self.makeWidgets = tk.Button(self)
self.makeWidgets["text"] = "Hello World\n(click me)"
self.makeWidgets["command"] = self.makeWidgets
self.makeWidgets.pack(side="top")
self.quit = tk.Button(self, text="QUIT", fg="red",
command=root.destroy)
self.quit.pack(side="bottom")
def makeWidgets(self, stock_sym):
#self.f = graphData(stock_sym,12,26)
self.f = schart2(stock_sym)
self.canvas = FigureCanvasTkAgg(self.f)
self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
self.canvas.show()
if __name__ == '__main__':
StockChart('ACAD').mainloop()
And the ERROR is:
self.makeWidgets(stock_sym)
AttributeError: Button instance has no __call__ method
Am I doing this completely wrong or is there an easy way to fix this? Any help would be greatly appreciated.
Your variable
self.makeWidgets = tk.Button(self)
Has the same name as your class method
def makeWidgets(self, stock_sym):
Try first to change this

Categories

Resources