How can I add picture to ttk Treeview? Example Code:
from tkinter import *
from ttk import *
from tkinter import ttk
import json
from PIL import Image, ImageTk
okno = Tk()
def maketable():
style = ttk.Style(okno)
style.configure("TVS.Treeview", rowheight=40)
tv = Treeview(okno, style="TVS.Treeview")
tv['columns'] = ('LName', 'Pic')
tv.heading("#0", text='Jméno', anchor='w')
tv.column("#0", anchor="w", width=200)
tv.heading('LName', text='Příjmení')
tv.column('LName', anchor='center', width=200)
tv.heading('Pic', text='Obrazek')
tv.column('Pic', anchor='center', width=200)
dbf = open("xxx.json", 'r')
db = json.loads(dbf.read())
for i in range(0, len(db)):
root_pic1 = Image.open(db[i]["Pic"])
root_pic2 = ImageTk.PhotoImage(root_pic1)
tv.insert('', 'end', image=root_pic2, text=db[i]['Name'], values=(db[i]['LName']))
tv.pack()
def main():
okno.mainloop()
if __name__ == '__main__':
maketable()
main()
I want the "Pic" Column to have pictures - this means a picture shouldn't be in #0 because it is a value. In db[i]["Pic"] there is a file path. In the values=() I can't add images—it doesn't show anything.
EDIT: Thanks for solution but doesnt work on multiple images:
for i in range(0, len(db)):
tv._image = Image
tv._image = Image.open(db[i]["Pic"])
tv._image.thumbnail((100, 200), PIL.Image.ANTIALIAS)
tv._image = ImageTk.PhotoImage(tv._image)
tv.insert('', 'end', image=tv._image, values=(db[i]['Name'], db[i]['LName']))
tv.pack()
Like #Nae and #furas pointed out, local variables are removed when function finishes and your images get garbage collected.
you would need to do something like:
tv._image = Image.open('test.png')
tv._image = ImageTk.PhotoImage(tv._image)
for i in range(0, len(db)):
tv.insert('', 'end', image=tv._image, text=db[i]['Name'], values=(db[i]['LName']))
or define them outside the function. It would be ideal if you were using classes and you can call self.root_pic = ...
EDIT: As for more images, you would need to create a list:
tv._images = []
for i in range(0, len(db)):
image = Image.open(db[i]["Pic"])
image = ImageTk.PhotoImage(image)
image.thumbnail((100, 200), PIL.Image.ANTIALIAS) # resize
tv._images.append(image)
tv.insert('', 'end', image=tv._images[-1], text=db[i]['Name'], values=(db[i]['LName']))
Related
I have been working on a project where I use labels that I want to disappear on click, but it only deletes the last label that was created. Here's my code:
from tkinter import *
import tkinter
import random
from PIL import Image, ImageTk
from functools import partial
width1=1280
height1=720
canvas = tkinter.Canvas(width=width1,height=height1, bg = 'white')
canvas.pack()
def clicked(*args):
label.destroy()
def square():
global label
global img
sq_time = random.randrange(4000,6000)
x = random.randrange(100,width1-40,40)
y = random.randrange(40,height1-40,40)
label = Label(canvas, image = img)
label.place(x = x , y = y)
label.bind("<Button-1>",partial(clicked))
canvas.after(sq_time, square)
img = ImageTk.PhotoImage(Image.open('froggy.png'))
square()
mainloop()
froggy.png is a image that I have saved in the same folder as the code. Can someone tell me how do I delete the label that was clicked?
In tkinter event handler functions are automatically passed an event object argument that, among other things, has an attribute that identifies the widget that triggered them. This means you can use that instead of creating a partial to get the information needed.
from tkinter import *
import tkinter
import random
from PIL import Image, ImageTk
width1 = 1280
height1 = 720
canvas = tkinter.Canvas(width=width1, height=height1, bg='white')
canvas.pack()
def clicked(event):
event.widget.destroy()
def square():
global label
global img
sq_time = random.randrange(4000, 6000)
x = random.randrange(100, width1-40, 40)
y = random.randrange(40, height1-40, 40)
label = Label(canvas, image = img)
label.place(x=x, y=y)
label.bind("<Button-1>", clicked)
canvas.after(sq_time, square)
img = ImageTk.PhotoImage(Image.open('froggy.png'))
square()
mainloop()
def on_click():
label.after(1000, label.destroy)
Button(win, text="Delete", command=on_click).pack()
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()
I have a issue in tkinter for python 3. I would like to create an animating game character in python, tkinter without using PIL. I found a way to animate the character using a gif, but I do not know how to move the gif I tried to use canvas.move
here is my code:
from tkinter import *
import os
import time
root = Tk()
c = Canvas(root,width = 500,height = 500)
c.pack()
frames = [PhotoImage(file=(os.path.expanduser("~/Desktop/DaQueenIDLE.gif")),format = 'gif -index %i' % (i)) for i in range(2)]
def update(ind):
frame = frames[ind]
ind += 1
if ind >= 2:
ind = 0
label.configure(image=frame)
root.after(100, update, ind)
label = Label(root)
label.pack()
root.after(0, update, 0)
c.move(frames,0,-100)
root.update()
root.mainloop()
move is a method for Canvas, and its first argument needs to be an item on Canvas.
In your case frames is not an item on the Canvas.
Replace:
def update(ind):
#...
label.configure(image=frame)
root.after(100, update, ind)
label = Label(root)
label.pack()
with:
def update(ind):
#...
c.itemconfig(character, image=frame)
c.move(character, 1, 1)
root.after(100, update, ind)
character = c.create_image((47,47), image=frames[0])
To convert your label into an image item in Canvas and move it.
Example
Below is a complete example that downloads(you can comment download_images out after the initial run) .gif images below online:
and then moves an image while animating between the two:
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except ImportError:
import Tkinter as tk
def download_images():
# In order to fetch the image online
try:
import urllib.request as url
except ImportError:
import urllib as url
url.urlretrieve("https://i.stack.imgur.com/57uJJ.gif", "13.gif")
url.urlretrieve("https://i.stack.imgur.com/8LThi.gif", "8.gif")
def animate_and_move(i):
i = (i + 1) % 2
canvas.itemconfig(moving_image, image=canvas.images[i])
canvas.move(moving_image, 1, 1)
canvas.after(100, animate_and_move, i)
if __name__ == '__main__':
download_images() # comment out after initial run
root = tk.Tk()
canvas = tk.Canvas(root, height=644, width=644, bg='#ffffff')
canvas.images = list()
canvas.images.append(tk.PhotoImage(file="8.gif"))
canvas.images.append(tk.PhotoImage(file="13.gif"))
moving_image = canvas.create_image((164, 164), image=canvas.images[0])
animate_and_move(0)
canvas.pack()
root.mainloop()
Note that if:
import tkinter
tkinter.TkVersion >= 8.6
returns True then .png files are also supported without an additional library.
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 *).
At first I implemented this code which uses classes and worked fine:
from Tkinter import *
import numpy as np
from PIL import Image,ImageTk
import time
#----------------------------------------------------------------------
class MainWindow():
#----------------
def __init__(self,main,pix):
# canvas for image
self.canvas = Canvas(main, width=424, height=424)
self.canvas.grid(row=0, column=0)
# images
self.im=Image.fromarray(pix.astype('uint8'))
self.photo = ImageTk.PhotoImage(image=self.im)
self.my_images = self.photo
self.my_image_number = 0
# set first image on canvas
self.image_on_canvas = self.canvas.create_image(0, 0, anchor = NW, image = self.my_images )
self.i=0
self.j=0
main.after(1,self.onButton,main,pix)# button to change image
main.update()
#----------------
def onButton(self,main,pix):
print self.i,self.j
if self.j==100:
return
pix[self.i][self.j]=255-pix[self.i][self.j]
self.i+=1
if self.i==100:
self.i=0
self.j+=1
self.im=Image.fromarray(pix.astype('uint8'))
self.photo = ImageTk.PhotoImage(image=self.im)
self.my_images = self.photo
self.canvas.itemconfig(self.image_on_canvas, image = self.my_images)
main.after(1,self.onButton,main,pix)
else:
main.after(0,self.onButton,main,pix)
#----------------------------------------------------------------------
root = Tk()
pix=np.array([[(i,j,255) for i in range(256)] for j in range(255,-1,-1)])
x=MainWindow(root,pix)
root.mainloop()
Later I tried to use the same functions without the class and it looks like this:
from Tkinter import *
import numpy as np
from PIL import Image,ImageTk
import time
def onButton(main,pix):
global i,j
if j==100:
return
pix[i][j]=255-pix[i][j]
i+=1
if i==100:
i=0
j+=1
im=Image.fromarray(pix.astype('uint8'))
photo = ImageTk.PhotoImage(image=im)
my_images = photo
canvas.itemconfig(image_on_canvas, image = my_images)
main.after(1,onButton,main,pix)
else:
main.after(0,onButton,main,pix)
root = Tk()
pix=np.array([[(i,j,255) for i in range(256)] for j in range(255,-1,-1)])
canvas = Canvas(root, width=424, height=424)
canvas.grid(row=0, column=0)
im=Image.fromarray(pix.astype('uint8'))
photo = ImageTk.PhotoImage(image=im)
image_on_canvas = canvas.create_image(0, 0, anchor = NW, image = photo )
i,j=0,0
root.after(1,onButton,root,pix)# button to change image
print "hi"
root.mainloop(f i==100:
i=0
j+=1
im=Image.fromarray(pix.astype('uint8'))
photo = ImageTk.PhotoImage(image=im)
my_images = photo
canvas.itemconfig(image_on_canvas, image = my_images)
main.after(1,onButton,main,pix)
else:
main.after(0,onButton,main,pix)
root = Tk()
pix=np.array([[(i,j,255) for i in range(256)] for j in range(255,-1,-1)])
canvas = Canvas(root, width=424, height=424)
canvas.grid(row=0, column=0)
im=Image.fromarray(pix.astype('uint8'))
photo = ImageTk.PhotoImage(image=im)
image_on_canvas = canvas.create_image(0, 0, anchor = NW, image = photo )
i,j=0,0
root.after(1,onButton,root,pix)# button to change image
print "hi"
root.mainloop()
Why does this not work? This is the first time I am working with tkinter, so I am probably missing something crucial. What do I need to change?
I'm curious why you need to implement such a thing without a class, but first let's look at your problem, namely the OnButton function.
You have already defined global variables i and j. But how about others?
All variables that you are trying to mutate (and keep value) must also be declared as global! This is a root of your problem and as for the answer to the question "why doesn't it work" - #PM 2Ring give you a good refer to photoimage docs.
Note: When a PhotoImage object is garbage-collected by Python (e.g. when you return from a function which stored an image in a local variable), the image is cleared even if it’s being displayed by a Tkinter widget.
try:
from tkinter import *
except ImportError:
from Tkinter import *
import numpy as np
from PIL import Image,ImageTk
def onButton():
global i, j, photo #, pix
print(i, j)
if j == 100:
return
pix[i][j] = 255-pix[i][j]
i += 1
if i == 100:
i = 0
j += 1
photo = ImageTk.PhotoImage(image=Image.fromarray(pix.astype('uint8')))
canvas.itemconfig(image_on_canvas, image=photo)
root.after(1, onButton)
else:
root.after(0, onButton)
root = Tk()
pix=np.array([[(i, j, 255) for i in range(256)] for j in range(255, -1, -1)])
canvas = Canvas(root, width=424, height=424)
canvas.grid(row=0, column=0)
photo = ImageTk.PhotoImage(image=Image.fromarray(pix.astype('uint8')))
image_on_canvas = canvas.create_image(0, 0, anchor=NW, image=photo)
i, j = 0, 0
root.after(1, onButton) # button to change image
print("hi")
root.mainloop()
And if there's no need for this variables outside function - then make'em local and move all relevant code into function!
After all, please, take a time to read this article. Maybe you change your mind about the classes (and about wildcard import).