dragov 发表于 2023-6-15 06:36:04

Tkinter 中的 Frame 怎么添加图片 ?

本帖最后由 dragov 于 2023-6-15 06:37 编辑

需要在 B 和 C 添加图片,如何实现啊 ?


from tkinter import *
root=Tk()
root.geometry("1000x350")
image_2=PhotoImage(file="s:\\a9.png")
f1=Frame(root)
f1.pack()
f_left=Frame(f1)
a=Label(f_left,text="A",width=33,height=6,background="blue")
a.grid(row=0,column=0)
b=Label(f_left,text="B",width=33,height=8,background="plum")
# b.config(image=image_2)
b.grid(row=1,column=1)
f_left.pack(side="left")

f_right=Frame(f1)
c=Label(f_right,text="C",width=33,height=8,background="sienna")
# c.config(image=image_2)
c.grid(row=1,column=0)
d=Label(f_right,text="D",width=33,height=6,background="gold")
d.grid(row=0,column=1)
f_right.pack(side="right")
mainloop()

阿奇_o 发表于 2023-6-15 07:21:25

Label里本身就可以直接设置图片,如
b=Label(f_left,text="B",bg="plum", width=100, height=80, image=image_2, compound='left')

dragov 发表于 2023-6-15 07:27:56

本帖最后由 dragov 于 2023-6-15 07:46 编辑

阿奇_o 发表于 2023-6-15 07:21
Label里本身就可以直接设置图片,如
b=Label(f_left,text="B",bg="plum", width=100, height=80, image=i ...


图片出不来的!


如果 Label 是放在主窗口中,使用 image = image_1 是可以显示图片的;
但目前 Label 是放在 Frame 窗口中,使用 image = image_1 就不行了。

wp231957 发表于 2023-6-15 08:35:02

from tkinter import *
from PIL import ImageTk, Image

root = Tk()
f1=Frame(root)
f1.pack()

image = Image.open("d:\\pic\\heart.png")
image = image.resize((200, 200))
photo = ImageTk.PhotoImage(image)


label = Label(f1, image=photo)
label.pack()

root.mainloop()

dragov 发表于 2023-6-15 09:36:37

wp231957 发表于 2023-6-15 08:35




也就是必须使用 pillow 库啦 ?

琅琊王朝 发表于 2023-6-27 19:10:33

解答如下:
页: [1]
查看完整版本: Tkinter 中的 Frame 怎么添加图片 ?