from tkinter import Tk, Canvas, Frame, BOTH
import random
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("Рисуем линии")
self.pack(fill=BOTH, expand=1)
canvas = Canvas(self)
for i in reversed(range(2, 11)):
if i == 2:
# центральный элемент
self.draw(canvas, [105, 200], i * 10, 'white', 'oval')
self.draw(canvas, [305, 200], i * 10, 'white', 'rect')
else:
self.draw(canvas, [105, 200], i * 10, self.get_color(), 'oval')
self.draw(canvas, [305, 200], i * 10, self.get_color(), 'rect')
canvas.pack(fill=BOTH, expand=1)
def get_color(self):
r = lambda: random.randint(0, 255)
return '#%02X%02X%02X' % (r(), r(), r())
def draw(self, canvas, center, size, color, figure):
x1 = center[0] - size
x2 = center[0] + size
y1 = center[1] - size
y2 = center[1] + size
if figure == 'rect':
canvas.create_rectangle(x1, y1, x2, y2, width=1, fill=color)
elif figure == 'oval':
canvas.create_oval(x1, y1, x2, y2, width=1, fill=color)
def main():
root = Tk()
ex = Example()
root.geometry("410x400+300+300")
root.mainloop()
if __name__ == '__main__':
main()