'How can I plot user chosen quadratics in the form ax^2 + bx +c using matplotlib with tkinter?

So far I have this code. When I try it without tkinter and just put the values for a,b and c into the code it works fine. However, when I try to grab the values from the tkinter entry boxes, it produces an error and says "numpy.core._exceptions._UFuncNoLoopError: ufunc 'multiply' did not contain a loop with signature matching types (dtype('<U1'), dtype('float64')) -> None". How can I make it work?

from tkinter import *
import matplotlib.pyplot as plt
import numpy as np

root = Tk()

yLbl = Label(root, text="y=", pady=30)
yLbl.grid(row=0, column=0)
aEntry = Entry(root, width=2)
aEntry.grid(row=0, column=1)
aLbl = Label(root, text='x\u00B2 +', pady=30)
aLbl.grid(row=0, column=2)
bEntry = Entry(root, width=2)
bEntry.grid(row=0, column=3)
bLbl = Label(root, text='x +', pady=30)
bLbl.grid(row=0, column=4)
cEntry = Entry(root, width=2)
cEntry.grid(row=0, column=5)

def btnGraph():
    x = np.linspace(-5,5,1000)

    a = aEntry.get()
    b = bEntry.get()
    c = cEntry.get()

    y = a*x**2 + b*x + c

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.spines['left'].set_position('center')
    ax.spines['bottom'].set_position('zero')
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
    plt.plot(x,y, 'r')
    plt.show()

btn = Button(root, text="Graph", command=btnGraph)
btn.grid(row=2, column=0)

root.mainloop()


Solution 1:[1]

Here is a link to using variables in tkinter https://www.askpython.com/python-modules/tkinter/tkinter-intvar Look at the defining the tkinter IntVar() variable and Retrieving Values Of IntVar() Variables sections. You can then get the variable into a regular python variable then append it to a list or numpy array. Might seem redundant but this is a great way to get the entry variables.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tester0001