# Draw Math Curve

```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()

```

0人收藏

0

0

»更多 您可能感兴趣的代码
1. 2016-11-08 09:02:07Draw Rectangle by 阮小七
2. 2017-03-05 14:15:26Set Button style: font by 风云轩
3. 2015-12-07 19:24:39Load bitmap into a Canvas by 山药
4. 2016-01-29 16:07:13Label font by 阮小七
5. 2016-10-29 09:05:11Draw rectangle with filled color by jack.chen
6. 2015-11-26 18:29:34Draw shape border by Solon.Ring
7. 2016-08-18 11:12:58Draw a Oval with fill by 大猪
8. 2016-10-04 09:53:51Fill shape color by 阮小七
9. 2015-02-23 09:40:23ubuntu安装python3的python-pip by 甄码农
10. 2015-12-18 09:09:05Arc with start degree and end degree by 司马
11. 2016-01-24 21:02:43Draw oval using Python by 阮小七