简体   繁体   English

Tkinter将图像放入框架,背景

[英]Tkinter placing Image into Frame, background

Screen: 屏幕: 屏幕

Firstly,I have got problem with the white background of my Frame because it expand beyond LabelFrame. 首先,我的相框的白色背景有问题,因为它超出了LabelFrame的范围。 Secondly, I would like image to be in the middle of my Frame. 其次,我希望图像位于框架的中间。

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)

The LabelFrame contains the label, and you have given it the bg color "white". LabelFrame包含标签,并且已为其指定bg颜色“白色”。 If you just want the interior to be white you can do that by inserting a new Frame with bg='white' , then put the image there. 如果您只希望内部为白色,则可以通过插入一个新的bg='white' Frame来实现,然后将图像放在此处。

Then; 然后; to place the image in the middle you have to tell grid() to expand tha cell to fill the available area with columnconfigure() and rowconfigure() . 要将图像放置在中间,您必须告诉grid()扩展单元格,以columnconfigure()rowconfigure()填充可用区域。 See my example below: 请参阅下面的示例:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM