tkinter PhotoImage object has no attribute 'read' - python

from tkinter import *
import tkinter as tk
from PIL import ImageTk, Image
from tkinter import ttk
import kalkulasi
#making the window
root = Tk()
root.title('Estimasi Perjalanan Kereta Rute Tangerang-Depok')
root.minsize(400,400)
root.geometry('650x700')
#define image
image = Image.open('petafix.png')
#define image as bg
bg = ImageTk.PhotoImage(image)
# create canvas
main_canvas = Canvas(root, width=650, height=700)
main_canvas.pack(fill='both', expand=True)
# set img in canvas
main_canvas.create_image(325, 300, image=bg)
# add "Selamat Datang!" text
main_canvas.create_text(325, 25, text="Selamat Datang!", font=('Helvetica', 25))
# making resize "HITUNG" button image
hitung_button = Image.open('button.jpg')
hitung_button_resized = hitung_button.resize((75, 30), Image.ANTIALIAS)
new_hitung_button = ImageTk.PhotoImage(hitung_button_resized)
label_hitung_button = Label(root, image= new_hitung_button, border= 0, anchor='center')
label_hitung_button.pack()
# reposition the "hitung" button
button = Button(root, image= new_hitung_button, border= 0, highlightthickness= 0,)
button_window = main_canvas.create_window(300, 670, anchor='nw', window=button)
# making resizer function
def resizer(event):
global bg1, resized_bg, new_bg
# open the image
bg1 = Image.open('petafix.png)
# Resize the image
resized_bg = bg1.resize((event.width, event.height), Image.ANTIALIAS)
# define image again as new bg
new_bg = ImageTk.PhotoImage(resized_bg)
# add new bg back to the canvas
main_canvas.create_image(325, 300, image=new_bg)
root.bind('<Configure>', resizer)
root.mainloop()
I'm trying to make my GUI window's dynamic, but there is an error i can't figured out that when i run the program, the program want to run but it still not dynamic and in the terminal, there is an error says AttributeError: 'PhotoImage' object has no attribute 'read'.

Related

Tkinter images and geometry creation

I've tried to do a star fractal drawing a star using tkinter and putting an image as a background.
from tkinter import *
from PIL import ImageTk, Image
import tkinter as tk
app = Tk()
app.title("Welcome")
img =Image.open('C:\\Users\\Stefa\\Downloads\\galaxy.jpeg')
bg = ImageTk.PhotoImage(img)
canvas_width=800
canvas_height=800
master = tk.Tk()
label = Label(app, image=bg)
label.place(x = 0,y = 0)
label2 = Label(app, text = "WELCOME TO OUR GALAXY",
font=("Times New Roman", 24))
label2.pack(pady = 50)
app.geometry(f"{canvas_width}x{canvas_height}")
can_widgt=Canvas(app, width=canvas_width, height= canvas_height)
can_widgt.pack()
points=[200,20,80,396,380,156,20,156,320,396]
can_widgt.create_polygon(points, outline='red',fill='cyan', width=6)
app.mainloop()
That's the code
However when i run it i want the star to be upon the background image. Any solutions for this ? Thanks
You need to create the image as a canvas object rather than as a label.
For example, this add the image to the top-left corner of the canvas, with the drawing on top of it:
can_widgt.create_image(0, 0, anchor="nw", image=bg)

my resizable background is not working after adding frame on the canvas

I am beginner in tkinter. I have added a resizable background which is perfectly working. Have look to the code
'''
import tkinter as tk
from PIL import ImageTk, Image
homeWin = tk.Tk()
homeWin.geometry("400x400")
background = ImageTk.PhotoImage(file = "bg.png")
hw_canvas = tk.Canvas(homeWin, width = 400, height=400)
hw_canvas.pack(fill="both", expand=True)
bg = hw_canvas.create_image(0, 0, image = background, anchor = "nw")
def bg_resizable(e):
global image, resized, image2
# open image to resize it
image = Image.open("bg.png")
# resize the image with width and height of root
resized = image.resize((e.width, e.height), Image.LANCZOS)
image2 = ImageTk.PhotoImage(resized)
hw_canvas.create_image(0, 0, image=image2, anchor='nw')
homeWin.bind("<Configure>", bg_resizable)
homeWin.mainloop()
'''
but after placing a frame on the canvas it is not working.
I don't know why this is. Pleas help me with it!
'''
import tkinter as tk
from PIL import ImageTk, Image
homeWin = tk.Tk()
homeWin.geometry("400x400")
background = ImageTk.PhotoImage(file = "bg.png")
hw_canvas = tk.Canvas(homeWin, width = 400, height=400)
hw_canvas.pack(fill="both", expand=True)
bg = hw_canvas.create_image(0, 0, image = background, anchor = "nw")
def bg_resizable(e):
global image, resized, image2
# open image to resize it
image = Image.open("bg.png")
# resize the image with width and height of root
resized = image.resize((e.width, e.height), Image.LANCZOS)
image2 = ImageTk.PhotoImage(resized)
hw_canvas.create_image(0, 0, image=image2, anchor='nw')
homeWin.bind("<Configure>", bg_resizable)
sl_frame = tk.Frame(hw_canvas, bg="white", width= 200, height=500)
sl_frame.place(relx= 0.5, rely= 0.5, anchor="center")
homeWin.mainloop()
'''
"<Configure>" events get called for main window and all children!
Using the following can exclude child "<Configure>" events:
import tkinter as tk
from PIL import ImageTk, Image
def bg_resizable(e):
global image, resized, image2
# main window has no master
if e.widget.master:
return
# resize the image with width and height of root
resized = image.resize((e.width, e.height), Image.LANCZOS)
image2 = ImageTk.PhotoImage(resized)
hw_canvas.itemconfigure(bg, image=image2)
image = Image.open("bg.png") # open image once, global
homeWin = tk.Tk()
homeWin.geometry("400x400")
hw_canvas = tk.Canvas(homeWin, width = 400, height=400)
hw_canvas.pack(fill="both", expand=True)
bg = hw_canvas.create_image(0, 0, anchor='nw')
sl_frame = tk.Frame(hw_canvas, bg="white", width= 200, height=500)
sl_frame.place(relx= 0.5, rely= 0.5, anchor="center")
homeWin.bind("<Configure>", bg_resizable)
homeWin.mainloop()
Also, note because event fires when window is first shown, there is not need to handle background outside of event.

python3 tkinter GUI with clickable transparent images

I have made a background for my GUI in inkscape and have managed to add a transparent image over the top to use as buttons but cant figure out how to make them run a subroutine (nav) when clicked. I either need an image with transparency over the top of my background that I can click or a completely see through rectangle that I can position over a button drawn on the background image in inkscape. I'm on Ubuntu if it matters.
from tkinter import *
from PIL import ImageTk, Image
import sys
root = Tk ()
root.title('GUI')
root.geometry("1000x564")
root.attributes('-zoomed', True)
root.attributes("-type", "splash")
#define image
bg = ImageTk.PhotoImage(file="BACKGROUND.png")
#create canvas
my_canvas = Canvas(root, width=800, height=500)
my_canvas.pack(fill="both", expand=True)
my_canvas.create_image(0,0, image=bg, anchor = NW)
def nav():
print ("navigation")
#creating button which supports png transparency
#button = PhotoImage(file="button2.png")
#my_canvas.create_image(260,-70, anchor=NW, image=button, state='normal', )
buttonImage = ImageTk.PhotoImage(Image.open("button.png"))
button = my_canvas.create_image(50, 50, image=buttonImage)
my_canvas.tag_bind(Button, "<Button-1>", nav())
def resizer(e):
global bg1, resized_bg, new_bg
# open image
bg1 = Image.open("BACKGROUND.png")
# resize
resized_bg =bg1.resize((e.width, e.height), Image.ANTIALIAS)
#DEFINE IMAGE AGAIN
new_bg =ImageTk.PhotoImage(resized_bg)
#add back to the canvas
my_canvas.create_image(0,0, image=new_bg, anchor = NW)
# my_canvas.create_image(260,-70, anchor=NW, image=button, state='normal', )
button = my_canvas.create_image(50, 50, image=buttonImage)
def close(e):
root.destroy()
root.bind('<Configure>', resizer)
root.bind('<Escape>', close)
root.mainloop()
when calling function with my_canvas.tag_bind(button, "<Button-1>", nav) nav(root) is actually being called when clicked so def nav(root): has to be used for the sub routine to run correctly.

Unable to write text on mouseclick area on Image

I am trying to draw text on Image where the user clicks.
Getting this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/Admin/PycharmProjects/ashish/td.py", line 35, in draw_text
cv2.putText(img, "OpenCV + Jurassic Park!!!", (event.x,event.y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
TypeError: Expected cv::UMat for argument 'img'
if __name__ == "__main__":
root = Tk()
frame = Frame(root, bd=2, relief=SUNKEN)
canvas = Canvas(frame, bd=0)
canvas.grid(row=0, column=0, sticky=N+S+E+W)
frame.pack(fill=BOTH,expand=1)
#adding the image
File = filedialog.askopenfilename(parent=root, initialdir="F:/",title='Choose an image.')
img = ImageTk.PhotoImage(Image.open(File))
canvas.create_image(0,0,image=img,anchor="nw")
#function to be called when mouse is clicked
def draw_text(event):
cv2.putText(img, "OpenCV + Jurassic Park!!!", (event.x,event.y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
cv2.imshow("Text", img)
#mouseclick event
canvas.bind("<Button 1>",draw_text)
root.mainloop()
Your img is ImageTK object from tkinter but cv2 is not part of tkinter and cv2.putText doesn't work with ImageTK. It needs something different. cv2 has own function to read image and it creates object which you can use with cv2.putText().
But canvas has function to display text on top of image and you don't need cv2. But it can't be saved in file as image with text.
But Image has function to draw text on image and it can be saved in file.
So finally you don't need cv2.
I use Image.Draw to create object on which I can put text or draw line/square/etc.
After adding text I replace image on canvas.
This method create image with text which you can save in file (img.save())
from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageTk, ImageDraw
# function to be called when mouse is clicked
def draw_text(event):
global imgtk
global cv_img
# create object for drawing
draw = ImageDraw.Draw(img)
# put text
draw.text((event.x,event.y), "ImageDraw + Jurassic Park!!!")
# replace old image
canvas.delete(cv_img_id)
imgtk = ImageTk.PhotoImage(img)
cv_img_id = canvas.create_image(0, 0, image=imgtk, anchor="nw")
if __name__ == "__main__":
root = Tk()
frame = Frame(root, bd=2, relief=SUNKEN)
frame.pack(fill=BOTH, expand=1)
canvas = Canvas(frame, bd=0)
canvas.grid(row=0, column=0, sticky=N+S+E+W)
#adding the image
file = filedialog.askopenfilename(parent=root, initialdir="F:/",title='Choose an image.')
img = Image.open(file)
imgtk = ImageTk.PhotoImage(img)
cv_img_id = canvas.create_image(0, 0, image=imgtk, anchor="nw")
#mouseclick event
canvas.bind("<Button 1>", draw_text)
root.mainloop()
Using canvas.create_text you can put text on top of image which you can move/remove later but it doesn't create image with text which you can save in file.
from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageTk
# function to be called when mouse is clicked
def draw_text(event):
canvas.create_text((event.x,event.y), text="Canvas + Jurassic Park!!!")
if __name__ == "__main__":
root = Tk()
frame = Frame(root, bd=2, relief=SUNKEN)
frame.pack(fill=BOTH, expand=1)
canvas = Canvas(frame, bd=0)
canvas.grid(row=0, column=0, sticky=N+S+E+W)
#adding the image
file = filedialog.askopenfilename(parent=root, initialdir="F:/",title='Choose an image.')
imgtk = ImageTk.PhotoImage(Image.open(file))
canvas.create_image(0, 0, image=imgtk, anchor="nw")
#mouseclick event
canvas.bind("<Button 1>", draw_text)
root.mainloop()

tkinter background image in Frame

I want to put a background image in a Frame, this is the code that I'm trying to run without success.
import tkinter as tk
from tkinter import *
root = tk.Tk()
F1 = Frame(root)
F1.grid(row=0)
photo = PhotoImage(file="sfondo.png")
label = Label(F1, image=photo)
label.image = photo
label.place(x=0, y=0)
b = tk.Button(label, text="Start")
b.grid(row=8, column=8)
root.mainloop()
If I run the code as this, only a little point in the top left corner is displayed (the frame without nothing in, even if I placed the label inside of it). If I replace the label parent with root, it displays the button with a little part of the image as backgound (only the perimeter of the button is colored for a few pixels). However what I want is a full displayed background image in the frame where I can put the widgets that I want.
I tried to with the place method as this and PIL module
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
root = tk.Tk()
F1 = Frame(root)
F1.grid(row=0)
image = Image.open("sfondo.png")
render = ImageTk.PhotoImage(image)
img = tk.Label(F1, image=render)
img.image = render
img.place(x=0, y=40)
b = tk.Button(img, text="Start")
b.grid(row=8, column=8)
root.mainloop()
Here more or less I'm having the same problems, if I set the parent of the label to root, the button is displayed with the perimeter coloured.
If I set the parent to F1 nothing happens and in both cases if I set the parent as root and remove the button, the image is fully displayed.
But what I want is that the image is fully displayed in the frame and after diplay on the background image the widgets.
You could put an image on a Canvas, and then place a Button on that by putting it inside a Canvas window object which can hold any Tkinter widget.
Additional widgets can be added in a similar fashion, each inside its own Canvas window object (since they can hold only a single widget each). You can workaround that limitation by placing a Frame widget in the Canvas window, and then putting other widgets inside it.
Here's an example showing how to display a single Button:
from PIL import Image, ImageTk
import tkinter as tk
IMAGE_PATH = 'sfondo.png'
WIDTH, HEIGTH = 200, 200
root = tk.Tk()
root.geometry('{}x{}'.format(WIDTH, HEIGHT))
canvas = tk.Canvas(root, width=WIDTH, height=HEIGTH)
canvas.pack()
img = ImageTk.PhotoImage(Image.open(IMAGE_PATH).resize((WIDTH, HEIGTH), Image.ANTIALIAS))
canvas.background = img # Keep a reference in case this code is put in a function.
bg = canvas.create_image(0, 0, anchor=tk.NW, image=img)
# Put a tkinter widget on the canvas.
button = tk.Button(root, text="Start")
button_window = canvas.create_window(10, 10, anchor=tk.NW, window=button)
root.mainloop()
Screenshot:
Edit
While I don't know of a way to do it in Frame instead of a Canvas, you could derive your own Frame subclass to make adding multiple widgets easier. Here's what I mean:
from PIL import Image, ImageTk
import tkinter as tk
class BkgrFrame(tk.Frame):
def __init__(self, parent, file_path, width, height):
super(BkgrFrame, self).__init__(parent, borderwidth=0, highlightthickness=0)
self.canvas = tk.Canvas(self, width=width, height=height)
self.canvas.pack()
pil_img = Image.open(file_path)
self.img = ImageTk.PhotoImage(pil_img.resize((width, height), Image.ANTIALIAS))
self.bg = self.canvas.create_image(0, 0, anchor=tk.NW, image=self.img)
def add(self, widget, x, y):
canvas_window = self.canvas.create_window(x, y, anchor=tk.NW, window=widget)
return widget
if __name__ == '__main__':
IMAGE_PATH = 'sfondo.png'
WIDTH, HEIGTH = 350, 200
root = tk.Tk()
root.geometry('{}x{}'.format(WIDTH, HEIGTH))
bkrgframe = BkgrFrame(root, IMAGE_PATH, WIDTH, HEIGTH)
bkrgframe.pack()
# Put some tkinter widgets in the BkgrFrame.
button1 = bkrgframe.add(tk.Button(root, text="Start"), 10, 10)
button2 = bkrgframe.add(tk.Button(root, text="Continue"), 50, 10)
root.mainloop()
Result:
Update
It recently dawned on me that there actually is a simpler way to do this than creating a custom Frame subclass as shown in my previous edit above. The trick is to place() a Label with image on it in the center of the parent Frame — you are then free to use other geometry managers like pack() and grid() as you normally would — as illustrated in the sample code below. Not only is it less complicated, it's also a more "natural" way of adding widgets than needing to call a non-standard method such as add().
from PIL import Image, ImageTk
import tkinter as tk
IMAGE_PATH = 'sfondo.png'
WIDTH, HEIGHT = 250, 150
root = tk.Tk()
root.geometry('{}x{}'.format(WIDTH, HEIGHT))
# Display image on a Label widget.
img = ImageTk.PhotoImage(Image.open(IMAGE_PATH).resize((WIDTH, HEIGHT), Image.ANTIALIAS))
lbl = tk.Label(root, image=img)
lbl.img = img # Keep a reference in case this code put is in a function.
lbl.place(relx=0.5, rely=0.5, anchor='center') # Place label in center of parent.
# Add other tkinter widgets.
button = tk.Button(root, text="Start")
button.grid(row=0, column=0)
button = tk.Button(root, text="Continue")
button.grid(row=0, column=1, padx=10)
root.mainloop()
Result#2
P.S. You can download a copy of the sfondo.png background image from here.

Categories

Resources