Setting Background image to GUI - python

I made a GUI with few buttons and I want to change the gray background to an image.
my code looks like this:
from tkinter import *
from urlread import givenumbers # my function
""" Setting The Main GUI """
GUI = Tk()
GUI.title('Check GUI')
GUI.iconbitmap('test.ico')
GUI.geometry("400x400")
background_image=PhotoImage('pic.jpg')
background_label = Label(GUI, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
""" Reading Images For Buttons """
A_Im = PhotoImage(file='A.gif')
""" Creating Buttons """
# A Button
A_Button = Button(GUI, image=A_Im, command=givenumbers)
A_Button.grid(column=0, row=1)
GUI.mainloop()
The code runs without error but the background is still gray without any effect.

The problem is in the line background_image=PhotoImage('pic.jpg'). The PhotoImage class only supports GIF-files, which means that it cannot read the file you're specifying. You should try something like this:
#Python 2.7
import Tkinter as tk
from PIL import Image, ImageTk
window = tk.Tk()
image = Image.open('image.jpg')
photo_image = ImageTk.PhotoImage(image)
label = tk.Label(window, image = photo_image)
label.pack()
# Python 3
import tkinter as tk
from PIL import Image, ImageTk
window = tk.Tk()
image = Image.open('image.jpg')
photo_image = ImageTk.PhotoImage(image)
label = tk.Label(window, image = photo_image)
label.pack()
The Image class from the PIL module supports a variety of formats, among which jpeg and png. You can install the PIL module by running pip install pillow in a command prompt or terminal.
If you want to put the widgets on top of the Label, you could indeed using grid to get them on top of each other, but using a Canvas would probably be easier. You can find more about the Canvas widget here.

This can be done without pil also:
from tkinter import *
import tkinter as ttk
""" Setting The Main GUI """
GUI = Tk()
F1=Frame(GUI)
F1=Frame(GUI,width=400,height=450)
F1.place(height=7000, width=4000, x=100, y=100)
F1.config()
F1.grid(columnspan=10,rowspan=10)
F1.grid_rowconfigure(0,weight=1)
F1.grid_columnconfigure(0,weight=1)
photo=PhotoImage(file="C:\\Users\\HOME\\Desktop\\Eshita\\12th\\computer
\\python\\GUI\\math3.gif")
label = Label(GUI,image = photo)
label.image = photo # keep a reference!
label.grid(row=0,column=0,columnspan=20,rowspan=20)
b=ttk.Button(GUI,text="Start")
b.grid(row=8,column=8)
GUI.mainloop()

Related

Image not being displayed in tkinter python

I've been using tkinter for a while now and currently I am working on an app in which I need to display an image at the home screen. The problem is that though the code is fine, the image is not being displayed. This is the image:
This the code I am using. Please tell the problem:
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.geometry("1000x700")
root.minsize(1000, 700)
root.maxsize(1000, 700)
bg_img = Label(image=ImageTk.PhotoImage(Image.open("image_processing20200410-19194-aihwb4.png")), compound=CENTER)
bg_img.pack(expand=1, fill=BOTH)
I have tried using the grid geometry manager but it is not working. Please help
When you add a PhotoImage to a Tkinter widget, you have to keep a separate reference to the image object. Otherwise, the image will not show up.
You need to define image object separately and then load it into Label:
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.geometry("1000x700")
root.resizable(False, False)
image = ImageTk.PhotoImage(Image.open("image_processing20200410-19194-aihwb4.png"))
panel = Label(root, image=image)
panel.pack(expand=1, fill=BOTH)
root.mainloop()
Output:
try this code it might work
from PIL import Image
from PIL.ImageTk import PhotoImage
root = Tk()
root.geometry("1000x700")
root.minsize(1000, 700)
root.maxsize(1000, 700)
bg_img = Label(image=ImageTk.PhotoImage(Image.open("Jitn6.png")), compound=CENTER)
bg_img.pack(expand=1, fill=BOTH)
you should do it like this
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.geometry("1000x700")
root.minsize(1000, 700)
root.maxsize(1000, 700)
my_image = ImageTk.PhotoImage(Image.open("image_processing20200410-19194-aihwb4.png"))
bg_img = Label(image=my_image, compound=CENTER)
bg_img.pack(expand=1, fill=BOTH)
you should first put the image in a variable then pass it in

IMG2PY for background label and icon in TKINTER

Working in a EXE file with background and Icon embed, I read some ideas about to use img2py in order to create module.py that can be imported into the EXE with pyinstaller.
I successfully create the imagebg.py (BG2.png Background) and imageico.py (ico2.ico Icon) modules using img2py. But there is no example or way to set that images into the modules in the tkinter label as background and the icon into the code.
Please anybody can help me.
Main code where must be imported BG ad ICON modules and use them as background and Icon images for tkinter GUI
#Main code GUI tkinter
import imagebg #from here we must import PNG_File.png for Background
import imageico #from here we must import ICO_File.ico for Icon
import wx #from img2py installation
from tkinter import *
root=Tk()
#set windows size
root.resizable(width=False, height=False)
root.geometry("925x722")
#set title
root.title("SOFT1)")
#frame 1
f1=Frame(root, width=345,height=475,bg="light
grey",highlightbackground="black",highlightthickness=4)
f1.place(x=20,y=235)
#set a image as BG
Logo=PhotoImage(file="PNG_File.png")
lab6=Label(root, image=Logo)
lab6.place(x=0, y=0)
#set a image as ICON
root.iconbitmap("ICO_File.ico")
mainloop()
The module imagebg.py for backgroud generated with IMG2PY
BG2 = PyEmbeddedImage(
b'iVBORw0KGgoAAAANSUhEUgAAAsUAAAKECAIAAABgrdCGAAAACXBIWXMAAA7EAAAOxAGVKw4b'
b'AAAgAElEQVR4nOzdd2Ab5d0H8Oe0hy1vecYjcZw4cfYimwQIEEbYhQJlFgqU3UFZLR2M9i2l'
b'tLRlFcouI5Qww15hZJEdO4kdOx7xtiVrj7v3DwdFlmXppu4kfT9/Wfbdc78YY339TMrpdBIA'
b'AAAAAVRyFwAAAABJD3kCAAAAhEKeAAAAAKGQJwAAAEAo5AkAAAAQCnkCAAAAhEKeAAAAAKGQ'
b'JwAAAEAo5AkAAAAQCnkCAAAAhEKeAAAAAKGQJwAAAEAo5AkAAAAQCnkCAAAAhEKeAAAAAKGQ'
b'JwAAAEAo5AkAAAAQCnkCAAAAhEKeA..... to much characters to be placed in this post
The module imageico.py for icon generated with IMG2PY
from wx.lib.embeddedimage import PyEmbeddedImage
ico2 = PyEmbeddedImage(
b'iVBORw0KGgoAAAANSUhEUgAAAsUAAAKECAYAAADvz0fRAAAABHNCSVQICAgIfAhkiAAAIABJ'
b'REFUeJzs3WeAVNXdBvDnTi/b+7L0svSmIIKd2Gus0ZhiSeKreZOYmGpL15i8SdTEFKMpGkss'
b'GEuMqNFYsCAgHRZYWFiWsn12p7f7fkBwy8zszC1z7537/L7ozs6c82dh4dkz/3OOEAgERBAR'
b'ERERmZhF6wKIiIiIiLTGUExEREREpsdQTERERESmx1BMRERERKbHUExEREREpsdQTERERESm'
b'x1BMRERERKbHUExEREREpsdQTERERESmx1BMRERERKbHUExEREREpsdQTERERESmx1BMRERE'
b'RKbHUExEREREps.... to much characters to be placed in this post
Please any idea of how to use the modules as images for background and icon in the main code GUI Tkinter
You need to convert the wx.Image to PIL compatible image if you want to use it in tkinter application.
Below is an example based on your posted code:
import tkinter as tk
from PIL import Image, ImageTk
import imagebg
import imageico
# function to convert a wx.Image to PIL.ImageTk.PhotoImage
def wx2pil(wx_image):
image = wx_image.Image
w, h = image.GetWidth(), image.GetHeight()
data = image.GetData()
img = Image.frombytes('RGB', (w, h), bytes(data))
return ImageTk.PhotoImage(img)
root = tk.Tk()
root.resizable(False, False)
root.geometry('925x722')
root.title('SOFT1')
# use iconphoto() instead of iconbitmap()
root.iconphoto(False, wx2pil(imageico.ico2))
#set a image as BG
logo = wx2pil(imagebg.BG2)
tk.Label(root, image=logo).place(x=0, y=0)
#frame 1
f1 = tk.Frame(root, width=345, height=475, bg='lightgray', highlightbackground='black', highlightthickness=4)
f1.place(x=20, y=235)
root.mainloop()
Note that I use wxPython v4.1.0.
As it depends on wxPython, why don't you use wxPython for the GUI directly instead of tkinter?

How to make a file open by default with tkinter app?

I made a tkinter app for displaying images, and I was wondering if there's a way to make an image open by default with this app
At the moment if I try that, I get an error for some non declared icon file (this is the icon that appears near the name of the app at the top)
There's no real goal behin this Gui, I'm just experimenting and learning.
Thanks
Try this:
from PIL import Image, ImageTk
import tkinter as tk
from tkinter import filedialog
my_img = []
def FileImport():
file = filedialog.askopenfilename()
my_img.clear()
my_img.append(ImageTk.PhotoImage(Image.open(file)))
label1.config(image=my_img[0])
root= tk.Tk()
root.title('Main')
root.geometry('400x400')
label = tk.Label(root, text = "Browse", fg="purple")
label.pack()
button = tk.Button(root, text='See Image',fg="blue", command=FileImport)
button.pack()
my_img.append(ImageTk.PhotoImage(Image.open(your_first_image)))
label1 = tk.Label(root, image = my_img[0])
label1.pack(pady= 50)
root.mainloop()
When you run this:
After you tap to see different image from computer:
Hope It helps!

Create a text on background image on a Raspberry pi in fullscreen mode

I'm new here, so I'm hoping to ask correctly.
I want to program a GUI on a raspberry pi in fullscreen mode with python. I managed to display a background image. Now I'm trying to get a simple text on it, but I'm not managing to do that. I searched a lot but didn't find any solution to my problem. Pls help!
Here my code:
from tkinter import *
from PIL import ImageTk
window = Tk()
C = canvas(window, height = 500, width=800)
C.pack()
image = ImageTk.PhotoImage(file = "home/pi/Downloads/background3.jpg")
background_label = Label(window, image = image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
Label(window, text="Hello", fg="black").pack()
window.attributes('-fullscreen', True)
windows.mainloop()
Thank you in advance!
You need to use the create_image and create_text methods of the canvas to overlay text on an image. Just add your image, the text you want and the fullscreen command to the below example to get what you want.
from tkinter import *
from PIL import ImageTk
window = Tk()
c = Canvas(window,height=450,width=750)
image = ImageTk.PhotoImage(file="YourImage.png")
c.create_image(0,0,image=image,anchor=NW)
c.create_text(200,200,text="Hello World",fill="yellow",font="Times 32 bold")
c.pack()
window.mainloop()

trying to show an image in python using Tkinter

I have been trying to show an image for a game but it doesn't show can i please have some help?
I've tried to download PIL but it doesn't work with python 3.
from tkinter import *
canvas = Canvas(width=500, height=500)
canvas.pack(expand=YES, fill=BOTH)
Logo = PhotoImage('photo.gif')
canvas.create_image(50, 10, image=Logo)
label = Label(image=Logo)
label.image = Logo
label.pack()
mainloop()
If you dont tell PhotoImage() what the first argunent is, it will assume its the widget's name. To get it to load a file you have to explicitly tell it that it is a file:
Logo = PhotoImage(file='photo.gif')
Python 3.6.4 with Pillow 5.2.0
You can use: python -m pip install Pillow
Here is an example of how to use PIL with tkinter
from PIL import Image, ImageTk
import tkinter as tk
root = tk.Tk()
img = Image.open("test.png")
tk_image = ImageTk.PhotoImage(img)
label =tk.Label(root, image=tk_image)
label.pack()
root.mainloop()

Categories