【问题标题】:Trouble with displaying images tkinter显示图像 tkinter 时出现问题
【发布时间】:2022-12-16 10:10:46
【问题描述】:

图像在彼此之上传递。

def show_ucgen():
    image = Image.open("image.png")
    image_resized = image.resize((256, 256))
    photo = ImageTk.PhotoImage(image_resized)
    label = customtkinter.CTkLabel(master,text = "", image=photo).place(x=150, y=30)
    label.pack()

    
def show_kare():
    image1 = Image.open("kare.png")
    image1_resized = image1.resize((256, 256))
    photo = ImageTk.PhotoImage(image1_resized)
    label = customtkinter.CTkLabel(master,text = "", image=photo).place(x=150, y=30)
    label.pack()


def show_daire():
    image2 = Image.open("daire.png")
    image2_resized = image2.resize((256, 256))
    photo = ImageTk.PhotoImage(image2_resized)
    label = customtkinter.CTkLabel(master,text = "", image=photo).place(x=120, y=30)
    label.pack()

它预计会在打开另一个图像时删除图像。

【问题讨论】:

  • “它预计会在打开另一个图像时删除图像。”- 你为什么期望这样,因为你每次都创建一个新标签而不破坏旧标签?

标签: python user-interface tkinter


【解决方案1】:

您应该在任何给定的小部件上只使用一个几何管理器(packplacegrid)。

代替:

label = customtkinter.CTkLabel(master,text = "", image=photo).place(x=150, y=30)
label.pack()

尝试只使用place或者pack(我倾向于pack

label = customtkinter.CTkLabel(master,text = "", image=photo)
label.pack()

您可能还想将标签告诉expandfill

#example
label.pack(expand=True, fill='both')  # 'both' meaning x and y axes

【讨论】: