Creating a "Fill" command in tk inter - python

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.

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.

Related

2d graphics in Python Kivy, advice is needed

I still have been studying world of Kivy and I have encountered with question. I am designing an interface. it has to have an appearance of analog device with arrow and curved scale like old voltmeters were. Just to display a data on the screen. I've tried to use canvas, but I stacked with resizing of window properties. Built canvas object was either connected to specific size in px, or changed location on the screen in wrong way. So, I am curious, maybe here are some others ways to embed objects which were built in another 2d graphics library, I am aware of existence of 3d module vpython and turtle. Maybe we have some more?
Can anybody give me a hint? I would be very grateful
kivy, canvas
view of my prototype
You can use Image widgets combined with Button Behavior or ToggleButton Behavior to make buttons that look like almost any kind of button that you like. And you can make a curved scale as described in this question.

How to get color of pixel on Tkinter Canvas in Python

Looking for a way to be able to find the color data of a pixel(s) on a Canvas. I'm sort of a beginner to coding, just gauging if this is possible/how to go about this. This is for a project I've been working on, and for part of it, it's necessary to compare the color data of a Canvas to an Image. I know how to find the per pixel color data of an Image, but I haven't found anything for a canvas. I'm using a Tkinter Canvas at the moment, but if it's not possible on that library, I'm open to switching to another library. Any ideas? Thanks guys!
Looking for a way to be able to find the color data of a pixel(s) on a Canvas.
Tkinter doesn't provide the ability to do this.

how to change the color of our pixels using tkinder?

i want to control every pixel of my tkinter program but when i search how, i just found out that the only solution is to use 'canvas' and even if it allows us to draw lines, i think i didnt find out how to change for exemple the color of the pixel (x=200, y=478)
and i m also afraid that when i use my code on a really big size of pixels, the program becomes slow with tkinter.. i m still a beginner for those who havent noticed it yet.. so i m curious to know if there is no tool in tinker that actually is used like it takes the x, y of the pixel and the color you wanna apply! i guess it should be possible bcs how either tkinter is supposed to be programmed.. (i m still a beginner ^^) anyways if this tool exists pls tell me about it and if it is supposed to be as slow as canvas i can actually just try a better code..
so i m curious to know if there is no tool in tinker that actually is used like it takes the x, y of the pixel and the color you wanna apply!
There is no such tool. Tkinter simply isn't designed to do this. If you need to control individual pixels in a GUI, tkinter is the wrong tool to use.

Fastest way of removing matplotlib artists from canvas embedded with wxpython

I have a python program where I have several matplotlib canvases embedded into a wxpython application. One of the canvases has many crosses in it. When the user right-click in this canvas the closest cross should be removed together with everything belonging to this cross (they are linked through an unique id-tag, and there might be things in every canvas that should be removed). I want the removing (or actually the replotting) to be as fast as possible. The program is quite large so I use several threads etc.
The easiest thing to implement this is to use wx.CallAfter(canvas.draw) for each canvas. However, there is a delay between the rightclick and the refresh of every canvas so I believe that canvas.draw() is too slow.
I saw two other functions for fast redrawing: the matplotlib functions blit() and draw_artist(). As far as I understand, blit() refreshes changed pixels inside some area (I used the axes bbox). I managed to get it to work with blit() in the sense that the program ran without crashing...but not updating what you see on the screen.
Did not manage to get draw_artist() to work when removing a pixel (tried using first line.remove(), then draw_artist(line) but the line was ofcourse already dead so draw_artist did not work).
Note: I called blit() and draw_artist() with wx.CallAfter()!
The feeling I have is that blit() is the best solution, but I did not manage to get it to update to the "screen-level". So my question is: what is the fastest and most resource-saving way of removing artists from matplotlib.canvases (embedded into wxPython) without redrawing more than you need to, but still let the change propagate to the screen?

How to make non-square edges in Tkinter?

In order to make one of my programs more aesthetically pleasing I'm using images to create the boarders, however I want to create a non square boarder so the program looks kinda like this
___________
/ /
/__________/
How should I go about this?
This is on windows 7, btw.
Edit:
A tried to make a pseudo-edge using transparency however it doesn't come out transparent. For some reason it cam out as a dark grey. I want the red to be the "edge".
The concept you are after is called a "shapped window". Search for "tk shaped window" with your favorite search engine. There is a tk extension that claims to support this, though I haven't personally tried it. I presume since it works with tcl/tk it can be made to work with Tkinter since Tkinter uses tcl/tk under the hood.
I'm not familiar with Tkinker, but you could make an image with transparency where you don't want the border, which would make pseudo-slanted edges. There is no way for a window to be non-square however (For a good reason too).

Categories