0

Hi there, Im writing a program that takes a function and makes a plot of it. the program consists of 2 functions and each creates its own GraphWin. However i want to have a mouse click at the end close both windows. So i returned the window from the first function which i thought should allow it to be accessed and the close() method called, but I keep getting the error, "NameError: global name 'graph' is not defined." I am not sure what I am doing wrong because all the rest works fine. Any help would be appreciated and thank you in advance.

This is my code so far

from graphics import *
from math import *


def plot_it(x_start, x_end, y_start, y_end, f_of_x):
    x_start = float(x_start.getText())
    x_end = float(x_end.getText())
    y_start = float(y_start.getText())
    y_end = float(y_end.getText())
    f_of_x = f_of_x.getText()
    graph = GraphWin('Plot of f(x)', 400, 400)
    graph.setCoords(x_start, y_start, x_end, y_end)
    graph.setBackground('white')
    N = 101
    delta = (x_end - x_start) / (N - 1)
    points = []
    for i in range(N):
        x = x_start + i * delta
        y = eval(f_of_x)
        p = Point(x, y)
        points.append(p)
        for q in range(len(points) - 1):
            Line(points[q], points[q+1]).draw(graph)
    return graph
        
    

def main():
    win = GraphWin('Control', 400, 400)
    win.setCoords(0.0, 0.0, 2.0, 2.0)
    win.setBackground('white')

    Text(Point(0.25, 1.75), 'X start: ').draw(win)
    Text(Point(0.25, 1.5), 'X end: ').draw(win)
    Text(Point(1.25, 1.75), 'Y start: ').draw(win)
    Text(Point(1.25, 1.5), 'Y end: ').draw(win)
    Text(Point(0.40, 0.75), 'F(x): ').draw(win)
    
    x_start = Entry(Point(0.85, 1.75), 10)
    x_start.setText('0.0')
    x_start.draw(win)

    x_end = Entry(Point(0.85, 1.5), 10)
    x_end.setText('0.0')
    x_end.draw(win)

    y_start = Entry(Point(1.75, 1.75), 10)
    y_start.setText('0.0')
    y_start.draw(win)

    y_end = Entry(Point(1.75, 1.5), 10)
    y_end.setText('0.0')
    y_end.draw(win)

    f_of_x = Entry(Point(1.0, 0.75), 20)
    f_of_x.setText('Enter a function')
    f_of_x.draw(win)

    t = Text(Point(1.0, 0.25), 'Plot f(x)')
    t.draw(win)
    Polygon(Point(0.75, 0.15), Point(0.75, 0.35), Point(1.25, 0.35), Point(1.25, 0.15)).draw(win)
    win.getMouse()
    plot_it(x_start, x_end, y_start, y_end, f_of_x)
    t.setText('Close window')
    win.getMouse()
    win.close()
    graph.close()
    
main()
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by jaymeaux77
0

When you return an object it isn't just automagically added to the current namespace... you need to actually store it in something.

All you'll need to do is say graph = plot_it(...) , savvy?

0

Ah thanks. That was pretty easy haha. Thanks very much for the help though it is very appreciated.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.