0

无法在 Tkinter 中可调整大小的图像标签上添加按钮。我正在尝试添加一个易于显示的可调整大小的图像。此外,我正在尝试在其顶部添加按钮,这在我看来是一个问题。

这是代码:

from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk

root = Tk()
root.title("Speaker Recognition")
root.geometry('1280x800+0+0')

def resize_image(event):
    new_width = event.width
    new_height = event.height
    image = copy_of_image.resize((new_width, new_height))
    photo = ImageTk.PhotoImage(image)
    label.config(image = photo)
    label.image = photo 

## Resizable Image

image = Image.open('speakerid.gif')
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = Label(root, image=photo)
label.place(x=0, y=0, relwidth=1, relheight=1)
label.bind('<Configure>', resize_image)
label.pack(fill=BOTH, expand = YES)

##Adding Buttons

train_button = Button(root, bg="red", text='Train the machine')
train_button.place(x=0, y=0, relwidth=1, relheight=1)
train_button.pack()
test_button = Button(root, bg="red", text='Test the machine')
test_button.place(x=0, y=0, relwidth=1, relheight=1)
test_button.pack()
quit = Button(root, bg="red", text="Quit", command=root.destroy)
quit.place(x=0, y=0, relwidth=1, relheight=1)
quit.pack()
root.mainloop()

知道如何解决这个问题吗?

4

1 回答 1

0

您不能同时放置和打包小部件。在这种情况下,只需放置所有内容。您有图像标签以较低的 z 顺序填充容器,然后如果您放置标签并丢弃 pack() 方法调用,它们应该看起来很好,因为它们的 z 顺序高于标签。否则,可以使用 lower 和 tkraise 来调整顺序,以便按钮出现在标签的顶部。

于 2018-03-24T12:38:46.213 回答