本文介绍了在 python 中使用 Tkinter 使用图像制作干净的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用 Tkinter 在 python 中构建一个应用程序,并且我正在尝试使用图像作为背景.我已经能够做到这一点,但是当我添加其他小部件(如框架和标签)时,它们自己的背景与第一个重叠,结果并不令人愉快.我已经搜索了一下,但我没有找到避免这种情况的方法.是否可以 ?这是我的代码:

I'm currently trying to build an app in python with Tkinter, and I'm trying to use an image as a background. I've been able to do that, but when I add other widgets (like frames and labels), their own background overlaps the first one, and the result is not really pleasant. I've search a bit, but I didn't find a way to avoid this. Is it possible ?This is my code :

import tkinter as tk
from PIL import ImageTk, Image

root = tk.Tk()
root.geometry("1280x720")
root.update()

background_img = Image.open("./background.jpg")
background_img = background_img.resize((root.winfo_width(),root.winfo_height()), Image.ANTIALIAS)
background_tkimg = ImageTk.PhotoImage(background_img)

background_label = tk.Label(root, image=background_tkimg)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

frame = tk.Frame(root)
frame.place(anchor="c", relx=.5, rely=.5)

title_label = tk.Label(frame, text="TITLE")
button1 = tk.Button(frame, text="button 1")
button2 = tk.Button(frame, text="button 2")
button3 = tk.Button(frame, text="button 3")

title_label.grid(row=0, column=1, pady=30)
button1.grid(row=1, column=0, padx=20, pady=10)
button2.grid(row=1, column=1, padx=20, pady=10)
button3.grid(row=1, column=2, padx=20, pady=10)

root.mainloop()

当我运行它时,我有这个窗口:

And when I run it, I have this window :

推荐答案

这个效果可以通过使用 Tkinter 画布并使用 .create_window() 放置小部件来实现:

This effect can be achieved by using the Tkinter canvas and placing the widgets using .create_window():

import tkinter as tk
from PIL import ImageTk, Image

root = tk.Tk()
root.geometry("1280x720")
root.update()

background_img = Image.open("./background.jpg")
background_img = background_img.resize((root.winfo_width(),root.winfo_height()), Image.ANTIALIAS)
background_tkimg = ImageTk.PhotoImage(background_img)

canvas = tk.Canvas(root, highlightthickness=0)
canvas.pack(expand = True, fill = "both")
canvas.create_image(0,0, image = background_tkimg, anchor = "nw")

root.update()
canvas_height = canvas.winfo_height()
canvas_width = canvas.winfo_width()

title_label = canvas.create_text((canvas_width//2), (canvas_height//2) - 25, fill = "white", text="TITLE")
button1 = tk.Button(canvas, text="button 1")
button2 = tk.Button(canvas, text="button 2")
button3 = tk.Button(canvas, text="button 3")

canvas.create_window((canvas_width//2) - 75, (canvas_height//2) + 25 , window = button1)
canvas.create_window((canvas_width//2), (canvas_height//2) + 25, window = button2)
canvas.create_window((canvas_width//2) + 75, (canvas_height//2) + 25, window = button3)

root.mainloop()

标签已被替换为 canvas.create_text() 以获得透明背景.定位与原始定位不同,它只是基于与中心的近似偏移.

The label has been replaced with canvas.create_text() for a transparent background. The positioning isn't the same as the original, it's just based off approximate offsets from the centre.

这会产生这样的结果:

This produces this result:

这篇关于在 python 中使用 Tkinter 使用图像制作干净的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:21