我的图片有问题。我有一个文本小部件和一个背景图片。也就是说,图像上方的文字图片。当我插入一张新图片时,它会变成文本小部件的上方。在这里,我需要一些功能来将新图像移动到背景,而Text则在前台。
from tkinter import*
def listbox():
def image_get(list_):
global image_1
if int(list_.curselection()[0]) == 0: #by clicking on the first item listbox
image_1 = PhotoImage(file="4.png")
lab = Label(root, image = image_1)
lab.place(x=0, y=0)
#root.attributes("-alpha", 0.5)
top = Toplevel(root)
list_ = Listbox(top,width=20)
list_.insert(END,'image 0','image 1','image 2','image 3','image 4','image 5', 'image 6','image 7','image 8','image 9')
list_.pack()
list_.bind("<Double-1>", lambda event: image_get(list_) or top.destroy())
root = Tk()
root.minsize(1000,555)
root.maxsize(1000,555)
image_1 = PhotoImage(file="0.png")
lab = Label(root, image = image_1)
lab.place(x=0, y=0)
text=Text(root)
text.place(x=0,y=0)
king = Menu(root, postcommand=lambda: listbox())
root.config(menu=king)
view = Menu(king,tearoff = 0)
#view.add_command(label='Background',command=None)
king.add_cascade(label="View",menu=view)
root.mainloop()
答案 0 :(得分:1)
The issue is the order of placement. You are placing the label over the text box in your image_get()
function. Instead you want to just update the label and the image will stay behind the textbox because we are not changing its potion and just changing the image.
The method you are using now just keeps adding new labels rather than updating the existing one.
Change this:
def image_get(list_):
global image_1
if int(list_.curselection()[0]) == 0: #by clicking on the first item listbox
image_1 = PhotoImage(file="4.png")
lab = Label(root, image = image_1)
lab.place(x=0, y=0)
To this:
def image_get(list_):
global image_1
if int(list_.curselection()[0]) == 0: #by clicking on the first item listbox
image_1 = PhotoImage(file="4.png")
lab.config(image = image_1)