user interface - Python tkinter: plot function with canvas, HOMEWORK -
an assignment uni asks me use tkinter create gui program takes function, color, start point, end point , how many steps take between (line drawn between each step).
i have had decent go @ it. have got entire layout , frame finished (basically) finding few things little hard.
we have been given support code.
from tkinter import * import tkmessagebox tkcolorchooser import askcolor math import * class functionerror(exception): """a simple function error exception produced make_function function invalid function definitions. """ pass def make_function(text): """take string representing function in x , return corresponding function. functionerror exception thrown if text not represent valid function. make_function(string) -> (float -> float) """ try: exec 'f = lambda x: ' + text 1+f(2.0) ## test see if there errors in definition except zerodivisionerror: ## ignore 0 division errors pass except: raise functionerror() return f class functioniterator(object): """an iterator object intended used produce (x,y) pairs of function constructor: functioniterator(function, startx, endx, steps) function function being iterated on startx x value start iteration endx x value @ end of iteration steps number of iterations. assumes if division 0 occurs @ given x won't occur 'close to' x example: assume square function defined square number - i.e. square(x) = x*x >>> list(functioniterator(square, 0.0, 5.0, 5)) [(0.0, 0.0), (1.0, 1.0), (2.0, 4.0), (3.0, 9.0), (4.0, 16.0), (5.0, 25.0)] , x,y in functioniterator(square, 0.0, 5.0, 5): print x,y produces output 0.0 0.0 1.0 1.0 2.0 4.0 3.0 9.0 4.0 16.0 5.0 25.0 note: functions undefined @ given x (e.g. log(-1)) y value returned set 10000000 """ def __init__(self, f, startx, endx, steps): self._f = f self._startx = startx self._endx = endx self._delta = (endx-startx)/steps def __iter__(self): self._x = self._startx self._zero = false return self def next(self): if self._x > self._endx + self._delta/2: raise stopiteration else: x = self._x try: y = self._f(x) self._x += self._delta return (x, y) except zerodivisionerror: if x == self._startx: x += self._delta/2 y = self._f(x) self._x += self._delta return (x, y) elif self._zero: x += self._delta/2 y = self._f(x) self._x += self._delta self._zero = false return (x, y) else: x -= self._delta/2 y = self._f(x) self._zero = true return (x, y) except: self._x += self._delta return (x, 10000000) class worldscreen(object): """the worldscreen class used convert between real coodinates , screen coordinates. constructor: worldscreen(x1, y1, x2, y2, width, height) (x1, y1) : bottom left in real coords (x2, y2) : top right in real coords width : width of screen (canvas) height: height of screen (canvas) note: on canvas y increases move down canvas 'flip' occurs between real y values , screen values - see example below """ def __init__(self, x1, y1, x2, y2, width, height): self.x = x1 self.y = y1 self.xscale = (x2-x1)/width self.yscale = (y2-y1)/height self.height = height def world2screen(self, x, y): """return screen (canvas) coordinates given real coordinates. if calculated screen y coord outside screen appropriate 'edge value' returned. world2screen(float, float) -> (float, float) example: >>> ws = world_screen(0,0,10,10, 100.0, 100.0) >>> ws.world2screen(5, 5) (50.0, 50.0) >>> ws.world2screen(10, 10) (100.0, 0.0) note in case when y 10 in real coordinates (i.e. @ top y coord in canvas 0 (i.e. @ top) """ wy = self.height - (y - self.y)/self.yscale """ if wy < 0: wy = 0 elif wy > self.height: wy = self.height """ return ((x-self.x)/self.xscale, wy) def screen2world(self, x, y): """return real coordinates given screen (canvas)coordinates. screen2world(float, float) -> (float, float) example: >>> ws = world_screen(0,0,10,10, 100.0, 100.0) >>> ws.screen2world(50,50) (5.0, 5.0) >>> ws.screen2world(100,0) (10.0, 10.0) """ return (x*self.xscale + self.x, (self.height - y)*self.yscale + self.y)
this code
class pointframe(frame): """a simple application allow user ti enter expression , evaluate """ def __init__(self, master): """ a=simple expression evaluator """ frame.__init__(self, master) label(self, text='last point clicked: ').pack(side=left) label(self, text='').pack(side=left) label(self, text='cursor point: ').pack(side=left) label(self, text='').pack(side=left) class canvasapp(object): def __init__(self, master): master.title('canvas') self._canvas = canvas(master, bg='white', width=500, height=500) self._canvas.pack(side=top, expand=true, fill=both) class functionframe(frame): """a simple application allow user ti enter expressio n , evaluate """ def __init__(self, master): """ a=simple expression evaluator """ frame.__init__(self, master, relief=sunken, bg='#a5a5a5', pady=3) label(self, text='function in x: ', bg='#a5a5a5').pack(side=left) function = entry(self, width=35).pack(side=left, padx=2) button(self, text='select', command=self.select).pack(side=right, padx=4) colour = entry(self, width=15).pack(side=right) label(self, text='function colour: ', bg='#a5a5a5').pack(side=right, padx=2) def select(self): (rgb, hx)= askcolor() class plotframe(frame): """a simple application allow user ti enter expression , evaluate """ def __init__(self, master): """ a=simple expression evaluator """ entry_width = 10 frame.__init__(self, master, bg='#a5a5a5', pady=3) label(self, text='plot settings', bg='#a5a5a5').pack(side=left) label(self, text='start x:', bg='#a5a5a5').pack(side=left) start_x = entry(self, width=entry_width).pack(side=left) label(self, text='end x:', bg='#a5a5a5').pack(side=left) end_x = entry(self, width=entry_width).pack(side=left) label(self, text='start y:', bg='#a5a5a5').pack(side=left) start_y = entry(self, width=entry_width).pack(side=left) label(self, text='end y:', bg='#a5a5a5').pack(side=left) end_y = entry(self, width=entry_width).pack(side=left) label(self, text='steps: ', bg='#a5a5a5').pack(side=left) steps = entry(self, width=entry_width).pack(side=left) class buttonframe(frame): """a simple application allow user ti enter expression , evaluate """ def __init__(self, master): """ a=simple expression evaluator """ frame.__init__(self, master, bg='#cecef6') button(self, text='add function', command=self.add_function).pack(side=left) button(self, text='redraw all', command=self.redraw_all).pack(side=left) button(self, text='remove last function', command=self.remove_last).pack(side=left) button(self, text='remove functions', command=self.remove_all).pack(side=left) button(self, text='exit', command=self.exit_app).pack(side=left) def add_function(self): make_function(function) def redraw_all(self): pass def remove_last(self): pass def remove_all(self): pass def exit_app(self): self.master.destroy class plotapp(object): def __init__(self, master): master.title('assignment 2') pointframe(master).pack(side=top, expand=true, fill=both, pady=2) self._canvas = canvas(master, bg='white', width=500, height=500).pack(side=top, expand=true, fill=both, padx=8) functionframe(master).pack(side=top, expand=true, fill=both, pady=4) separator = frame(height=18, bd=1) separator.pack(fill=x) plotframe(master).pack(side=top, expand=true, fill=both, pady=4) separator = frame(height=10, bd=1) separator.pack(fill=x) buttonframe(master).pack(side=top, pady=5)
the things im having problems with;
how can use make_function function in support code check if function given in entry (function) function. sorry saying function much.
my first class - pointframe meant o show last clicked point in canval , current curser point in real world coordinates. meant use worldscreen function in support convert real world cords. how can this?
the select button uses tkcolorchooser pick color. how can make color come in color entry next it?
how can use color, function, start , end point , steps draw function. using functioniterator in support code? how can draw on new one, change color, delete , redraw all?
i realize alot. not asking me. after helps, hints , can me closer.
thank you
Comments
Post a Comment