bitbyte
bitbyte

Reputation: 119

Tkinter Canvas.create_oval does not update color

I am trying to change the color of the line Im drawing on the canvas, with the function change_red. After clicking the buttons which runs the function the value from self.color changes to red, however the line still draws in black.

I've tried to change self.color in the init methode, but it still draws only in black.

from PIL import Image, ImageTk
import tkinter as tk

class Window(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.color = "black"
        menubar = tk.Menu(self)
        self.config(menu=menubar)
        operation_menu = tk.Menu(menubar, tearoff=0)
        config_menu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Change Color", menu=operation_menu)
        menubar.add_cascade(label="Help", menu=config_menu)
        config_menu.add_command(label="Exit", command=lambda: exit())
        operation_menu.add_command(label="Red", command=lambda: self.change_red())
        self.im = ImageTk.PhotoImage(Image.open("Test.png"))
        self.geometry("500x500")
        self.title("Image Editor")
        self.resizable(False, False)
        self.im_cv = tk.Canvas(width=300, height=200)
        self.im_cv.pack(expand="yes", fill="both")
        self.im_cv.create_image(50, 10, image=self.im, anchor="nw")
        self.im_cv.bind("<B1-Motion>", self.paint)

    def change_red(self):
        self.color = "red"

    def paint(self, event):
        print(self.color)
        x1, y1 = (event.x - 1), (event.y - 1)
        x2, y2 = (event.x + 1), (event.y + 1)
        self.my_canvas = self.im_cv.create_oval(x1, y1,x2,y2, fill=self.color, width=5)

window = Window()
window.mainloop()

Upvotes: 0

Views: 2234

Answers (1)

Miraj50
Miraj50

Reputation: 4417

You need to realize that the width property in create_oval functions is actually the width of the border around the outside of the ellipse. Default is 1 pixel. So, even though the color has changed, you are not able to see it visually. For that, you need to change the thickness of your ovals/ellipses or set width=0 i.e. no border. Here is an example :

#I didn't have ImageTk so I have changed it accordingly
import tkinter as tk

class Window(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.color = "black"
        self.t = 2
        menubar = tk.Menu(self)
        self.config(menu=menubar)
        operation_menu = tk.Menu(menubar, tearoff=0)
        config_menu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Change Color", menu=operation_menu)
        menubar.add_cascade(label="Help", menu=config_menu)
        config_menu.add_command(label="Exit", command=lambda: exit())
        operation_menu.add_command(label="Red", command=lambda: self.change_red())
        self.im = tk.PhotoImage(file="ex.png")
        self.geometry("500x500")
        self.title("Image Editor")
        self.resizable(False, False)
        self.im_cv = tk.Canvas(self, width=300, height=200)
        self.im_cv.pack(expand="yes", fill="both")
        self.im_cv.create_image(50, 10, image=self.im, anchor="nw")
        self.im_cv.bind("<B1-Motion>", self.paint)

    def change_red(self):
        self.color = "red"

    def paint(self, event):
        # print(self.color)
        x1, y1 = (event.x - self.t), (event.y - self.t)
        x2, y2 = (event.x + self.t), (event.y + self.t)
        self.my_canvas = self.im_cv.create_oval(x1, y1, x2, y2, fill=self.color, width=0)

window = Window()
window.mainloop()

Upvotes: 1

Related Questions