简体   繁体   中英

drawing graphs using tkinter

I am trying to write a code that takes 4 inputs and draws a cubic graph of it. the code that I have written so far is:

from tkinter import *
import numpy as np
import matplotlib.pyplot as plt
root=Tk()


def equation():
    global a
    global b
    global c
    global d
    for i in (a,b,c,d):
        k=float(i.get())
        i.delete(0,'end')
        i.insert(0,k)
        
    xlist = np.linspace(-10,10,num=1000)
        def Cubic_function(xlist,a,b,c,d):
            
            return a*xlist**3+b*xlist**2+c*xlist+d
        
        plt.figure(num=0,dpi=120)
        ylist = Cubic_function(xlist,a,b,c,d)
        plt.plot(xlist,ylist,label="f(x)", linestyle='--')

        plt.legend()
        plt.grid(linestyle =':')
        plt.xlim([-1000, 1000])
        plt.ylim([-1000, 1000])

        plt.title('graph')
        plt.xlabel('x-axis')
        plt.ylabel('y-axis')


            

a=Entry(root,text='3')
a. pack() 
b=Entry(root)
b.pack() 
c=Entry(root)
c.pack() 
d=Entry(root)
d.pack() 
buttonl=Button(root, text="press",command=equation)
buttonl.pack() 
button2=Button(root, text='cubic', command=Cubic_function)
button2.pack()
root .mainloop()

I am relatively new to coding, especially with Tkinter, and there will probably some silly mistakes, but can anyone help me please?

the code to draw the equations works correctly when the graph is drawn on the IDE, but as soon as I try to draw the graph on a new window it doesn't work.

this is the code that draws the equation in the IDE:

from tkinter import *
import numpy as np
import matplotlib.pyplot as plt
root=Tk()
root.geometry('900x500')
a=float(input('enter the value of a: '))
b=float(input('enter the value of b: '))
c=float(input('enter the value of c: '))
d=float(input('enter the value of d: '))
scale=float(input('enter the value of the scale: '))
    
    
    
def plotting_function():
    def f(x,a,b,c,d):
        return a*x**3+b*x**2+c*x+d

    xlist = np.linspace(-10,10,num=1000)
    # xlist = np.arange(-10,10.1,.1)

    
    ylist = f(xlist,a,b,c,d)


    plt.figure(num=0,dpi=120)
    plt.plot(xlist,ylist,label="f(x)", linestyle='--')

    plt.legend()
    plt.grid(linestyle =':')
    plt.xlim([-scale,scale])
    plt.ylim([-scale, scale])

    plt.title('graph')
    plt.xlabel('x-axis')
    plt.ylabel('y-axis')
    

    

button1=Button(root, text='click here to display the equation.', command=plotting_function)
button1.pack()
root.mainloop() ```

First, there indentation errors. We need to indent xlist = np.linspace(-10,10,num=1000) into the for loop. In addition, button2 is trying to access Cubic_function() , but that is in another function, so we need to move Cubic_function() out of equation() . In Cubic_function() , it looks like that there are no .get() , and Python can't multiply an integer by an Entry. We need to type in float(a.get()) in order to multiply.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM