I have this message: image "pyimage2" doesn't exist . I want to have multi windows with images , how ?
Here is my code:
import Image
import ImageTk
import Tkinter
def new():
wind = Tkinter.Tk()
wind.geometry('600x600') # This not work, why?
imageFile2 = Image.open("someimage2.jpg")
image2 = ImageTk.PhotoImage(imageFile2)
panel2 = Tkinter.Label(wind , image=image2)
panel2.place(relx=0.0, rely=0.0)
wind.mainloop()
master = Tkinter.Tk()
master.geometry('600x600') # This work fine
imageFile = Image.open("someimage.jpg")
image1 = ImageTk.PhotoImage(imageFile)
panel1 = Tkinter.Label(master , image=image1)
panel1.place(relx=0.0, rely=0.0)
B = Tkinter.Button(master, text = 'New image', command = new).pack()
master.mainloop()
Change wind = Tkinter.Tk() to wind = Tkinter.Toplevel():
def new():
wind = Tkinter.Toplevel()
wind.geometry('600x600')
That's all you need to change.
Reference:
Tkinter Toplevel
Related
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()
First image is for background which is shows 2 times, And second one is a profile image which in not showing, Both photos are present in same root directory and have same format (jpg).
from tkinter import *
from PIL import ImageTk, Image
import json
with open('config.json') as config_data:
data = json.load(config_data)['variables']
root = Tk()
root.title(data['title'])
##TODO: for full screen
# root.wm_attributes('-fullscreen', 'true')
# MAIN BG SECTION:
image = Image.open("bg.jpg")
image = image.resize((1620, 880), Image.ANTIALIAS)
bg_image = ImageTk.PhotoImage(image)
# PHOTO FRAME SECTION:
canvas = Canvas(width=data['canvas_width'], heigh=data['canvas_height'], bg="gray11")
canvas.pack()
canvas.create_image(0, 0, image=bg_image, anchor=NW)
frame_image = Image.open("profile.jpg")
frame_image = image.resize((400, 400), Image.ANTIALIAS)
f_image = ImageTk.PhotoImage(frame_image)
photo_Label = Label(root, image=f_image).pack()
root.mainloop()
As #acw1668 mentioned frame_image = image.resize(...) should be frame_image = frame_image.resize(...)
I've got the following code:
import tkinter
import cv2
from PIL import Image, ImageTk
import threading
class HudGui:
def __init__(self, window):
# defining everything for video capture and the tkinter window
self.CapDev= cv2.VideoCapture(0)
self.cams = []
self.counter = 0
self.frame = None
self.panel = None
self.wind = window
btn = tkinter.Button(window, text="Snapshot!")
btn.pack(side="bottom", fill="both", expand="yes", padx=10, pady=10)
# defining threading for video stream
self.stopEvent = threading.Event()
self.thread = threading.Thread(target=self.videoLoop(), args=(1,))
self.thread.start()
def videoLoop(self):
while not self.stopEvent.is_set():
# reading and modifying the frame to have the proper data type
self.frame = self.CapDev.read()[1]
image = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
image = Image.fromarray(image)
image = ImageTk.PhotoImage(image)
if self.panel is None:
self.panel = tkinter.Label(image=image)
self.panel.image = image
self.panel.pack(side="left") # side="left", padx=10, pady=10
# otherwise, update the panel
else:
self.panel.configure(image=image)
self.panel.image = image
hud = tkinter.Tk()
hud.title("HUD")
HudGui(hud)
hud.mainloop()
When I run this code, nothing happens. I get no window but I do know it is updating the panel (I've tested it with printing messages). Could there be permissions problem for accessing the camera and things like that?
Project info:
Python = 3.7.3
opencv-contrib-python = 3.4.4.19
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 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 *).