新浦京81707con > 首页 > Draw Math Curve

原标题:Draw Math Curve

浏览次数:150 时间:2019-06-19

from Tkinter import *
import math

root = Tk()

fram = Frame(root)
Label(fram,text='f(x):').pack(side=LEFT)
func = Entry(fram)
func.pack(side=LEFT, fill=BOTH, expand=1)
butt = Button(fram, text='Plot')
butt.pack(side=RIGHT)
fram.pack(side=TOP)

fram = Frame(root)
bounds = []
for label in 'minX', 'maxX', 'minY', 'maxY':
    Label(fram,text=label ':').pack(side=LEFT)
    edit = Entry(fram, width=6)
    edit.pack(side=LEFT)
    bounds.append(edit)
fram.pack(side=TOP)

c = Canvas(root)
c.pack(side=TOP, fill=BOTH, expand=1)

def minimax(values=[0.0, 1.0, 0.0, 1.0]):
    for i in range(4):
        edit = bounds[i]
        try: values[i] = float(edit.get())
        except: pass
        edit.delete(0, END)
        edit.insert(END, '%.2f'%values[i])
    return values

def plot():
    minx, maxx, miny, maxy = minimax()

    f = func.get()
    f = compile(f, f, 'eval')

    CX = c.winfo_width()
    CY = c.winfo_height()

    coords = []
    for i in range(0,CX,5):
        coords.append(i)
        x = minx   ((maxx-minx)*i)/CX
        y = eval(f, vars(math), {'x':x})
        j = CY*(y-miny)/(maxy-miny)
        coords.append(j)

    c.delete(ALL)
    c.create_line(*coords)

butt.config(command=plot)

f = 'sin(x)   cos(x)'
func.insert(END, f)
minimax([0.0, 10.0, -2.0, 2.0])

root.mainloop()

本文由新浦京81707con发布于首页,转载请注明出处:Draw Math Curve

关键词: 新浦京81707con Python 2D

上一篇:澳门葡京网址Random number

下一篇:没有了