Skip to content
Advertisement

Labels with no background tkinter

I’m making a tkinter app with a background image and three labels.

The problem that I have is that these labels have a white background and I don’t want that. I want to make like a “png” label, with no background.

First, I tried to put this line of code:

raiz.wm_attributes('-transparentcolor' , raiz["bg"])

But it didn’t work, literally, the label was a hole.

Then I searched for a solution, and I saw that the best way to make labels with no background is using canvas, but there were more bugs, however, I think that’s the solution, but I don’t know how to do it.

This is my code:

from tkinter import *
import winsound
from winsound import *
from tkinter import messagebox
import time
 
#creamos la ventana
 
raiz=Tk()
 
raiz.wm_attributes('-transparentcolor', raiz['bg'])
 
raiz.title("Game")
raiz.geometry("900x550")
raiz.resizable(0,0)
raiz.iconbitmap('descarga.ico')
#winsound.PlaySound('C:\Users\user\Downloads\pygame\MY\sound.wav', winsound.SND_ALIAS | winsound.SND_ASYNC)
 
#----------------------aa
def ventanas(frame):
    frame.tkrise()
 
def jugar():
    messagebox.showinfo("Próximamente...", "Esta opción estará disponible 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'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()

EDIT: Now I’m testing with some code, like this:

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


#window--------------------------------------------------------------------------------
raiz=Tk()
raiz.title("Game")
raiz.geometry("900x550")
raiz.resizable(0,0)
raiz.iconbitmap(r"C:UsersuserDesktopGamedescarga.ico")
#winsound.PlaySound('C:\Users\user\Downloads\pygame\MY\sound.wav', winsound.SND_ALIAS | winsound.SND_ASYNC)

fondo = PhotoImage(file= r'C:UsersuserDesktopMyadoriginsbackground.png')
bg = Label(raiz, image=fondo)
bg.place(x=0, y=0)

x = 40  # Horizontal position in pixels.
y = 100 # Vertical position in pixels.
dx = 80 # Horizontal size in pixels.
dy = 30
#functions---------------------------------------------------------------------------------------

def subimage(src, left, top, right, bottom):
    dst = PhotoImage()
    dst.tk.call(dst, 'copy', src, '-from',
                left, top, right, bottom, '-to', 0, 0)
    return dst

#images--------------------------------------------------------------------
label_bg = subimage(fondo, x-2, y-2, x+dx, y+dy)

info = Label(raiz, text='Beer', bd=0, height=dy-1, width=dx-1,
             image=label_bg, compound='center')
info.place(x=x, y=y)    # Use place() to stack on top of bg image.


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

raiz.mainloop()

Advertisement

Answer

This could be improved upon, but here’s an illustration of a general idea: Cut out smaller images from the background image and use them as background in the labels.

import tkinter as tk

root = tk.Tk()

# Create bg image and place it on root window.
bgi = tk.PhotoImage(file='pilner.png')
bg = tk.Label(root, image=bgi)
bg.place(x=0, y=0)

# Adjust window to fit background image
root.geometry('{}x{}+0+0'.format(bgi.width(), bgi.height()))

# Image dimensions for label. Set these to contain the text size. 
x = 40  # Horizontal position in pixels.
y = 300 # Vertical position in pixels.
dx = 200 # Horizontal size in pixels.
dy = 50 # Vertical size in pixels.

# Function to copy a subimage from bg image at coordinates.
# Can't remember where I got this snippet. Pillow should work also.
def subimage(src, left, top, right, bottom):
    dst = tk.PhotoImage()
    dst.tk.call(dst, 'copy', src, '-from',
                left, top, right, bottom, '-to', 0, 0)
    return dst

# Create label bg image from root window bg image.
label_bg = subimage(bgi, x-2, y-2, x+dx, y+dy)

# For some reason there is a border around the label if I don't
# set height and width. 
info = tk.Label(root, text='Beer', bd=0, height=dy-1, width=dx-1, 
                           fg='white', font=(None, 50),
                           image=label_bg, compound='center')
info.place(x=x, y=y)    # Use place() to stack on top of bg image. 

root.mainloop()

Live Demo: repl.it

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement