How to change image in canvas?[python tkinter] - python

As below, I want to show an image in canvas using tkinter, and when a click the button, an other photo should be shown. But I failed. The first image shows well, but the image didn't change when I click the button
C = Tkinter.Canvas(top, bg="#fff", height=500, width=600)
// show image1 in canvas first and it works
itk = ImageTk.PhotoImage(img1)
C.create_image(300, 250, image=itk)
C.pack()
def changeImage():
// I want to show image2 in canvas, but I fails
print 'change image in canvas'
itk2 = ImageTk.PhotoImage(img2)
C.create_image(300, 250, image=itk2)
button = Tkinter.Button(top,text='click', command=changeImage)
button.pack()
top.mainloop()

Changes one or more options for all matching items. 1
myimg = C.create_image(300, 250, image=itk)
def changeImage():
// I want to show image2 in canvas, but I fails
print 'change image in canvas'
itk2 = ImageTk.PhotoImage(img2)
C.itemconfigure(myimg, image=itk2)

itk2 is destroyed once the function exits (and you will get syntax errors on other lines of the code). One solution, of many, is to keep it outside of the function. Consider this pseudo-code as I don't have time to test it.
class ImageTest():
def __init__(self):
self.root = tk.Tk()
self.root.title('image test')
self.image1 = ImageTk.PhotoImage(img1)
self.image2 = ImageTk.PhotoImage(img2)
self.displayed=True
self.panel1 = Tkinter.Canvas(top, bg="#fff", height=500, width=600)
self.panel1.create_image(300, 250, image=self.image1)
self.panel1.pack()
tk.Button(self.root, text="Next Pic", command=self.callback,
bg="lightblue").pack()
tk.Button(self.root, text="Exit", command=quit, bg="red").pack()
self.root.mainloop()
def callback(self):
if self.displayed:
self.panel1["image"]=self.image2
else:
self.panel1.config(image=self.image1)
self.displayed=not self.displayed
IT=ImageTest()

Related

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.

Is it possible to change the content in canvas.create_text by clicking on it just like in Entry?

Following is my code,
from tkinter import *
window = Tk()
canvas = Canvas(window,width=300, height=300, bd=0)
canvas.pack()
background = PhotoImage(file="Images/background.png") # can be any background image
canvas.create_image(300,300,image=background)
canvas_textbox = canvas.create_text(20, 70, text='TOUCH ME TO EDIT THIS TEXT', anchor=NW, fill="lime")
window.mainloop()
Is there any possibilities to change the canvas.create_text so that it can function just like Entry (gives the text cursor when user clicks on it for edit text) but looks like canvas.create_text only.
canvas_textbox = canvas.create_text() will return an object id(numeric)
Firstly, Bind the canvas to the mouse. Then pass the mouse position to closest=canvas.find_closest(x, y), which will return the item(s) id under the x,y position.
Now check whether the object id text is in the closest. If it is in the closest use create_window to place the Entry widget at the mouse position or the position f your choice.
Here is the code:
from tkinter import *
from PIL import Image, ImageTk
def update(event):
canvas.delete('entry')
canvas.itemconfig(tagOrId='text', text=text_box.get())
def clicked(event):
closest = canvas.find_closest(event.x, event.y)# returns the closest item to x, y in the form of tuple
if 2 in closest:
canvas.itemconfig(tagOrId='text', text='')
canvas.create_window(event.x, event.y, window=text_box, tag='entry')
else:
print('No')
window = Tk()
canvas = Canvas(window,width=300, height=300, bd=0)
canvas.pack()
background = ImageTk.PhotoImage(Image.open(r"\path.jpg")) # can be any background image
canvas.create_image(300,300,image=background)
canvas_textbox = canvas.create_text(20, 70, text='TOUCH ME TO EDIT THIS TEXT', anchor=NW, fill="lime", tag='text')
text_box = Entry(window)
text_box.bind('<Return>', update)
print(canvas.find_all()) # returns all the items in canvas as tuple
canvas.bind('<Button>', clicked)
window.mainloop()
Or you may also try this:
from tkinter import *
from PIL import Image, ImageTk
def update(event):
canvas.delete('entry')
canvas.itemconfig(tagOrId='text', text=text_box.get())
def clicked(event):
closest = canvas.find_closest(event.x, event.y)# returns the closest item to x, y in the form of tuple
x, y = canvas.coords(closest)
if canvas_textbox in closest:
canvas.itemconfig(tagOrId='text', text='')
canvas.create_window(x+100, y, window=text_box, tag='entry')
else:
print('No')
window = Tk()
canvas = Canvas(window,width=300, height=300, bd=0)
canvas.pack()
background = ImageTk.PhotoImage(Image.open(r"\image")) # can be any background image
canvas.create_image(300,300,image=background)
canvas_textbox = canvas.create_text(20, 70, text='TOUCH ME TO EDIT THIS TEXT', anchor=NW, fill="lime", tag='text')
text_box = Entry(window, borderwidth=0, highlightthickness=0)
text_box.insert(0, 'TOUCH ME TO EDIT THIS TEXT')
print(canvas.coords(2))
text_box.bind('<Return>', update)
print(canvas.find_all()) # returns all the items in canvas as tuple
canvas.bind('<Double-1>', clicked)
window.mainloop()
(double click on the text)
I'd do it like that. But you should consider different windows for Output and control. But here how it works.
from tkinter import *
window = Tk()
canvas = Canvas(window, width=300, height=300, bd=0)
canvas.pack()
def updatetext(): #create a function
x = newtext.get()
print(x)
canvas.itemconfig(tagOrId='text', text = x)
background = PhotoImage(file="background.png") # can be any background image
canvas.create_image(300, 300, image=background)
newtext = StringVar(value = 'Your new text')
canvas.create_text(20, 70, text='TOUCH ME TO EDIT THIS TEXT', anchor=NW, fill="lime", tag = 'text')
Entry(window, textvariable = newtext).pack()
Button(window, command = updatetext, text = 'Update text').pack()
window.mainloop()
Canvas is now updated with the Update button. And use tags, it is a really powerful thing in Tkinter and very handy. If you want text to be updated without pressing update button then use built-in after.

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.

How do I switch images before clicking a button in Python using TKinter

In the following code, I want to display picture1.jpg as soon as button1 is pressed. After some time, I want to display picture2.jpg. In the following case, only picture2.jpg is displayed 5 seconds after the button1 is pressed. How can I change pictures?
from Tkinter import *
from PIL import Image, ImageTk
import time
def show_image():
while True:
image = Image.open("picture1.jpg")
tk_img = ImageTk.PhotoImage(image)
x = canvas.create_image(400, 300, image=tk_img)
canvas.itemconfigure(x, state=NORMAL)
# I want to show picture1.jpg now
time.sleep(5) #This is a dummy because it takes a while for picture2.jpg
#Now I can show picture2.jpg
image = Image.open("picture2.jpg")
tk_img = ImageTk.PhotoImage(image)
x = canvas.create_image(400, 300, image=tk_img)
canvas.itemconfigure(x, state=NORMAL)
yield
root = Tk()
canvas = Canvas(root, width=850, height=750)
canvas.grid(row=0, column=0)
button1 = Button(
root, text="Insert next device and then take a picture", command=show_image().next, anchor='w',
width=50, activebackground="#33B5E5" ,fg = "blue" , font=("Arial", 24))
button1.grid(row=1, column=0)
root.mainloop()
First, as a general rule you should never call sleep in the main thread of a GUI. It causes your whole program to pause -- buttons won't work, the screen won't update, etc.
Tkinter has a way to run code in the future. Change the image, and then use this function to change it back later. Roughly speaking, it would look like this:
def show_image(path):
image = Image.open(path)
tk_img = ImageTk.PhotoImage(image)
x = canvas.create_image(400, 300, image=tk_img)
def switch_images():
show_image("picture1.jpg")
root.after(5000, show_image, "picture2.jpg")

Displaying .jpg image when button is clicked in Tkinter?

This seems like a pretty straightforward question, but i am having trouble displaying a jpg image when a button is clicked. Here is my code (without the button code for the sake of time):
from tkinter import *
#screen stuff here
canvas = Canvas(app)
canvas.grid(row = 0,column = 0)
photo = PhotoImage(file = "test.jpg")
canvas.create_image(0,0, image = photo)
def show_image():
global canvas
global photo
canvas.create_image(0,0, image = photo)
#button that calls the function down here
Thanks!
This works with Python2:
import Tkinter as tk
import ImageTk
def show_image():
x = canvas.create_image(125, 125, image=tk_img)
while True:
print('show')
canvas.itemconfigure(x, state=tk.NORMAL)
button.configure(text = 'Hide')
yield
print('hide')
canvas.itemconfigure(x, state=tk.HIDDEN)
button.configure(text = 'Show')
yield
root = tk.Tk()
canvas = tk.Canvas(root, width=250, height=250)
canvas.grid(row=0, column=0)
tk_img = ImageTk.PhotoImage(file='image.png')
button = tk.Button(
root, text="Show", command=show_image().next, anchor='w',
width=10, activebackground="#33B5E5")
button.grid(row=1, column=0)
root.mainloop()
In Python3, PhotoImage can open GIF, PPM/PGM images. To open other formats, you may need to install Pillow (a fork of the PIL project for Python3).

Categories

Resources