通过循环缩小圆圈

我正在试着写一个程序,当它运行时,它做一个半径为50的圆,然后下一个圆将是45,依此类推,每次递减5。
我不确定的是,我怎么才能用循环来做这件事,或者这是不可能的。我期待着我能得到的任何帮助。

# 回答1


你打算使用Tkinter或其他一些图形用户界面吗?你的半径可以在列表理解中定义。

选择 | 换行 | 行号
  1. >>> [rad for rad in range(50, 0, -5)]
  2. [50, 45, 40, 35, 30, 25, 20, 15, 10, 5]
  3. >>> 

使用Tkinter,您可以创建画布并在画布上放置圆圈。下面是一个例子:

选择 | 换行 | 行号
  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.  

标签: python

添加新评论