Replace Canvas Image in Tkinter - python

I have a program and I want when someone clicks a button, the canvas image will change. My code is below:
from PIL import ImageTk,Image, ImageFont, ImageDraw
import tkinter
import textwrap
from tkinter import Frame, Canvas, Text, INSERT, END
root = tkinter.Tk()
root.geometry("296x337")
root.resizable(False, False)
im=Image.open("red.jpg")
photo=ImageTk.PhotoImage(im)
cv = tkinter.Canvas()
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(0, 0, image=photo, anchor='nw')
def changepic():
###place where I want to change the Canvas Image
print("change color")#I added this because python wouldn't let me run thee function without something.
a2=tkinter.Button(root,text='change color',bd=0, command=changepic)
a2.config(highlightbackground='black')
a2.place(x=135, y=70)

Instead of using Canvas, I replaced the code so that it prints the image using tkinter.Label:
from PIL import ImageTk,Image, ImageFont, ImageDraw
import tkinter
import textwrap
from tkinter import Frame, Canvas, Text, INSERT, END
root = tkinter.Tk()
root.geometry("296x337")
root.resizable(False, False)
img = ImageTk.PhotoImage(Image.open("red.jpg"))
panel = tkinter.Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
def changepic(imagename):
img2 = ImageTk.PhotoImage(Image.open(imagename))
panel.configure(image=img2)
panel.image = img2
a2=tkinter.Button(root,text='change color',bd=0, command=changepic("blue.jpg")
a2.config(highlightbackground='black')
a2.place(x=135, y=70)
I got my information from: How to update the image of a Tkinter Label widget?
And: https://www.tutorialspoint.com/python/tk_label.htm

Related

Make gif transapernt on tkinter canvas

I have the below code to show my animated gif on a canvas but even though the gif has a transpert bg, it get in a white background. is there a way to remove it?
import tkinter as tk
from PIL import Image, ImageTk
from itertools import cycle
DELAY = 20
def animate():
canvas.itemconfig(img,image=next(frames_cycle))
root.after(DELAY,animate)
root = tk.Tk()
root.geometry('300x300')
root.config(background='red')
frames = []
im = Image.open('loading_circle.gif')
for frame in range(im.n_frames):
im.seek(frame)
frames.append(ImageTk.PhotoImage(im.convert('RGBA')))
frames_cycle=cycle(frames)
canvas = tk.Canvas(root,bg='red', width=300, height=300)
img = canvas.create_image(120,0,anchor='nw',image=next(frames_cycle))
canvas.pack()
animate()
root.resizable(False, False)
root.mainloop()

How to create an image button in tkinter? and how to put into another interface?

from tkinter import *
from PIL import ImageTk, Image
root = Tk()
def save():
editor = Tk()
img= Image.open('images/save.png')
fixed= ImageTk.PhotoImage(img)
btn_save_editor = Button(editor, image=fixed)
btn_save_editor.place(x=30, y=80, height=20)
root.mainloop()
From another interface, if you mean another window, then you should use a TopLevel widget, not TK
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
def save():
editor = Toplevel()
editor.geometry("500x500") # you can change it
img = Image.open('images/save.png')
fixed= ImageTk.PhotoImage(img)
btn_save_editor = Button(editor, image=fixed)
btn_save_editor.place(x=30, y=80, height=20)
save()
root.mainloop()

Tkinter Display picture when calling function

I wanted to display a picture .png in tkinter when calling a function (but it could also be with a boolean showOnOff).
This is what I wrote for the moment, but the picture doesn't appear. Do you have any idea ?
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
root = Tk()
def display():
# Use library PIL to display png picture
path = '3d.png'
img = ImageTk.PhotoImage(Image.open(path), Image.ANTIALIAS)
panel = Label(root, image = img)
panel.grid(row=1, column=0)
ButtonDisplay = ttk.Button(root, text="Display", command=display)
ButtonDisplay.grid(row=0, column=0)
root.mainloop()
The problem is the definition of the image which is only local inside the funtion. If you make the image global, it works:
def display():
global img
# Use library PIL to display png picture
path = '3d.png'
img = ImageTk.PhotoImage(Image.open(path))
panel = Label(root, image=img)
panel.grid(row=1, column=0)

tkinter - rectangle hidden under background image

I am trying to create a really simple HMI, to display different images and name of the file and other information in a lower bar using tkinter. I have created a layout, added the background image but the lower rectangle is not visible.
The background image overlays the rectangle, how can I force the ground image to behind all the other components inside the canvas? The black rectangle should be visible at the lower section of the picture
from PIL import Image, ImageTk
from tkinter import Tk, BOTH, Canvas
from tkinter.ttk import Frame, Label, Style
root = Tk()
root.title('Screen')
w = Canvas(root, width=800 , height=480)
w.pack()
back_ground = ImageTk.PhotoImage(Image.open("./icon/sync_background.bmp"))
back_ground_label = Label(image=back_ground, borderwidth=0)
back_ground_label.place(x=0,y=0)
w.create_rectangle(0, 400, 800, 480,outline="#000", fill="#000")
w.pack()
root.mainloop()
Your background image is placed on top of the canvas and so it hides the canvas. You should create the image using w.create_image(...) instead:
from PIL import Image, ImageTk
from tkinter import Tk, BOTH, Canvas
root = Tk()
root.title('Screen')
back_ground = ImageTk.PhotoImage(Image.open("./icon/sync_background.bmp"))
w = Canvas(root, width=800 , height=480)
w.pack()
w.create_image(0, 0, image=back_ground, anchor='nw')
w.create_rectangle(0, 400, 800, 480,outline="#000", fill="#000")
root.mainloop()
Here is what you can do:
from PIL import Image, ImageTk
from tkinter import Tk, BOTH, Canvas
from tkinter.ttk import Frame, Label, Style
root = Tk()
root.title('Screen')
w = Canvas(root, width=800 , height=480)
w.pack()
back_ground = ImageTk.PhotoImage(Image.open("./icon/sync_background.bmp"))
back_ground_label = Label(image=back_ground, borderwidth=0)
back_ground_label.place(x=0,y=0)
rect = w.create_rectangle(0, 400, 800, 480,outline="#000", fill="#000")
w.tag_raise(rect)
w.pack()
root.mainloop()
What this does is creates an object "rect" and then uses the tag_raise method on it to bring it in front.
Hope it helps!
Cheers!

Display an image tkinter macOSX [duplicate]

How do I insert a JPEG image into a Python 2.7 Tkinter window? What is wrong with the following code? The image is called Aaron.jpg.
#!/usr/bin/python
import Image
import Tkinter
window = Tkinter.Tk()
window.title("Join")
window.geometry("300x300")
window.configure(background='grey')
imageFile = "Aaron.jpg"
window.im1 = Image.open(imageFile)
raw_input()
window.mainloop()
Try this:
import tkinter as tk
from PIL import ImageTk, Image
#This creates the main window of an application
window = tk.Tk()
window.title("Join")
window.geometry("300x300")
window.configure(background='grey')
path = "Aaron.jpg"
#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
img = ImageTk.PhotoImage(Image.open(path))
#The Label widget is a standard Tkinter widget used to display a text or image on the screen.
panel = tk.Label(window, image = img)
#The Pack geometry manager packs widgets in rows or columns.
panel.pack(side = "bottom", fill = "both", expand = "yes")
#Start the GUI
window.mainloop()
Related docs: ImageTk Module, Tkinter Label Widget, Tkinter Pack Geometry Manager
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
win = tk. Tk()
image1 = Image. open("Aoran. jpg")
image2 = ImageTk. PhotoImage(image1)
image_label = ttk. Label(win , image =.image2)
image_label.place(x = 0 , y = 0)
win.mainloop()
from tkinter import *
from PIL import ImageTk, Image
window = Tk()
window.geometry("1000x300")
path = "1.jpg"
image = PhotoImage(Image.open(path))
panel = Label(window, image = image)
panel.pack()
window.mainloop()

Categories