Tkinter将图像放入框架,背景

时间:2019-03-30 13:26:58

标签: python image user-interface tkinter frame

屏幕:Screen

首先,我的Frame的白色背景有问题,因为它的扩展范围超出了LabelFrame。其次,我希望图像位于框架的中间。

img = ImageTk.PhotoImage(Image.open(filename))
panel.image = img
panel.configure(image=img)

ImageFrame = LabelFrame(page1, text="Podgląd", height=200, width=300, background='white')
ImageFrame.grid(row=0, column=3, pady=10, padx=5)
panel = Label(ImageFrame)
panel.grid(row=0, column=0, sticky='NESW')
ImageFrame.grid_propagate(0)

1 个答案:

答案 0 :(得分:1)

LabelFrame包含标签,并且您给它指定了bg颜色“白色”。如果只希望内部为白色,则可以通过在Frame中插入新的bg='white'来实现,然后将图像放在此处。

然后;要将图像放置在中间,您必须告诉grid()扩展单元格以用columnconfigure()rowconfigure()填充可用区域。请参阅下面的示例:

from tkinter import *

page1 = Tk()

ImageFrame = LabelFrame(page1, text="Podgląd", height=200, width=300)
ImageFrame.grid(row=0, column=3, pady=10, padx=5)
ImageFrame.grid_propagate(0)            # Stops geometry propagation
ImageFrame.columnconfigure(0, weight=1) # Expands cell to fill available space
ImageFrame.rowconfigure(0, weight=1)    # --- " ---

whiteframe = Frame(ImageFrame, bg='white')  # For white background
whiteframe.grid(row=0, column=0, sticky='nsew', padx=5, pady=5)
whiteframe.columnconfigure(0, weight=1) # Expands cell to fill available space
whiteframe.rowconfigure(0, weight=1)    # --- " ---

img = PhotoImage(file='images/beer.png')
panel = Label(whiteframe, image=img)
panel.image = img
panel.grid(row=0, column=0)

page1.mainloop()