Treeview Image not displaying - python-3.x

having problems displaying an image using treeview. Found some other mentions on the net with a similar problem but the answers do not seem to work for me. I am using Win10 if that makes a difference
import ttkthemes
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo
from tkinter import PhotoImage
from PIL import Image, ImageTk
self = tk.Tk()
self.title('Tkinter PhotoImage Demo')
#self.image = Image.open("A4.bmp")
#self.python_image = ImageTk.PhotoImage(self.image)
#print(self.python_image)
#ttk.Label(self, image=self.python_image).pack()
# columns
columns = ('#1', '#2')
tree = ttk.Treeview(self, columns=columns, show='headings', height=20)
tree.tag_configure('oddrow', background='#ece0cf')
tree.tag_configure('evenrow', background='#e0e0e0')
style = ttk.Style()
style.theme_use('clam')
style.configure("Treeview",font=(None,12))
style.configure("Treeview.Heading", font=(None, 12))
# define headings
tree.heading('#1', text='Date')
tree.column("#1", minwidth=0, width=160)
tree.heading('#2', text='Logo')
tree.column("#2", minwidth=0, width=80)
# add a scrollbar
scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL, command=tree.yview)
tree.configure(yscroll=scrollbar.set)
scrollbar.grid(row=0, column=1, sticky='ns')
tree.grid(row=0, column=0, sticky='nsew')
logo="A4.bmp"
rowcol='oddrow'
im = Image.open(logo)
ph = ImageTk.PhotoImage(im)
print(ph)
tree.insert('', -1, values=("2021-03-25", logo), image=ph, tags=(rowcol,))
tree.grid(row=0, column=0, sticky='nsew')
self.update_idletasks()
self.update()
If it helps the value of ph is: pyimage1
The commented out code at the start displays the image just fine, so the issue seems to be on adding the image itself into the tree.insert.... bit of coding
Whilst I have your attention and as related but very minor questions, is there also a way to add a second image into treeview? Plus is there a way to display some values, then an image, then some more values, then another image and finally more values, or do the images have to be at the start or end of the rows?

Related

Insert an image in a labelFrame title with tkinter

I wonder if it's possible to insert an image or icon in the title of a Labelframe in tkinter ?
Maybe something like this lf = Labelframe(root, image='my_img'), the same as we can do with a button.
It would be very useful.
You can insert any widget as labelwidget option of LabelFrame.
If you want an image instead of text displayed in LabelFrame:
import tkinter as tk
from PIL import Image, ImageTk
w = tk.Tk()
w.geometry("400x500")
img_open = Image.open("example_1.png")
img = ImageTk.PhotoImage(img_open)
label_title = tk.Label(w, image=img) #important Do Not grid/pack/place it!
label_frame = tk.LabelFrame(w, labelwidget=label_title)
label_frame.grid(row=0, column=0)
#fill LabelFrame with something to make it visible
bt = tk.Button(label_frame, text="Press")
bt.grid(padx=20, pady=20)
w.mainloop()
Output:
Or like I said before you can use any widget.
An example label with image and a label with text.
import tkinter as tk
from PIL import Image, ImageTk
w = tk.Tk()
w.geometry("400x500")
img_open = Image.open("example_1.png")
img = ImageTk.PhotoImage(img_open)
frame_labelwidget = tk.Frame(w) #important Do Not grid/pack/place it!
label_image = tk.Label(frame_labelwidget, image=img)
label_image.grid(row=0, column=0)
label_text = tk.Label(frame_labelwidget, text="Teamwork")
label_text.grid(row=0, column=1)
label_frame = tk.LabelFrame(w, labelwidget=frame_labelwidget)
label_frame.grid(row=0, column=0)
#fill LabelFrame with something to make it visible
bt = tk.Button(label_frame, text="Press")
bt.grid(padx=20, pady=20)
w.mainloop()
Output:
Of course that makes less sense because you can have text and an image combined in tk.Label by using compound option.
I added it only for demonstration purpose, you can add any widget you want. Instead of label_text you could add an Entry, Canvas...

How can I fix my button positions on python tkinter?

I want to fix the position of my two buttons like if I resize my window the buttons will remain in their positions. Or My buttons dynamically resize with the window size. I have positioned my buttons using place. But I ain't getting success. Here's the code so far.
import turtle
import tkinter as tk
##wn=turtle.Screen()
wn=tk.Tk()
image=tk.PhotoImage(file="MS-3.PNG")
wn.geometry("700x700")
label1=tk.Label(wn,image=image)
label1.pack(side="top",fill="both",expand="yes")
label1.grid_rowconfigure(0, weight=1)
label1.grid_columnconfigure(0, weight=1)
label1.grid_rowconfigure(1, weight=1)
def callback1():
import Detection1
def callback():
import detection
button1=tk.Button(label1,text="Health Identification", command=callback, bd=12,bg="grey",font="caliber")
button1.place(x=100,y=500 )
label1.image=image
button2=tk.Button(label1,text="Disease Classification", command=callback1, bd=10,bg="grey",font="caliber")
button2.place(x=400, y=500 )
label1.image=image
wn.mainloop()
Sadia: If I understood your question correctly, you could make your windows non-resizable with wn.resizable(False, False). Then place your buttons exactly where you want them to be. If you are new to tkinter and python, making every object resizable might be a bit too complex to begin with.
Hopefully this helps.
import turtle
import tkinter as tk
from PIL import Image, ImageTk
##wn=turtle.Screen()
wn = tk.Tk()
wn.geometry("700x700")
wn.resizable(False, False)
img = ImageTk.PhotoImage(Image.open("MS-3.PNG"))
label1 = tk.Label(wn, image=img)
label1.pack(side="top", fill="both", expand="yes")
# label1.grid_rowconfigure(0, weight=1)
# label1.grid_columnconfigure(0, weight=1)
# label1.grid_rowconfigure(1, weight=1)
def callback1():
import Detection1
def callback():
import detection
button1 = tk.Button(label1, text="Health Identification", command=callback, bd=12, bg="grey", font="caliber")
button1.place(x=100, y=500)
button2 = tk.Button(label1, text="Disease Classification", command=callback1, bd=10, bg="grey", font="caliber")
button2.place(x=400, y=500)
wn.mainloop()

button to take frame screenshot

can someone help me take a screnshot of a specific frame?
been playing with these, but cant seem to specify just the frame
import pyautogui
#im1 = pyautogui.screenshot()
#im1.save('my_screenshot.png')
#im2 = pyautogui.screenshot('my_screenshot2.png')
from tkinter import *
import time
from PIL import ImageTk, Image
import pyautogui as pg
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
frm2shoot = Frame(win)
frm2shoot.grid(column=0, row=0)
lbl = Label(frm2shoot, width=16, text="testing testing:", justify=LEFT, anchor="w").grid(row=0, column=0, sticky=W, pady=2)
# Define a function for taking screenshot
def screenshot():
random = int(time.time())
filename = "/Users/ricardosimoes/Desktop/"+ str(random) + ".jpg"
ss = pg.screenshot(filename)
ss.show()
frm2shoot.deiconify()
# Create a Button to take the screenshots
button = Button(win, text="Take Screenshot", font=('Aerial 11 bold'), background="#aa7bb1", foreground="white", command=screenshot)
button.grid(column=5, row=0)
win.mainloop()
Anyone have any idea how to do this??

How to embed a base64 image into python 3 guizero/tkinter?

I am trying to embed a base64 image into either a box or the picture widget so my application requires less files. I found a way to do it with plain tkinter at Base64 string to image in tkinter answered by Bryan Oakley and I updated the code to work with python 3.
What they did
import tkinter as tk
IMAGE_DATA = '''
R0lGODlhEAAQALMAAAAAAP//AP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAA\nAAAAACH5BAEAAAIALAAAAAAQABAAQAQ3UMgpAKC4hm13uJnWgR
TgceZJllw4pd2Xpagq0WfeYrD7\n2i5Yb+aJyVhFHAmnazE/z4tlSq0KIgA7\n
'''
root = tk.Tk()
image = tk.PhotoImage(data=IMAGE_DATA)
label = tk.Label(root, image=image, padx=20, pady=20)
label.pack()
root.mainloop()
What I've tried
import tkinter as tk
from guizero import *
import base64
app= App()
#the same image they used for testing
IMAGE_DATA = '''
R0lGODlhEAAQALMAAAAAAP//AP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAA\nAAAAACH5BAEAAAIALAAAAAAQABAAQAQ3UMgpAKC4hm13uJnWgR
TgceZJllw4pd2Xpagq0WfeYrD7\n2i5Yb+aJyVhFHAmnazE/z4tlSq0KIgA7\n
'''
box=Box(app,width=app.width,height=app.height)
box.bg='gray'
#button=PushButton(app, image=IMAGE_DATA)
#image = Picture(box, image=IMAGE_DATA)
#picText = Text(box, tk.data=IMAGE_DATA)
#Pure tkinter
'''
image = tk.PhotoImage(data=IMAGE_DATA)
label = tk.Label(box, image=image, padx=20, pady=20)
label.pack()
'''
app.display()
What I used
https://lawsie.github.io/guizero/ (module)
https://base64.guru/converter/encode/image (converts images to base 64)
The image option requires an instance of the PhotoImage class from tkinter. So, create an instance, and pass that to guizero widget.
image = tk.PhotoImage(data=IMAGE_DATA)
button=PushButton(app, image=image)

Trouble adding Images into a Gui using tkinter

OK so i am trying to make a program that displays an image when pressing a button, and i am having trouble getting the images into the program
this is my full code:
# Nicolas Bart
import tkinter as tk
from PIL import Image, ImageTk
from tkinter import *
window = tk.Tk()
window.title('Bad Meme Generator')
window.geometry('500x500')
window.configure(bg='saddle brown')
meme_label = tk.Label(window, text='PRESS BUTTON FOR BAD MEMES:',
fg='blue4', bg='brown4', font=('comicsans', '20'))
meme_label.grid(pady=25, padx=25, column=0, row=0)
def button_command():
meme_window = tk.Tk()
meme_window.title('I Warned You')
meme_window.grid()
image = Image.open('pexels-photo-247932.jpg')
photo = ImageTk.PhotoImage(image)
label = tk.Label(meme_window, image=photo)
label.image = photo
label.place(x = 0, y = 0)
button = tk.Button(window, text='Dont Do It!', command=button_command,
padx=100, pady=75, font=('comicsans', '20'),
bg='brown4', fg='blue4')
button.grid(column=0, row=1)
warning_label = tk.Label(window, text="Really shit tier memes incoming:",
bg='brown4', fg='blue4',
font=('comicsans', '20'))
warning_label.grid(pady=75)
window.mainloop()
every time i run this program, when i press the button to open the image, it gives the error "AttributeError: type object 'Image' has no attribute 'open'"
the specific part of the program that is giving the error is:
def button_command():
meme_window = tk.Tk()
meme_window.title('I Warned You')
meme_window.grid()
image = Image.open('pexels-photo-247932.jpg')
photo = ImageTk.PhotoImage(image)
label = tk.Label(meme_window, image=photo)
label.image = photo
label.place(x = 0, y = 0)
any help would be appreciated. Thank you :)
This is a good example of why you shouldn't do from tkinter import *. Tkinter has an Image class, so by doing this import after importing Image from PIL you overwrite the PIL class with the tkinter class.
Since you're already importing tkinter the preferred way (import tkinter as tk), you don't need to import tkinter a second time. You need to remove the statement from tkinter import *.
You also make the mistake of creating more than one instance of Tk. I don't know if it contributes to the problem or not, but it's not something you should be doing. If you need additional windows then you should create instances of Toplevel.

Resources