user crate oval shape in tkinter

Viewed 43

I want to let user to draw an oval shape in the canvas of tkinter.

current_x = 0
current_y = 0


def select_color(col):
    global pen_color
    pen_color = col


def locate_xy(work):
    global current_x, current_y
    current_x = work.x
    current_y = work.y


def addLine(work):
    global current_x, current_y

    canvas.create_line((current_x, current_y, work.x, work.y), width=pen_size.get(), fill=pen_color,
                       capstyle=ROUND,
                       smooth=TRUE)
    current_x, current_y = work.x, work.y

def addoval(work):
    global current_x, current_y
    canvas.create_oval(current_x, current_y, work.x, work.y,fill= pen_color,outline=pen_color)
    current_x, current_y = work.x, work.y

the create_line works just fine. but the oval return this error:

TypeError: addoval() missing 1 required positional argument: 'work'

I call it later :

oval_shape = PhotoImage(file="oval.png")
oval_draw = Button(window, image=oval_shape, bg="#f2f3f5", command=addoval, relief=RIDGE)
oval_draw.place(x=3, y=343)

it's a button that when user click it the addoval will be activated

1 Answers

Try this.

from tkinter import Tk
from tkinter import Canvas
 
window = Tk()
window.geometry("400x400")
window.configure(background = "grey")
window.title("Canvas - Draw Shapes")
window.resizable(False, False)
 
# setting up the canvas
canvas = Canvas(width = 350, height = 350, bg = "white")
canvas.pack(pady = 20)
 
canvas.create_text(175, 20, text = "Circle", font = ("Arial", 30))
canvas.create_oval(175, 100, 100, 275, width = 3)
 
window.mainloop()

Output:

enter image description here

Related