2

excuse my noobness as I'm new to learning Python. I'm currently doing a very simple GUI using the Tkinter library. It is basically a LabelFrame on the top part, with some other widgets inside of it, and below it on the bottom part, a Treeview to display some data.

As it can be seen when running the code,(can't add images to my posts yet), the background of the main root window is set to 'CyanBlue'.

How can I make it to have an image instead of a color, with the all the widgets on the main window overlapping on top of it?

I attach some code for reference. Thank you very much in advance.

from tkinter import *
from tkinter import ttk
import tkcalendar

window = Tk() 
wind = window  
#HERE I SET THE BG /// WANT AN IMAGE INSTEAD OF COLOR 
wind.config(bg = 'LightCyan2')

#Create a LabelFrame 
frame = LabelFrame(wind)
frame.grid(row = 0,column = 0, columnspan = 7, pady = 20, padx = 20)

#Date FROM input 
Label(frame, text = "From Date: ", font = "Cambria 12 bold" ).grid(row = 1, column = 0, padx = 10 
,sticky = "e")
datefrom = tkcalendar.DateEntry(frame, locale = 'en_US', date_pattern ="yyyy-mm-dd")
datefrom.grid(row = 1, column = 1, columnspan = 6, padx = (0,20),pady = 5, sticky = W+E)

#Button add product 
b = Button(frame, text = 'Consult Database', font ="Bahnschrift 12 bold",height = 2 ,width = 5, bg = 
'DarkOliveGreen1')
b.grid(row = 7, columnspan = 7, padx = 10, pady = 5,sticky = W + E)

####TREEVIEW
tree = ttk.Treeview(selectmode = "extended", height = 10 , columns = ('#1','#2','#3','#4')) #Colum #0 
is always implied/ its the column icon/ the one that later becomes the Label Text 
tree.grid(row = 8, column = 0, columnspan = 4)

window.mainloop()   
  • 2
    Does this answer your question? [How to use an image for the background in tkinter?](https://stackoverflow.com/questions/10158552/how-to-use-an-image-for-the-background-in-tkinter) – Random Davis Dec 08 '20 at 17:20
  • Not quite, I'm not able to solve my issue with the proposed solution to that question... – Daniel Barreiro Dec 08 '20 at 17:39
  • Not sure but you could create a canvas, and the place an image in the canvas as the bg, and then `create_window()` in the canvas and make the widgets in the canvas. – Delrius Euphoria Dec 08 '20 at 18:07

1 Answers1

0

I'm working on tkinter stuff myself at the moment, and I'm fairly new to it as well, but I think you can use the .grid() function for this.

Images are put into widgets in tkinter. So rather than setting bg = image or similar, you'd have to create a widget that houses your image. There's a guide to that here: https://www.c-sharpcorner.com/blogs/basics-for-displaying-image-in-tkinter-python

Then you use .grid() to place the image. You might find grid's rowspan and colspan attributes useful for explaining the that image needs to fill the entire window.

Then, after the background is in place, you can grid() your new buttons on top.

So code for two buttons on top on an image might look like:

background_img.grid(row = 0, column = 0, colspan = 2)
left_button.grid(row = 0, column = 0)
right_button.grid(row = 0, column = 1)