Fit a background image in python

0

I have a problem I want to adjust the background image to cover the entire interface, but I move the size and sometimes if you show it to me and other times you can help me in this detail please

from tkinter import *
from tkinter import Tk    
from tkinter import PhotoImage
from tkinter import Canvas
from tkinter import NW
from tkinter import Menu
from tkinter import filedialog
from PIL import Image, ImageTk
from sys import argv
import tkinter as tk



ventana = tk.Tk ()

ventana.geometry("900x900+0+0")
fondo=PhotoImage(file="fondo.gif")
lblFondo=Label(ventana,image=fondo).place(x=150,y=150) #fondo 

 ###############################################################################################################


img_frame = tk.Frame(ventana, height=600, width=800, bg='#faf0e6')
img_frame.pack()
canvas = tk.Canvas (img_frame, height=600, width=800, bg='#faf0e6', relief=tk.SUNKEN)



imagenL = None
def abrir():
    global imagenL
    ventana.filename=filedialog.askopenfilename(initialdir="C:/Imágenes")
    ruta=ventana.filename
    imagen = Image.open(ruta)
    imagenL = ImageTk.PhotoImage(imagen)
    canvas.create_image(0,0,anchor=NW,image=imagenL) 



class Ayuda_Dialog:
    def __init__(self, parent):
        text = ("\n")


class Main_Window:
    def __init__(self,  ventana):
        mnuAyuda.add_command(label="Ayuda",command=self.ayuda)

    def ayuda(self):
        Ayuda_Dialog(ventana)


barraMenu=Menu(ventana)
mnuArchivo=Menu(barraMenu)
mnuDiagnostico=Menu(barraMenu)
mnuAyuda=Menu(barraMenu)
mnuArchivo.add_command(label="Abrir",command = abrir)
mnuArchivo.add_separator()
mnuArchivo.add_command(label="Salir",command=ventana.destroy)

barraMenu.add_cascade(label="Archivo",menu=mnuArchivo)

ventana.config(menu=barraMenu)

if __name__ == "__main__":            
    Main_Window(ventana)
ventana.mainloop()
    
asked by Airam 03.04.2018 в 03:31
source

1 answer

1
  

Translation of the original answer by Marcin

This is a sample application that uses Pillow to change the size of the image on the label as the label resizes:

from tkinter import *

from PIL import Image, ImageTk

root = Tk()
root.title("Title")
root.geometry("600x600")
root.configure(background="black")



class Example(Frame):
    def __init__(self, master, *pargs):
        Frame.__init__(self, master, *pargs)



        self.image = Image.open("./resource/Background.gif")
        self.img_copy= self.image.copy()


        self.background_image = ImageTk.PhotoImage(self.image)

        self.background = Label(self, image=self.background_image)
        self.background.pack(fill=BOTH, expand=YES)
        self.background.bind('<Configure>', self._resize_image)

    def _resize_image(self,event):

        new_width = event.width
        new_height = event.height

        self.image = self.img_copy.resize((new_width, new_height))

        self.background_image = ImageTk.PhotoImage(self.image)
        self.background.configure(image =  self.background_image)



e = Example(root)
e.pack(fill=BOTH, expand=YES)


root.mainloop()

This is how it works using the image of Lenna as an example:

  

Here there is other useful information, Regards!

    
answered by 04.04.2018 в 17:26