我正在制作一个带有背景图片和三个标签的tkinter应用。
我遇到的问题是这些标签的背景是白色的,我不希望这样。我想制作一个没有背景的“ png”标签。
首先,我尝试放置以下代码行:
raiz.wm_attributes('-transparentcolor' , raiz["bg"])
但是它不起作用,从字面上看,标签是一个洞。
然后我搜索了一个解决方案,我发现制作无背景标签的最佳方法是使用画布,但是还有更多的错误,但是我认为这是解决方案,但是我不知道该怎么做它。
这是我的代码:
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("MYRIAD ALLIANCE: ORIGINS")
raiz.geometry("900x550")
raiz.resizable(0,0)
raiz.iconbitmap('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'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()
这是我得到的结果:
编辑: 现在,我正在测试一些代码,如下所示:
from tkinter import *
import winsound
from winsound import *
from tkinter import messagebox
import time
#window--------------------------------------------------------------------------------
raiz=Tk()
raiz.title("MYRIAD ALLIANCE: ORIGINS")
raiz.geometry("900x550")
raiz.resizable(0,0)
raiz.iconbitmap(r"C:\Users\shado\Desktop\Myadorigins\descarga.ico")
#winsound.PlaySound('C:\\Users\\shado\\Downloads\\pygame\\MY\\dark.wav', winsound.SND_ALIAS | winsound.SND_ASYNC)
fondo = PhotoImage(file= r'C:\Users\shado\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()
但是,“啤酒”文本没有出现,截图: screenshot
答案 0 :(得分:2)
可以对此进行改进,但这是一个总体思路的示例:从背景图像中切出较小的图像,并将其用作标签中的背景。
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{}+800+50'.format(bgi.width(), bgi.height()))
# Image dimensions for label. Set these to contain the text size.
x = 40 # Horizontal position in pixels.
y = 100 # Vertical position in pixels.
dx = 80 # Horizontal size in pixels.
dy = 30 # 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,
image=label_bg, compound='center')
info.place(x=x, y=y) # Use place() to stack on top of bg image.
root.mainloop()