[Tutor] small (I hope) questions

Michael P. Reilly arcege@speakeasy.net
Tue, 5 Jun 2001 07:50:53 -0400 (EDT)


Isaac Hall wrote
> first, let me introduce myself a little as I am new to this list (but

Welcome.

> I may well be widely known as the guy with all the dumb questions
> soon...I hope not)  Ive just begun working with Python and Tkinter in
> physics applications, and Im getting stuck in some fairly minor aspects
> of this.  I have previously only programmed in C/C++.  anyway my
> question(s) are:
>     a) I am trying to produce a pie chart of just random numbers in a
> list to get started with this, however Im confused as to how to call up
> a canvas, and draw the chart.

All you need is a little math to get the values properly set up (but then
this will be the same with just about any package.

Here is a segment to help you start:
size = (200, 200)
graph = Canvas(graphicframe, height=size[0], width=size[1])
# get the total representing the circumference
sum = reduce(operator.add, values)
# starting angle
lastangle = 0.0
for i in range(len(values)):
  item = values[i]
  # how much of the pie do we eat up
  percentage = (item / sum)
  # calculate ending angle based on that percentage
  newangle = lastangle + (percentage * 360.0)
  # create a new pie arc in the circle
  # the "get_indexed_color()" function is something you'll need to
  # write, you should get a unique color for each value, probably
  # not repeat the colors, and likely start with the primary colors
  # (red, yellow, blue) first
  graph.create_arc(0, 0, size[0], size[1],
    start=lastangle, extent=newangle,
    fill=get_indexed_color(i),
    style='pieslice'
  )
  lastangle=newangle

You probably have to tweek things to get the details to work (I'm not
feeling well, so not going to fully debug this, sorry), but all you need
is a little geometry.

>     b) in addition to the pie chart, I also need to put these numbers in
> a table (which I have figured out, however I would like to be able to
> create this so that the pie chart comes up first, and then clicking on
> the pie chart will display the table.

Do you want a new window with the table (look at Toplevel)?  Do you
want a new area of the window (with the pie chart) to display the table
(you can just use pack for this)?  The methods for these are different.

  -Arcege

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |