通过循环缩小圆圈
我正在试着写一个程序,当它运行时,它做一个半径为50的圆,然后下一个圆将是45,依此类推,每次递减5。
我不确定的是,我怎么才能用循环来做这件事,或者这是不可能的。我期待着我能得到的任何帮助。
# 回答1
你打算使用Tkinter或其他一些图形用户界面吗?你的半径可以在列表理解中定义。
你打算使用Tkinter或其他一些图形用户界面吗?你的半径可以在列表理解中定义。
选择 | 换行 | 行号
- >>> [rad for rad in range(50, 0, -5)]
- [50, 45, 40, 35, 30, 25, 20, 15, 10, 5]
- >>>
使用Tkinter,您可以创建画布并在画布上放置圆圈。下面是一个例子:
选择 | 换行 | 行号
- from Tkinter import *
- class Point(object):
- def __init__(self, x=0.0, y=0.0):
- self.x = float(x)
- self.y = float(y)
- def __add__(self, other):
- return Point(self.x+other.x, self.y+other.y)
- def drawCircles(columns, rows, radius=100, space=0):
- root = Tk()
- canvas_width = columns*(radius*2+space)
- canvas_height = rows*(radius*2+space)
- w = Canvas(root, width=canvas_width, height=canvas_height)
- # create a black background
- w.create_rectangle(0,0,canvas_width,canvas_height,fill="black")
- # calculate UL and LR coordinates in column 1, row 1
- pt1 = Point(space/2.0, space/2.0)
- pt2 = pt1+Point(radius*2, radius*2)
- for i in range(0, columns):
- for j in range(0, rows):
- # xy = UL.x, UL.y, LR.x, LR.y
- xy = pt1.x+i*(radius*2+space), \
- pt1.y+j*(radius*2+space), \
- pt2.x+i*(radius*2+space), \
- pt2.y+j*(radius*2+space)
- # create a circle with red fill
- w.create_oval(xy, fill='red')
- w.pack()
- root.mainloop()
- if __name__ == '__main__':
- drawCircles(4, 3, 125)
- drawCircles(4, 2, 75, 20)
- drawCircles(4, 2, 75, 100)
- drawCircles(24, 16, 12, 6)
标签: python