LIshy2 Asked:2020-12-10 02:32:11 +0800 CST2020-12-10 02:32:11 +0800 CST 2020-12-10 02:32:11 +0800 CST Tkinter 窗口背景图片 772 如何在 Tkinter 中设置整个窗口的背景图像?使所有按钮都在顶部 python 2 个回答 Voted Best Answer S. Nick 2020-12-10T03:05:12+08:002020-12-10T03:05:12+08:00 试试这样: import tkinter as tk from PIL import ImageTk, Image def show_image(path): root = tk.Tk() img = Image.open(path) width = 500 ratio = (width / float(img.size[0])) height = int((float(img.size[1]) * float(ratio))) imag = img.resize((width, height), Image.ANTIALIAS) image = ImageTk.PhotoImage(imag) panel = tk.Label(root, image=image) panel.pack(side="top", fill="both", expand="no") tk.Button(root, text='Quit', command=root.quit).place(x=250, y=250) root.mainloop() show_image('D:/_Qt/img/cat.jpg') insolor 2022-01-11T18:44:12+08:002022-01-11T18:44:12+08:00 通过 Canvas 添加背景图像的选项。使用 放置按钮create_window。要创建具有透明背景的文本,请Label使用create_text. import tkinter as tk from PIL import ImageTk, Image path = 'cat.jpg' root = tk.Tk() image = Image.open(path) width = 500 ratio = (width / float(image.size[0])) height = int((float(image.size[1]) * float(ratio))) image = image.resize((width, height), Image.ANTIALIAS) image = ImageTk.PhotoImage(image) canvas = tk.Canvas(root, width=width, height=height) canvas.pack(side="top", fill="both", expand="no") canvas.create_image(0, 0, anchor="nw", image=image) button = tk.Button(root, text='Quit', command=root.quit) canvas.create_window((250, 250), anchor="nw", window=button) canvas.create_text(100, 100, text="Cat", fill="Yellow", font="Verdana 14") root.mainloop()
试试这样:
通过 Canvas 添加背景图像的选项。使用 放置按钮
create_window
。要创建具有透明背景的文本,请Label
使用create_text
.