python - 没有背景 tkinter 的标签
问题描述
我正在制作一个带有背景图像和三个标签的 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()
但是,“啤酒”文字没有出现,截图: 截图
解决方案
这可以改进,但这里有一个一般想法的说明:从背景图像中剪下较小的图像并将它们用作标签中的背景。
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()
现场演示:repl.it
推荐阅读
- python - TensorFlow模型正确预测图像,但不是来自实时视频流的帧?
- go - 从运行时作为参数传递的函数中读取函数体
- arrays - 对 Char 类型的结构数组进行排序,qsort
- laravel - Laravel 特定格式随机字符串
- javascript - 如何将数据从一个导出函数传递到另一个导出函数?
- html - FA 符号看起来很奇怪
- amazon-web-services - 如何通过 Ingress AWS 应用程序负载均衡器公开 Linkerd Viz 仪表板
- python - 为什么我收到类型错误:列表索引必须是整数或切片,而不是 str,同时调用函数并传递字典
- selenium - Selenium 4 + webdriver_manager:executable_path 已弃用。火狐如何使用?
- python - 特权意图突然导致我的机器人停止响应