我想在一个框架中放置一个背景图像,这是我试图运行但没有成功的代码。
import tkinter as tk
from tkinter import *
root = tk.Tk()
F1 = Frame(root)
F1.grid(row=0)
photo = PhotoImage(file="sfondo.png")
label = Label(F1, image=photo)
label.image = photo
label.place(x=0, y=0)
b = tk.Button(label, text="Start")
b.grid(row=8, column=8)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
如果我这样运行代码,则只显示左上角的一个小点(框架中没有任何内容,即使我将标签放在其中)。如果我用 替换标签父级root,它会显示带有一小部分图像作为背景的按钮(只有按钮的周边有几个像素的颜色)。但是,我想要的是框架中完整显示的背景图像,我可以在其中放置我想要的小部件。
我尝试使用 place 方法作为 this 和 PIL 模块
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
root = tk.Tk()
F1 = Frame(root)
F1.grid(row=0)
image = Image.open("sfondo.png")
render = ImageTk.PhotoImage(image)
img = tk.Label(F1, image=render)
img.image = render
img.place(x=0, y=40)
b = tk.Button(img, text="Start")
b.grid(row=8, column=8)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
在这里或多或少我遇到了同样的问题,如果我将标签的父级设置为root,则按钮会显示为外围颜色。如果我将父项设置为什么F1都不发生,并且在这两种情况下,如果我将父项设置为root并删除按钮,则图像将完全显示。但我想要的是图像完全显示在框架中,并在背景图像上显示小部件之后。
您可以将图像放在 a 上Canvas,然后Button通过将其放在可以容纳任何 Tkinter 小部件的Canvas 窗口对象中来放置 a 。
可以以类似的方式添加额外的小部件,每个小部件都在自己的Canvas窗口对象中(因为它们每个只能容纳一个小部件)。您可以通过在Frame窗口中放置一个小部件Canvas,然后将其他小部件放入其中来解决该限制。
这是一个显示如何显示单个 的示例Button:
from PIL import Image, ImageTk
import tkinter as tk
IMAGE_PATH = 'sfondo.png'
WIDTH, HEIGTH = 200, 200
root = tk.Tk()
root.geometry('{}x{}'.format(WIDTH, HEIGHT))
canvas = tk.Canvas(root, width=WIDTH, height=HEIGTH)
canvas.pack()
img = ImageTk.PhotoImage(Image.open(IMAGE_PATH).resize((WIDTH, HEIGTH), Image.ANTIALIAS))
canvas.background = img # Keep a reference in case this code is put in a function.
bg = canvas.create_image(0, 0, anchor=tk.NW, image=img)
# Put a tkinter widget on the canvas.
button = tk.Button(root, text="Start")
button_window = canvas.create_window(10, 10, anchor=tk.NW, window=button)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
截屏:
虽然我不知道用Frame代替 a 的方法Canvas,但您可以派生自己的Frame子类以更轻松地添加多个小部件。这就是我的意思:
from PIL import Image, ImageTk
import tkinter as tk
class BkgrFrame(tk.Frame):
def __init__(self, parent, file_path, width, height):
super(BkgrFrame, self).__init__(parent, borderwidth=0, highlightthickness=0)
self.canvas = tk.Canvas(self, width=width, height=height)
self.canvas.pack()
pil_img = Image.open(file_path)
self.img = ImageTk.PhotoImage(pil_img.resize((width, height), Image.ANTIALIAS))
self.bg = self.canvas.create_image(0, 0, anchor=tk.NW, image=self.img)
def add(self, widget, x, y):
canvas_window = self.canvas.create_window(x, y, anchor=tk.NW, window=widget)
return widget
if __name__ == '__main__':
IMAGE_PATH = 'sfondo.png'
WIDTH, HEIGTH = 350, 200
root = tk.Tk()
root.geometry('{}x{}'.format(WIDTH, HEIGTH))
bkrgframe = BkgrFrame(root, IMAGE_PATH, WIDTH, HEIGTH)
bkrgframe.pack()
# Put some tkinter widgets in the BkgrFrame.
button1 = bkrgframe.add(tk.Button(root, text="Start"), 10, 10)
button2 = bkrgframe.add(tk.Button(root, text="Continue"), 50, 10)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
结果:
我最近突然意识到,实际上有一种比创建自定义Frame子类更简单的方法,如我之前的编辑所示。诀窍是place()一个Label与父的心就可以了形象Frame-你就可以自由地使用其他形状管理一样pack(),并grid()像往常一样-如图下面的示例代码。它不仅不那么复杂,而且是一种比需要调用非标准方法(如add().
from PIL import Image, ImageTk
import tkinter as tk
IMAGE_PATH = 'sfondo.png'
WIDTH, HEIGHT = 250, 150
root = tk.Tk()
root.geometry('{}x{}'.format(WIDTH, HEIGHT))
# Display image on a Label widget.
img = ImageTk.PhotoImage(Image.open(IMAGE_PATH).resize((WIDTH, HEIGHT), Image.ANTIALIAS))
lbl = tk.Label(root, image=img)
lbl.img = img # Keep a reference in case this code put is in a function.
lbl.place(relx=0.5, rely=0.5, anchor='center') # Place label in center of parent.
# Add other tkinter widgets.
button = tk.Button(root, text="Start")
button.grid(row=0, column=0)
button = tk.Button(root, text="Continue")
button.grid(row=0, column=1, padx=10)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
结果#2
PS你可以sfondo.png从这里下载背景图片的副本。
| 归档时间: |
|
| 查看次数: |
14287 次 |
| 最近记录: |