Search code examples
pythontkinterpaint

Keep getting error "_tkinter.TclError: can't parse color "115" when attempting to draw circle onto PhotoImage in Tkinter through mouse input


from tkinter import *
import tkinter as tk
import math
class Paint():
    def __init__(self):

        self.window=Tk()
        self.sizex=500
        self.sizey=500
        self.default_pen_size=10

        self.canvas = Canvas(self.window, width=self.sizex, height=self.sizey, bg = "white")
        self.canvas.pack()
        self.img = PhotoImage(width=self.sizex, height=self.sizey)
        self.canvas.create_image((self.sizex/2, self.sizey/2), image=self.img, state="normal")
        self.canvas.bind("<Button-1>", self.color_in)
        self.canvas.bind("<B1-Motion>", self.color_in)
        self.window.mainloop()
    def color_in(self, event):
        self.img.put("black", (event.x , event.y))
        radius_sqrt=math.sqrt(self.default_pen_size)
        circle=self.canvas.create_oval(event.x - radius_sqrt, event.y - radius_sqrt, event.x + radius_sqrt,
        event.y + radius_sqrt, fill="black")
        self.img.put(circle)

if __name__=='__main__':
    paint=Paint()

In my code above, I'm trying to draw a circle onto a Photoimage object using the canvas.create_oval method. Its working so far, but it keeps throwing the exception "_tkinter.TclError: can't parse color "115"". Any idea whats causing this?


Solution

  • The problem is this line of code:

    self.img.put(circle)
    

    The first argument to put needs to be a color or list of colors. You're passing it the id of the circle object that was created two lines earlier, and an id is not a color.

    I don't know what you think that line is supposed to do, but my guess is that you can just remove it without replacing it with anything else.