Setting an image as a tkinter window background - python

I am Trying to set an image as the background of my Tkinter window and everything I try doesn't seem to work. Here's the code I have:
import random
import Tkinter # note use of caps
from Tkinter import *
from PIL import Image, ImageTk
import os.path
window = Tk()
window.title('Ask If You Dare')
window.configure(background = '#007501')

If you want the background to be an image (without using canvas), use labels:
import random
import Tkinter # note use of caps
from Tkinter import *
from PIL import Image, ImageTk
import os.path
window = Tk()
window.title('Ask If You Dare')
photo=PhotoImage(file="path/to/your/file")
l=Label(window,image=photo)
l.image=photo #just keeping a reference
l.grid()

Related

How do I do this?

Attribute Error: type object 'Image' has no attribute 'open'
this code is giving this error
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk
\#from PIL import Image
import PIL.Image
root=Tk()
root.geometry('300x400')
Button(root,text='open second window',command=open).pack()
def open():
global myimage , img
img=PIL.Image.open("C:\\Users\\HP\\Desktop\\test\\img_lights.jpg")
myimage = ImageTk.PhotoImage(img)
top=Toplevel()
top.geometry("300x400")
Button(top,text='close window',command=top.destroy).pack()
Label(top,image=myimage).pack()
mainloop()
I want the image to come on top level window but it is showing attribute error
I do not used PIL. Re-arranged your code in order to make it working. Don't use double slash.
Code:
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk
import PIL.Image
root=Tk()
root.geometry('300x400')
def open():
global myimage , img
img=PIL.Image.open(r"C:\Users\HPDesktop\test\img_lights.jpg")
myimage = ImageTk.PhotoImage(img)
top=Toplevel()
top.geometry("300x400")
Button(top,text='close window',command=top.destroy).pack()
Label(top,image=myimage).pack()
Button(root,text='open second window',command=open).pack()
root.mainloop()
Output:

AttributeError: Image has no attribute 'open'

I'm new to learning the Tkinter Module that is built in Python. I was trying to build a simple Image Viewer GUI using pillow. I'm getting an Attribute Error here.
AttributeError: type object 'Image' has no attribute 'open'
Here is my code:
from PIL import ImageTk,Image
from tkinter import *
base = Tk()
base.title("Image Viewer")
base.iconbitmap("download.ico")
img1 = ImageTk.PhotoImage(Image.open("download.png"))
label1 = Label(image = img1)
label1.grid(row = 0, column = 0, columnspan = 3)
base.mainloop()
I can't seem to find a fix for this and none of the solutions for similar questions on StackOverflow, work.
from tkinter import *
this imports everything from tkinter, including
Image:
Init signature: Image(imgtype, name=None, cnf={}, master=None, **kw)
Docstring: Base class for images.
File: [...]
Type: type
Subclasses: PhotoImage, BitmapImage
So, the Image module that you import earlier from PIL is overwritten.
you can
a) reverse the order:
from tkinter import *
from PIL import Image, ImageTk
b) import only that what you need from tkinter
from PIL import ImageTk, Image
from tkinter import Tk
c) import Image as something else:
from PIL import ImageTk
from PIL import Image as PILImage
from tkinter import *
You can do onething try to use this snippet
import PIL.Image
import PIL.ImageTk
im = PIL.Image.open("download.png")
photo = PIL.ImageTk.PhotoImage(im)

python tkinter image missing

I want to have a image of a map next to my application I am creating right now, which includes a filter system. However I wasn't sure why it wasn't showing up. Here are snippits from my code
from Tkinter import *
import Tkinter as tk
from PIL import ImageTk, Image
import os
class MainPage(tk.Frame):
def __init__(self,app):
tk.Frame.__init__(self)
frame_A = Frame(self,width=930,height=780)
frame_A.grid(row=0,column=0,columnspan=2,rowspan=3)
contentmap = Frame(frame_A)
img = ImageTk.PhotoImage(Image.open("/Users/J/Desktop/unknown.png"))
panel = Label(contentmap, image = img)
App().mainloop()
Additionally I would like to ask for some advice as to whether it was possible to make my image a interactive image like if I were to zoom in google maps.
Thanks a million!

Not being able to load an image in python

I am trying to load an image into a canvas in python. However I am getting the error: TclError: couldn't recognize data in image file "C:\testimage\spongesea.jpg"
import Tkinter
from Tkinter import *
import time
import inspect
import os
from PIL import Image, ImageTk
class game_one:
def __init__(self):
global root
global canvas_one
root = Tk()
root.title(" Thanks Josh Dark
canvas_one = Tkinter.Canvas(root, bg="BLACK")
canvas_one.pack(expand= YES, fill= BOTH)
canvas_one.focus_set() #allows keyboard events
p = PhotoImage(file="C:\testimage\spongesea.jpg")
canvas_one.create_image(0, 0, image = p, anchor=NW)
root.grab_set()#I forget what this does. Don't change it.
root.lift()#This makes root appear in front of the other applications
ObjectExample = game_one()# starts the animation
I can open the image manually from the file, so it is not corrupted, and it is calling the correct place. Any ideas? Thanks
PhotoImage works only with GIF and PGM/PPM.
You have to use Image, ImageTk to work with other formats
from PIL import Image, ImageTk
image = Image.open("lenna.jpg")
photo = ImageTk.PhotoImage(image)
BTW: read PhotoImage and see "Garbage Collection problem" in note.

Tkinter: menu not displayed

I run this code which aim is only to display a menu bar.
There is a simple menu bar in which 3 sub-menus are created without performing anything except for the Exit one which closes the window:
from Tkinter import *
import tkMessageBox
import numpy as np
import ttk
import tkFont
from PIL import ImageTk, Image
from tkColorChooser import askcolor
from tkFileDialog import askopenfilename
class MyGui(Frame):
def __init__(self,master):
Frame.__init__(self,master)
self.master=master
self.themenus() # menu initialization within the constructor
def themenus(self):
self.menubar=Menu(self.master)
self.filemenu=Menu(self.menubar,tearoff=0)
self.filemenu.add_command(label="Open Image")
self.filemenu.add_command(label="Save Image")
self.filemenu.add_command(label="Exit",command=self.master.quit)
self.menubar.add_cascade(label="File",menu=self.filemenu)
if __name__=="__main__":
root=Tk()
root.wm_title("Test")
mg=MyGui(root)
root.mainloop()
I get this error:
libdc1394 error: Failed to initialize libdc1394
How to fix this ?
EDIT:
I resolved the problem by removing away the original imports that I did not use after all:
import tkMessageBox
import numpy as np
import ttk
import tkFont
from PIL import ImageTk, Image
from tkColorChooser import askcolor
from tkFileDialog import askopenfilename
Now, no error is triggered, however I do not see the menu displayed. Why ?
I resolved my problem by adding this line at the end of themenus() function:
self.master.config(menu=self.menubar)

Categories