0
votes

I am pretty new to Python and coding in general. I have been working on a program that is similar in nature to ms paint. So far, I've added the capabilities to create multi-colored rectangles, lines, ovals, and really any polygon.

I've been using the tkinter GUI. I've been wanting to add a fill command, but I'm kind of stuck as to how to start it. My idea for how it would work would be that it would check the color of the pixel the user is currently hovering over, then check up, down, left, and right for the same color in pixels. If it found that, it would change the color of those pixels (I guess by creating a really small rectangle object?). This would theoretically be able to fill an area. But, I really can't find anything on how to access the color of a pixel in tkinter.

I know the location is event.x and event.y for a specific event, but I can't find anything about pixel color. I don't really have any code written for it yet because I am unsure that tkinter can even access the color of a pixel and not just object colors.

1
Really depends on how you are doing the drawing. If you are using a PIL image as the drawing canvas then you can directly convert that to a numpy array and do exactly what you described. If you aren't, I would recommend you switch to a PIL image, as it will make things like saving and scaling the image a lot easier. - Novel
This is called "Flood fill" and isn't all to hard to implement. I wrote an example of this here (although it's in pygame). The problem is that you'll probably have performance problems doing this manually in python. For it to work in tkinter, you'd have to create an image to manipulate and to blit onto the window. It's a bit tedious and it's probably better to use a library such as pygame, pyglet, or maybe kivy instead. - Ted Klein Bergman

1 Answers

0
votes

Unfortunately, this isn't possible. I did some searching around, and found several other similar questions, but the general idea is that Tkinter does not support such a feature. It makes sense, considering that Tkinter is a GUI library.

I saw a suggestion somewhere, where an idea was proposed to create 1x1 rectangles using the Tkinter Canvas to basically mimic pixels. However, this method eventually leads into performance issues and lagging, so it's not really recommended either.

You may want to try exploring some other libraries to work together with Tkinter. You can keep the Tkinter GUI, but use an image manipulation library or something similar which integrates well with Tkinter, for the actual pixel drawing.