[Tutor] One question......

Asrarahmed Kadri ajkadri at googlemail.com
Fri Nov 24 17:03:45 CET 2006


Hi Folks,

I am constructing a bar-chart using Tkinter. The function takes a list
'data' and draws horizontal bar for each value.
Now what I want is  the the canvas widget should be able to handle variable
number of data-items. I cannot figure out how to do that, because I have
hard coded the lengths of X and Y axes. I want to make the entire function a
lot more flexible so that it determines the length of both the axes on the
basis of data supplied to it.
Do you know how can I get around with this issue.  The code for bar-chart is
as under:


from Tkinter import *
import tkFont


def draw_rect(canvas,data,x1,y1,x2,y2):

    f_obj = tkFont.Font(family='Arial',size=7,slant='italic')
    l = len(data)
    for i in data:

        y1 = y1 - 30
        x2 = x1 + (3 * i)
        y2 = y2 - 30
        canvas.create_rectangle(x1,y1,x2,y2)
        canvas.create_text(x2+5,y2,text=i,font=f_obj,anchor=NW)
        canvas.create_text(105,y1,text='21-11 at 12:00:00',font=f_obj)



data = [10,20,100,0,50,40,30,79]
root = Tk()

frame = Frame(root)
frame.pack()

canvas = Canvas(frame,height=500,width=500)

canvas.create_line(150,350,450,350,width=2)   # X-axis
canvas.create_line(150,350,150,50,width=2)    # Y-axis
canvas.pack()

draw_rect(canvas,data,x1=150,y1=340,x2=250,y2=330)

root.mainloop()
Thanks a lot.
Best Regards,
Asrarahmed Kadri


-- 
To HIM you shall return.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061124/43566afb/attachment.html 


More information about the Tutor mailing list