Добавьте еще две кнопки – одну для рисования круга, другую для очистки полотна

Есть программа которая выводит треугольник и прямоугольник. Но нужно еще добавить функцию вывода круга и очистки полотна. Я писала примерно такой код, но сильно теряюсь, как реализовать поставленную задачу.

from tkinter import *
def triangle():
 canvas.coords(r, (0, 0, 0, 0))
 canvas.itemconfig(t, fill='yellow', outline='white')
 canvas.coords(t, (50, 200, 340, 200, 110, 60))
 text.delete(1.0, END)
 text.insert(1.0, 'Зображення трикутника')
 text.tag_add('title', '1.0', '1.end')
 text.tag_config('title', font=('Times', 14),
foreground='blue')
def rectangle():
 canvas.coords(t, (0, 0, 0, 0, 0, 0))
 canvas.itemconfig(r, fill='blue', outline='white')
 canvas.coords(r, (80, 50, 320, 200))
 text.delete(1.0, END)
 text.insert(1.0, 'Зображення прямокутника')
 text.tag_add('title', '1.0', '1.end')
 text.tag_config('title', font=('Times', 14), foreground='black')
def oval():
 canvas.coords(t, (0, 0, 0, 0, 0, 0))
 canvas.itemconfig(r, fill='blue', outline='white')
 canvas.coords(r, (80, 50, 320, 200))
 text.delete(1.0, END)
 text.insert(1.0, 'Зображення круга')
 text.tag_add('title', '1.0', '1.end')
 text.tag_config('title', font=('Times', 14), foreground='black')
win = Tk()
b_triangle = Button(text="Трикутник", width=15, command=triangle)
b_rectangle = Button(text="Прямокутник", width=15, command=rectangle)
b_oval = Button(text="Круг", width=15, command=oval)
canvas = Canvas(width=400, height=300, bg='#fff')
text = Text(width=55, height=5, bg='#fff', wrap=WORD)
t = canvas.create_polygon(0, 0, 0, 0, 0, 0)
r = canvas.create_rectangle(0, 0, 0, 0)
y = canvas.create_oval(50, 10, 150, 110)
b_triangle.grid(row=0, column=0)
b_rectangle.grid(row=1, column=0)
b_oval.grid(row=1, column=0)
canvas.grid(row=0, column=1, rowspan=10)
text.grid(row=11, column=1, rowspan=3)
win.mainloop()


Ответы (1 шт):

Автор решения: Никита

Я написал для вас программу (реализовал все что надо). Если что, фигуры можно удалять методом canvas.itemconfig(ФИГУРА, state='hidden'). Чтобы показать элемент снова state='normal'. Дам вам совет: делайте отступы между строками кода (делите программу на блоки), такой код читать будет в разы легче. Также не забывайте про такой полезный инструмент программиста как коментарии (в Python их можно писать после символа "#"). Удачи вам в изучении Python.

from tkinter import *


def triangle():
 canvas.itemconfig(r, state='hidden')
 canvas.itemconfig(y, state='hidden')

 canvas.itemconfig(t, fill='yellow', outline='white', state='normal')
 canvas.coords(t, (50, 200, 340, 200, 110, 60))
 text.delete(1.0, END)
 text.insert(1.0, 'Зображення трикутника')
 text.tag_add('title', '1.0', '1.end')
 text.tag_config('title', font=('Times', 14),
foreground='blue')

def rectangle():
 canvas.itemconfig(t, state='hidden')
 canvas.itemconfig(y, state='hidden')

 canvas.itemconfig(r, fill='blue', outline='white', state='normal')
 canvas.coords(r, (80, 50, 320, 200))
 text.delete(1.0, END)
 text.insert(1.0, 'Зображення прямокутника')
 text.tag_add('title', '1.0', '1.end')
 text.tag_config('title', font=('Times', 14), foreground='black')

def oval():
 canvas.itemconfig(t, state='hidden')
 canvas.itemconfig(r, state='hidden')

 canvas.itemconfig(y, fill='green', outline='white', state='normal')
 canvas.coords(y, (80, 50, 280, 250))
 text.delete(1.0, END)
 text.insert(1.0, 'Зображення кола')
 text.tag_add('title', '1.0', '1.end')
 text.tag_config('title', font=('Times', 14), foreground='black')

def clear():
 canvas.itemconfig(t, state='hidden')
 canvas.itemconfig(y, state='hidden')
 canvas.itemconfig(r, state='hidden')
 text.delete(1.0, END)

win = Tk()

b_triangle = Button(text="Трикутник", width=15, command=triangle)
b_rectangle = Button(text="Прямокутник", width=15, command=rectangle)
b_oval = Button(text="Коло", width=15, command=oval)
b_clear = Button(text="Очистити", width=15, command=clear)

canvas = Canvas(width=400, height=300, bg='#fff')
text = Text(width=55, height=5, bg='#fff', wrap=WORD)

t = canvas.create_polygon(0, 0, 0, 0, 0, 0)
r = canvas.create_rectangle(0, 0, 0, 0)
y = canvas.create_oval(0, 0, 0, 0)

b_triangle.grid(row=0, column=0)
b_rectangle.grid(row=1, column=0)
b_oval.grid(row=2, column=0)
b_clear.grid(row=3, column=0)

canvas.grid(row=0, column=1, rowspan=10)
text.grid(row=11, column=1, rowspan=3)
win.mainloop()
→ Ссылка