473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Making Circles smaller via a loop

I am trying to write a program so when it runs, it makes one circle of say 50 radius, then the next one would be 45 and so on going down by 5 each time.

What I am unsure of is how can I do this with a loop or is it impossible. I look forwards to any help that I can get.
Nov 22 '11 #1

✓ answered by bvdet

Are you planning to use Tkinter or some other GUI interface? Your radiuses can be defined in a list comprehension.
Expand|Select|Wrap|Line Numbers
  1. >>> [rad for rad in range(50, 0, -5)]
  2. [50, 45, 40, 35, 30, 25, 20, 15, 10, 5]
  3. >>> 
Using Tkinter, you can create a canvas and place circles on the canvas. Here's an example:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. class Point(object):
  4.     def __init__(self, x=0.0, y=0.0):
  5.         self.x = float(x)
  6.         self.y = float(y)
  7.     def __add__(self, other):
  8.         return Point(self.x+other.x, self.y+other.y)
  9.  
  10. def drawCircles(columns, rows, radius=100, space=0):
  11.     root = Tk()
  12.     canvas_width = columns*(radius*2+space)
  13.     canvas_height = rows*(radius*2+space)
  14.  
  15.     w = Canvas(root, width=canvas_width, height=canvas_height)
  16.     # create a black background
  17.     w.create_rectangle(0,0,canvas_width,canvas_height,fill="black")
  18.     # calculate UL and LR coordinates in column 1, row 1
  19.     pt1 = Point(space/2.0, space/2.0)
  20.     pt2 = pt1+Point(radius*2, radius*2)
  21.     for i in range(0, columns):
  22.         for j in range(0, rows):
  23.             # xy = UL.x, UL.y, LR.x, LR.y
  24.             xy = pt1.x+i*(radius*2+space), \
  25.                  pt1.y+j*(radius*2+space), \
  26.                  pt2.x+i*(radius*2+space), \
  27.                  pt2.y+j*(radius*2+space)
  28.             # create a circle with red fill
  29.             w.create_oval(xy, fill='red')
  30.  
  31.     w.pack()
  32.     root.mainloop()
  33.  
  34. if __name__ == '__main__':
  35.     drawCircles(4, 3, 125)
  36.     drawCircles(4, 2, 75, 20)
  37.     drawCircles(4, 2, 75, 100)
  38.     drawCircles(24, 16, 12, 6)
  39.  

1 1913
bvdet
2,851 Expert Mod 2GB
Are you planning to use Tkinter or some other GUI interface? Your radiuses can be defined in a list comprehension.
Expand|Select|Wrap|Line Numbers
  1. >>> [rad for rad in range(50, 0, -5)]
  2. [50, 45, 40, 35, 30, 25, 20, 15, 10, 5]
  3. >>> 
Using Tkinter, you can create a canvas and place circles on the canvas. Here's an example:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. class Point(object):
  4.     def __init__(self, x=0.0, y=0.0):
  5.         self.x = float(x)
  6.         self.y = float(y)
  7.     def __add__(self, other):
  8.         return Point(self.x+other.x, self.y+other.y)
  9.  
  10. def drawCircles(columns, rows, radius=100, space=0):
  11.     root = Tk()
  12.     canvas_width = columns*(radius*2+space)
  13.     canvas_height = rows*(radius*2+space)
  14.  
  15.     w = Canvas(root, width=canvas_width, height=canvas_height)
  16.     # create a black background
  17.     w.create_rectangle(0,0,canvas_width,canvas_height,fill="black")
  18.     # calculate UL and LR coordinates in column 1, row 1
  19.     pt1 = Point(space/2.0, space/2.0)
  20.     pt2 = pt1+Point(radius*2, radius*2)
  21.     for i in range(0, columns):
  22.         for j in range(0, rows):
  23.             # xy = UL.x, UL.y, LR.x, LR.y
  24.             xy = pt1.x+i*(radius*2+space), \
  25.                  pt1.y+j*(radius*2+space), \
  26.                  pt2.x+i*(radius*2+space), \
  27.                  pt2.y+j*(radius*2+space)
  28.             # create a circle with red fill
  29.             w.create_oval(xy, fill='red')
  30.  
  31.     w.pack()
  32.     root.mainloop()
  33.  
  34. if __name__ == '__main__':
  35.     drawCircles(4, 3, 125)
  36.     drawCircles(4, 2, 75, 20)
  37.     drawCircles(4, 2, 75, 100)
  38.     drawCircles(24, 16, 12, 6)
  39.  
Nov 22 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Gregory | last post by:
Hello, I've managed to build two web pages, one that can display images with associated text data in a table, and one that can resize and display images without the text. I'd like to resize the...
36
by: Remi Villatel | last post by:
Hi there, There is always a "nice" way to do things in Python but this time I can't find one. What I'm trying to achieve is a conditionnal loop of which the condition test would be done at...
8
by: Terry Olsen | last post by:
How do I loop back to the beginning of a for/next loop before getting to the end of it? Isn't there an "iterate" command or something like that? For Each This in That ...code if This = False...
3
by: triplejump24 | last post by:
Hey. Im trying to make program that basically displays all the prime numbers. I need to use bool and for but im not quite sure if i have this right. So far i have this bunch of a mess but can...
6
by: Crcullen3916 | last post by:
Hi, I have this assignment that I have almost finished except I have one problem. I'm having trouble making my for loop find the sum of the first n odd numbers ( n being the variable in which a value...
26
by: a.mil | last post by:
I am programming for code-speed, not for ansi or other nice-guy stuff and I encountered the following problem: When I have a for loop like this: b=b0; for (a=0,i=0;i<100;i++,b--) { if (b%i)...
3
by: danielkun | last post by:
I'm building a simple slideshow and would like to exchange the FOR loop below into an infinite loop that will start over with picure 1 when picture 5 has been shown. Im still new to C and I hope...
1
by: z1 | last post by:
hi- i wrote a program with java using eclipse and i think i remember it used 70,000k in the process list in windows in task manager. i understand that there is some eclipse runtime overhead...
5
by: ronparker | last post by:
First of all, thank you for taking the time to read my question. With the following code, what I have done is make a database in MySQL and created a table using python. I then imported an excel...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.