'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:\Users\user\Desktop\Game\descarga.ico")
#winsound.PlaySound('C:\\Users\\user\\Downloads\\pygame\\MY\\sound.wav', winsound.SND_ALIAS | winsound.SND_ASYNC)
fondo = PhotoImage(file= r'C:\Users\user\Desktop\Myadorigins\background.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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
