背景标签 Python Tkinter 的问题



我正在制作一个带有三个标签的基本窗口。问题是这些标签有白色背景,我不希望这样。我希望标签变得没有任何背景,但我没有任何想法。如果有人知道如何解决它,请告诉我。

PD:我用了画布,但有很多问题,但是我认为制作画布是解决方案,但我不知道该怎么做。

代码

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:UsersuserDesktopgamebackground.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()

您可以尝试使用以下代码设置背景颜色:

label.config(bg="gray")

您可以尝试使背景颜色透明:

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

使默认颜色透明或仅使用画布,这应该是类似的

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

我见过有人使用 PIL 做一些解决方法,但它是德语:https://www.python-forum.de/viewtopic.php?t=20573

无论如何,您计划使用 TKinter 执行的操作似乎非常复杂。也许你应该简单地选择一个不同的GUI库,以防你计划的话为您的界面添加更多花哨的功能。

编辑:

那你为什么不试试这个:

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()

最新更新