【问题标题】:Problem with background labels Python Tkinter背景标签Python Tkinter的问题
【发布时间】:2019-02-02 10:43:58
【问题描述】:

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

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

截图

代码

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

#creamos la ventana

raiz=Tk()

raiz.title("MYRIAD ALLIANCE: ORIGINS")
raiz.geometry("900x550")
raiz.resizable(0,0)
raiz.iconbitmap("C:\\Users\\shado\\Desktop\\Myadorigins\\descarga.ico")
#winsound.PlaySound('C:\\Users\\shado\\Downloads\\pygame\\MY\\dark.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'C:\Users\shado\Desktop\Myadorigins\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()

【问题讨论】:

    标签: python tkinter background label


    【解决方案1】:

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

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

    【讨论】:

    • 我想让它透明,但是当我尝试使用 wm_attributes 来实现时,标签实际上变成了一个洞,没有任何背景。另外,我认为解决方案是使用画布,但我不知道该怎么做
    猜你喜欢
    • 1970-01-01
    • 2019-06-14
    • 2022-07-04
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    相关资源
    最近更新 更多