Search code examples
pythontkinterbackgroundlabel

Problem with background labels Python Tkinter


I'm making a basic window with three labels. The problem is that those labels have white background, and I don't want that. I want that labels become without any background, but I haven't got any idea. If somebody knows how to fix it, please let me know.

PD: I used canvas, but there were a lot of problems with that, however I think that make a canvas is the solution, but I don't know how to do it

Code:

from tkinter import *
import winsound
from winsound import *
from tkinter import messagebox
import time

#creamos la ventana

raiz=Tk()

raiz.title("Title")
raiz.geometry("900x550")
raiz.resizable(0,0)
raiz.iconbitmap("C:\\Users\\user\\Desktop\\Game\\descarga.ico")
#winsound.PlaySound('C:\\Users\\user\\Downloads\\pygame\\mu\\sound.wav', winsound.SND_ALIAS | winsound.SND_ASYNC)

#----------------------aa
def ventanas(frame):
    frame.tkrise()

def jugar():
    messagebox.showinfo("Próximamente...")

def quitar():
    if messagebox.askokcancel("Saliendo...", "¿Estas seguro de que quieres salir?"):
        raiz.destroy()

def clickDerecho(event):
    jugar()

def clickDerecho2(event):
    quitar()

#frames--------------------------------------------------------------------

frameJugar = Frame()

fondo = PhotoImage(file= r'C:\Users\user\Desktop\game\background.png')
background_label = Label(raiz, image=fondo)
background_label.place(x=0, y=0, relwidth=1, relheight=1)


texto = Label(raiz, text="JUGAR" ,bd=0, font=("COMIC SANS MS", 30, "italic"), fg="#00deff", cursor="hand2")
texto.bind("<Button-1>", clickDerecho)
texto.place(x=100, y=196)

texto2 = Label(raiz, text="TUTORIAL" ,bd=0, font=("COMIC SANS MS", 30, "italic"), fg="#00deff", cursor="hand2")
texto2.bind("<Button-1>", clickDerecho)
texto2.place(x=100, y=306)

texto3 = Label(raiz, text="SALIR" ,bd=0, font=("COMIC SANS MS", 30, "italic"), fg="#00deff", cursor="hand2")
texto3.bind("<Button-1>", clickDerecho2)
texto3.place(x=100, y=416)

#-----------------------------------------------------------------ejecutamos la ventana

raiz.protocol("WM_DELETE_WINDOW", quitar)
raiz.mainloop()

Solution

  • You can try setting the background colour using the following code:

    label.config(bg="gray")
    

    you can try to make the background colour transparent:

    root.wm_attributes('-transparentcolor', root['bg'])
    

    to make the default color transparent or just use canvas, which should be something like

    canvas.create_text(x, y, text="Some text", ...)

    I have seen somebody doing some workaround using PIL, but its in German: https://www.python-forum.de/viewtopic.php?t=20573

    Anyways, what you plan to do seems to be quite complex using tkinter. Maybe you should simply choose a different GUI library, in case you plan to add more fancy functions to your interface.

    Edit:

    So why don't you try this:

    import Tkinter as    tk
    from   PIL    import Image, ImageTk, ImageDraw
    from   os     import listdir,curdir
    
    class BlendedRectangle(object):
    
    def __init__(self, xpos=0, ypos=0, width=10, height=10, image=None,
            fill='black', intensity=1.0):
    
            self.xpos = xpos
            self.ypos = ypos
            self.width = width
            self.height = height
            self.image = image
            self.fill = fill
            self.intensity = intensity
    
    
            self.coords = (self.xpos, self.ypos, self.xpos+self.width,
                self.ypos+self.height)
    
    
            self.bottom_image = self.image.crop(self.coords)
    
    
            self.top_image = self.image.crop(self.coords)
    
            self.draw = ImageDraw.Draw(self.top_image)
    
            self.draw.polygon([
                (0, 0), (self.width, 0), (self.width, self.height),
                (0, self.height), (0, 0)], fill= self.fill)
    
            self.blended_graphic_obj = Image.blend(self.bottom_image,
                self.top_image, self.intensity)
    
            self.image.paste(self.blended_graphic_obj, (self.xpos , self.ypos))
    
            self.tk_image  = ImageTk.PhotoImage(self.image)
    
    root = tk.Tk()
    
    image = Image.open("my_image.jpg")
    image_width, image_height = image.size
    
    canvas = tk.Canvas(root, width=image_width, height=image_height)
    canvas.pack()
    
    image_obj = BlendedRectangle(10, 10, 50, 50, image, 'red', intensity=0.3)
    canvas.create_image(0, 0, image=image_obj.tk_image, anchor='nw')
    
    root.mainloop()