img= (Image.open("image/frame.png")).resize((240, 240), Image.ANTIALIAS)
new_image = ImageTk.PhotoImage(img)
panel = tk.Label(right_workspace, image=new_image)
panel.pack(side = "top", fill = "none", expand = "none", pady=29)
Thats my label with its image background. Now how can I change this background by a function so everytime my program generates a new qrcode from an input it replaces previous background?
I gather what you're doing, but please leave a minimum reproducible code in future.
Here's an example of how you'd create a functioning tkinter GUI that displays a random image from a list of QR .pngs in a set folder when the button is pressed. You should be able to adapt this to the other part of your program that generates the QR code for you.
import tkinter as tk
from PIL import Image, ImageTk
import os
import random
class QrGenerator:
def __init__(self, main):
self.main = main
self.panel = tk.Label(main)
self.panel.grid(row=0, column=0)
self.button = tk.Button(main, text='Random QR', command=self.random_qr)
self.button.grid(row=1, column=0)
def random_qr(self):
fp = r'C:\Filepath\QR Codes Folder'
os.chdir(fp)
qr_code = random.choice(os.listdir(fp))
print(qr_code)
img = Image.open(qr_code).resize((240, 240), Image.ANTIALIAS)
new_image = ImageTk.PhotoImage(img)
self.panel.configure(image=new_image)
self.panel.image = new_image
if __name__ == '__main__':
root = tk.Tk()
gui = QrGenerator(root)
root.mainloop()
Related
i am using Python and I inserted a gif in my project. The issue is when i start the application the gif in the application runs in different way than orginal gif. I think that the frames in the gif are run in different speed. How to set the original gif?
I imported PIL and tkinter. This is the code:
import threading
from tkinter import *
from PIL import Image, ImageTk, ImageSequence
def play_gif():
global img
img = Image.open("Gifs/beaming_face_with_smiling_eyes.gif")
lbl = Label(root)
lbl.place(x = 0, y = 0)
for img in ImageSequence.Iterator(img):
img = ImageTk.PhotoImage(img)
lbl.config(image = img)
root.update()
def exit():
root.destroy()
threading.Timer(3.0, play_gif).start()
Button(root,text = "exit", command=exit).place(x = 450, y = 300)
root.mainloop()
Currently, the code chooses images from a folder and displays it on the tkinter window. I can click 'Next image' to see the next image from the folder. I would like to make the images clickable, so that it take would take me to a specified link. I can make it clickable but not sure how to bind the different links to each image. It will get confusing if I were to randomize the images(planning to do this later)
import tkinter as tk
from tkinter.filedialog import askdirectory
import os
img_list = []
def save_to_list(event):
click_loc = [event.x, event.y]
print ("you clicked on", click_loc)
img_list.append(click_loc)
def next_img():
img_label.img = tk.PhotoImage(file=next(imgs))
img_label.config(image=img_label.img)
root = tk.Tk()
root.geometry('500x500')
# Choose multiple images
img_dir = askdirectory(parent=root, initialdir="./yoga_Images/", title='Choose folder')
os.chdir(img_dir)
imgs = iter(os.listdir(img_dir))
img_label = tk.Label(root)
img_label.pack()
img_label.bind("<Button-1>",save_to_list)
btn = tk.Button(root, text='Next image', command=next_img)
btn.pack()
next_img()
root.mainloop()
You can simply specify a image object when creating a button.
from PIL import Image, ImageTk
import tkinter as tk
def example():
print("Clickable Image!")
root = tk.Tk()
image = Image.open("image.png")
btnPhoto= ImageTk.PhotoImage(image)
imgBtn = tk.Button(root, image=btnPhoto, command=example)
imgBtn.pack()
root.mainloop()
Here is some code if you want to make multiple clickable images, don't forget to specify your image directory (Apologies for using list comprehensions, I know they are somewhat confusing).
from PIL import Image, ImageTk
import tkinter as tk
import os
def example():
print("Clickable Image!")
root = tk.Tk()
imgDir = "imgs"
images = [ImageTk.PhotoImage(Image.open(os.path.join(imgDir, imgName))) for imgName in os.listdir(imgDir)]
for img in images:
imgBtn = tk.Button(root, image=img, command=example)
imgBtn.pack()
root.mainloop()
Hello I am having issues with resizing my picture. I am trying to resize the image to fit the blue drawing. However the way I am doing it, returns an error.
File "gui.py", line 42, in fileDialog
self.display = Label(image=self.photo.resize((800, 600),Image.ANTIALIAS))
AttributeError: 'PhotoImage' object has no attribute 'resize
I am just testing it to see if it fits by doing 800,600 I really don't know.
def fileDialog(self):
self.filename = filedialog.askopenfilename(title="Select")
self.label = ttk.Label(self.labelFrame, text="")
self.label.grid(column=1, row=2)
self.label.configure(text=self.filename)
self.photo= ImageTk.PhotoImage(file = self.filename)
self.display = Label(image=self.photo.resize((800, 600),Image.ANTIALIAS))
self.display.grid(row=0)
Is there something that I am doing incorrectly? Please advise.
You need to resize the image, not the photoimage.
import tkinter as tk
from PIL import Image, ImageTk
filename = 'bell.jpg'
img = Image.open(filename)
resized_img = img.resize((200, 100))
root = tk.Tk()
root.photoimg = ImageTk.PhotoImage(resized_img)
labelimage = tk.Label(root, image=root.photoimg)
labelimage.pack()
To address the new question, you do not have to know the filename at the time of label creation. The following code produces the same result:
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
labelimage = tk.Label(root)
labelimage.pack()
filename = 'bell.jpg'
img = Image.open(filename)
resized_img = img.resize((200, 100))
root.photoimg = ImageTk.PhotoImage(resized_img)
labelimage.configure(image=root.photoimg)
So im trying to make an app that will display images, and the image I have is 1000*1000 but this is way too big, I need a way to resize the image. I've tried using PIL and ImageTK but that didn't work, here's my code so far:
from tkinter import *
app = Tk()
app.title('embeded image')
fname = Canvas(bg = 'black', height=100, width=100)
fname.pack(side=TOP)
image = PhotoImage('Sun.png')
image = image.resize((25, 25), Image.ANTIALIAS)
icon = fname.create_image(image=image)
fname.pack()
app.mainloop()
I've no idea why this doesn't work, im relatively new to Tkinter so sorry if it's obvious.
You mix two differnt class PhotoImage in tkinter which doesn't have resize and PIL.Image which have resize
import tkinter as tk
from PIL import Image, ImageTk
app = tk.Tk()
fname = tk.Canvas(height=200, width=200)
fname.pack()
pil_image = Image.open('Sun.png')
pil_image = pil_image.resize((25, 25), Image.ANTIALIAS)
image = ImageTk.PhotoImage(pil_image)
icon = fname.create_image((0,0), image=image, anchor='nw')
app.mainloop()
So I made a script in python with Tkinter and the thing is that the first Tkinter window pops up without problems but when the code goes to the second window it says :
_tkinter.TclError: image "pyimage1" doesn't exist
and I didn't find anything that helped me, could someone help me please ?
Here is the code :
from Tkinter import *
from PIL import ImageTk, Image
def choose():
global name, chosen
name = name1.get()
chosen = chosen1.get()
print name
print chosen
root0.quit()
root0 = Tk()
name1 = Entry(root0)
name1.pack()
chosen1 = Entry(root0)
chosen1.pack()
Button(root0, text="ENTER", command=choose).pack()
root0.mainloop()
root = Tk()
img = ImageTk.PhotoImage(Image.open('person1.png'))
panel1 = Label(root, image = img)
panel1.pack(side="left")
img2 = ImageTk.PhotoImage(Image.open('person2.png'))
panel2 = Label(root, image = img2)
panel2.pack(side="right")
root.mainloop()
by the way, the python version is 2.7
This is a side effect of using 2 roots (Tk() instances). The images default to associate with the first root window. The quick fix is to provide the image with the correct root:
img2 = ImageTk.PhotoImage(Image.open('person2.png'), master=root)
The proper fix is to never use more than one Tk(). Put all your code into Frame instances, and then destroy one and load the other when the time is right:
import Tkinter as tk
def choose():
global name, chosen
name = name1.get()
chosen = chosen1.get()
print name
print chosen
frame0.destroy() # kill this frame
frame1.pack() # open new frame
root = tk.Tk()
frame0 = tk.Frame(root)
name1 = tk.Entry(frame0)
name1.pack()
chosen1 = tk.Entry(frame0)
chosen1.pack()
tk.Button(frame0, text="ENTER", command=choose).pack()
frame1 = tk.Frame(root)
img = ImageTk.PhotoImage(Image.open('person1.png'))
panel1 = tk.Label(frame1, image = img)
panel1.pack(side="left")
img2 = ImageTk.PhotoImage(Image.open('person2.png'))
panel2 = tk.Label(frame1, image = img2)
panel2.pack(side="right")
#start the program
frame0.pack() # load frame0
root.mainloop()
Note I also moved you away from the evil wildcard imports (from module import *).