parabola

Salvatore salvatore.didio at wanadoo.fr
Tue Nov 8 12:54:42 CET 2005


Here is an old piece of code I wrote to test Tkinter

from Tkinter import *

import string
import Numeric
from Canvas import Line
import math

class Tableau(Canvas):
    def
__init__(self,master=None,size=400,col='black',colrep='red',colgrid='grey'):

Canvas.__init__(self,master,width=size,height=size,bg=col,highlightthickness=0)
        self.pack(expand=1,fill=BOTH)
        self.width_table = 0; self.height_table = 0
        self.nl = 20.0
        self.bind("<Configure>",self.Redraw)
        self.colrep = colrep
        self.colgrid = colgrid

    def GetSize(self,event):
        self.width_table = event.width
        self.height_table = event.height

    def SetBgColor(self,color):
        self['bg'] = color

    def ChangeColGrid(self,col):
        self.colgrid = col
        self.itemconfig('grid',fill=col)

    def ChangeColAxe(self,col):
        self.colrep = col
        self.itemconfig('repere',fill=col)

    def Redraw(self,event):
        self.delete('all')
        self.TraceAxe()
        self.UpdateGraphe()
        self.update()

    def Redessine(self):
        self.delete('all')
        self.TraceAxe()
        #self.UpdateGraphe()
        self.update()

    def TraceAxe(self):
        self.delete('all')
        w , h = self.winfo_width(),self.winfo_height()

        #Trace Grille
        posx=0;posy=0
        intervallex = w/self.nl
        intervalley = h/self.nl
        posx=intervallex;posy=intervalley
        for i in range(0,self.nl):

self.create_line(posx,0,posx,h,fill=self.colgrid,tag=('repere','grid'))

self.create_line(0,posy,w,posy,fill=self.colgrid,tag=('repere','grid'))
            posy += intervalley
            posx += intervallex
        #Trace des axes
        posx = w/2.0; posy = h/2.0

self.create_line(posx,0,posx,h,fill=self.colrep,tag=('repere','axe'))

self.create_line(0,posy,w,posy,fill=self.colrep,tag=('repere','axe'))


    def UpdateGraphe(self):
        for f in F.keys():
            try:
                name,color = F[f]
                Function(f,nom=name,col=color)
            except:
                Function(f)

class Function:
    def __init__(self,fonction = '',nom='affine',col='yellow',epaiss =
1):
        w , h = t.winfo_width(),t.winfo_height()
        self.valeur = eval('lambda x: '+fonction)
        self.x = Numeric.arange(-200.0,200.0)
        self.y = 20*self.valeur(self.x/20.0)
        res = Numeric.arange(0.0,800.0)
        res.shape = (400,2)
        res[:,0] = self.x*w/400.0 + w/2.0
        res[:,1] = -self.y*h/400.0 + h/2.0
        Line(t,res.tolist(),fill=col,tag=nom,width=epaiss)

def expression(exp):
    return eval('lambda x: '+exp)


if __name__ == '__main__':
    print "test"
    sin = Numeric.sin
    F =
{'x*x':('carre','green'),'2*sin(x)':('sin','blue'),'x*x*x':('cube','yellow')}
    t = Tableau()
    t.delete('grid')
    t.mainloop()




More information about the Python-list mailing list