Make a background transparent in Tkinter - python

while developing my app I finally succeeded in making everything I want to, the latest being attaching a background image to the app window. My only issue now is with the background of the labels, they are covering the background making the app ugly.
I tried using:
window.wm_attributes('-transparentcolor',color)
but something weird happened, the whole thing became transparent and It's not my desired outcome as you can see :
Picture here
anyone knows how to get around this or how to fix it?
been looking everywhere but can't really find a solution to my issue

I had tried this but I think you had already done this before,
from tkinter import *
from PIL import Image,ImageTk
main=Tk()
photo=Image.open("Eujbx.png")
photo = ImageTk.PhotoImage(photo)
aa=Label(main,image=photo)
aa.pack(expand=True,fill=BOTH)
title=Label(aa,text="This app is to look for homework",bg='#9e87b9')
title.pack(pady=10)
new=Entry(aa,bg="#9e87b9")
new.pack()
Button(aa,bg="#9e87b9",text="Hlw").pack()
main.mainloop()
If you had not tried this yet then try it. It could help you.
Steps(Explanation):
Using PIL to get the image,
Then change image to ImageTk which we can use,
Make Label which store the image and show in the background,
pack that with expandable form and fill Both side(X and Y),
Create the label and other stuff,
put all of them inside the main Label which contain background image,
give them background color of color of background image.

Related

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.

Is it possible to remove background on tkinter image

Here is my code.
png image without background
Here is a result. Is it even possible to remove image background in tkinter or I have to make same color as on image?
Looks like you have the basic grey background to you're added picture by tkinter.
So try if it is possible to change you're backround from grey to for example red with:
tag: bg="red", if that is possible, you can just change the color to the blue background.
Label(root,image=self.img, bg="blue")
I tested it and it worked out for me.
Its not possible as far as i know to use rgb() or the hexadezimal with the transperancy option.
Gave me back errors.
Its not really transperent but kind of a solution !
Good Luck !

How can I display an image and my code keeps running in the background?

I take part in a project in which we are making a sudoku solver. I want to print the image of the solved sudoku grid on the screen while our drawing table is drawing the solution on the paper grid.
But I can't find a way to display an image and my code keeps running.
I have looked into - I think - all of the opencv and matplotlib.pyplot functions to display images but every time the code stops when the image is displayed and continues once the image is closed (plt.show() or using cv2.waitKey()).
So if anyone has an idea of a way to display an image while the python code keeps running, I'd be glad to hear it.
Thanks
The PIL/Pillow Image.show() method will leave your image showing on the screen and your code will continue to run.
If you have a black and white image in a Numpy/OpenCV array, you can make it into a PIL Image and display it like this:
from PIL import Image
Image.fromarray(NumpyImg).show()
If your image is colour, you'll need to go from BGR to RGB either using cv2.cvtColor(...BGR2RGB..) or by reversing your 3rd channel something like (untested):
Image.fromarray(NumpyImg[:,:,::-1]).show()

tkinter making background image as semi-transparent under all widgets

First image:
Second image:
The first image shows what I'm getting at this moment.
But I just want to make it as the 2nd image which is looking transparent for all widgets. Apparently tkinter widgets does not support transparency. How can I make my GUI like second image?
Any help would be appreciated.
I know question is asked long ago, but I(and probably still many beginners like myself) was bothered with same thing and I came up with some kind of solution.
I've just cut part of background of exact place where button is placed with exact measurements and I achieved something similar to transparency by placing that cropped image as background of a button, label....
Not a "coded" solution but worked for me.

How to crop a region selected with mouse click, using Python Image Library?

Is there a way to select a region in a image with mouse click and crop these region using python PIL ? How can i do it?
Thanks
the answer at this post is pretty well developed:
Image Cropping using Python
it uses pygame as the GUI.
The PIL library itself provides no GUI code --what you are asking for is an application with a GUI. I'd suggest using Tkinter + PIL, but there is no way it is trivial - you will have to handle the mouse clicks, create a rectangle object tracking it, have a way to "reset" the rectangle, and so on.
Unfortunatelly the Canvas Tkinter widget which is used to draw things on is poorly documented - you will have to read trough it here:
http://www.pythonware.com/library/tkinter/introduction/canvas.htm
Bellow there is an example code that reads an image file from the disk and draws it on a tkinter window. As you can see, here is some object juggling to get it right.
import Tkinter
import Image, ImageTk, ImageDraw
image_file = "svg.png"
w = Tkinter.Tk()
img = Image.open(image_file)
width, height = img.size
ca = Tkinter.Canvas(w, width=width, height=height)
ca.pack()
photoimg = ImageTk.PhotoImage("RGB", img.size)
photoimg.paste(img)
ca.create_image(width//2,height//2, image=photoimg)
Tkinter.mainloop()
I tried to make a little tool like this to run in the Jupyter ecosystem, so that you could crop the images and then use the cropped results later on in the same notebook. It's designed to work for many images, to let you crop each of them sequentially. Check it out and see if it works for you. Can install via pip install interactivecrop, but you'll want to check out the blog post for usage instructions.

Categories