Tkinter - Images is not display - python

I am new on Tkinter. I was trying to display two images on my canvas but I couldn't. I tried to achieve this by creating two different files. One will contain all logic behind and the other one will handle the gui. Here is my code so far:
file1.py
from file2 import *
import tkinter as tk
import random
# global variables
w = 'initial'
class start_gui(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self,parent, *args, **kwargs)
# create canvas
self.canvas = tk.Canvas(parent, width=800, height=800, background="green")
self.canvas.pack()
c = Display(self.canvas)
c.current_play(w)
if __name__ == "__main__":
# create main window
root = tk.Tk()
root.geometry("800x800")
start_gui(root)
root.mainloop()
file2.py
import tkinter as tk
from functools import partial
from PIL import ImageTk
from PIL import Image
class Display:
def __init__(self, canv):
self.canvas = canv
def current_play(self, option):
if (option == 'initial'):
self.initial_display()
elif (option == 'n' or option == 's'):
self.ns_display()
def initial_display(self):
# display cat image
self.im = Image.open("cat.gif")
self.photo_image = ImageTk.PhotoImage(self.im)
self.demo = self.canvas.create_image(400, 400, image=self.photo_image, anchor='center')
self.canvas.create_rectangle(50, 25, 150, 75, fill="blue")
self.temp_image = tk.PhotoImage(file="cat.gif")
self.demo2 = self. canvas.create_image(600, 600, image = self.temp_image, anchor='center')
The problem here is that the two image items I created do not show up on the canvas but only the rectangle. Can someone help me with this?
PS: I am using python v 3.4

Another solution: We can make Display inherit from class tk.Canvas
import tkinter as tk
from PIL import ImageTk
from PIL import Image
import random
# global variables
w = 'initial'
class Display(tk.Canvas):
def __init__(self, parent, *args, **kwargs):
tk.Canvas.__init__(self, parent, *args, **kwargs)
def current_play(self, option):
if option == 'initial':
self.initial_display()
elif option == 'n' or option == 's':
self.ns_display()
def initial_display(self):
# display cat image
self.im = Image.open("cat.gif")
self.photo_image = ImageTk.PhotoImage(self.im)
self.demo = self.create_image(400, 400, image=self.photo_image, anchor='center')
self.create_rectangle(50, 25, 150, 75, fill="blue")
self.temp_image = tk.PhotoImage(file="cat.gif")
self.demo2 = self.create_image(600, 600, image = self.temp_image, anchor='center')
class start_gui(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self,parent, *args, **kwargs)
# create canvas
self.canvas = Display(parent, width=800, height=800, background="green")
self.canvas.pack()
self.canvas.current_play(w)
if __name__ == "__main__":
root = tk.Tk()
root.geometry("800x800")
start_gui(root)
root.mainloop()

The problem is one of garbage collection. Your Display object is stored in a local variable inside start_gui.__init__. Once start_gui is constructed, this object is thrown away. The image is an attribute of that object, so it gets garbage-collected. When an image object gets garbage-collected, tkinter is unable to display it.
The simple solution is to keep a permanent reference to Display:
self.display = Display(canvas)
self.display.current_play(w)

Related

How do I show random images from a folder on Tkinter?

So I have a couple of images in a folder and I want to do a little pack opener on tkinter where if I press on a button it randomly opens an Image of that folder and shows it. So I did this:
import os
import random
from PIL import Image
from tkinter import *
def pack():
path ='C:\\Users\\matt\OneDrive\Images\cards'
files = os.listdir(path)
index = random.randrange(0, len(files))
image = Image.open(files[index])
image.show()
pack_button = Button(window,text = " Pack ",fg="white",bg = 'black',command = pack)
pack_button.grid(row = 2,column = 1,padx = 10,pady = 5)
window.mainloop()
The problem is that this function doesn't want to work and it always tells me:
AttributeError: type object 'Image' has no attribute 'open'
Can someone please help me? And does someone know how to do a button out of an image?
Thank you in advance.☺
Assuming you're using Python 3, and you have a folder named card images in the same directory as your Python script, and that folder contains .png images of your playing cards, then the following should work:
import tkinter as tk
def get_random_image_path():
from random import choice
from pathlib import Path
return str(choice(list(Path("card images").glob("*.png"))))
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title("Random card")
self.geometry("171x239")
self.resizable(width=False, height=False)
self.image = tk.PhotoImage(file=get_random_image_path())
self.button = tk.Button(self, image=self.image, command=self.assign_new_image)
self.button.pack()
def assign_new_image(self):
self.image = tk.PhotoImage(file=get_random_image_path())
self.button.configure(image=self.image)
def main():
application = Application()
application.mainloop()
return 0
if __name__ == "__main__":
import sys
sys.exit(main())
Note: I just grabbed three public domain card images from Wikimedia Commons. You can download them either in .svg or .png format in different resolutions. In my case I downloaded them as .pngs with a resolution of 171x239, which is why I picked the same dimensions for the tkinter window. Since I've only downloaded three images, sometimes it appears as though clicking the button doesn't seem to do anything, which isn't the case - it's just that, with only three images to choose from, we're likely to pick the same image multiple times in a row.
To get your example running, see a minimal solution below.
For a ready Tkinter program and to avoid PIL use Paul M.'s solution.
import os
import random
from PIL import Image
# assuming that this dir contains only images
# otherwise test if r is an image
img_folder = r'/home/ktw/Desktop/test_img'
def pack():
r = random.choice(os.listdir(img_folder))
image = Image.open(os.path.join(img_folder, r))
image.show()
if __name__=='__main__':
pack()
EDIT:
This should work for you. Just change the path to the full path of your image folder.
choice(os.listdir(img_folder)) gives you only the name of the random file. os.path.join(img_folder, choice(os.listdir(img_folder))) builds an absolute path from the image folder and the random image so Tk.PhotoImage should work.
import tkinter as tk
import os
from random import choice
from pathlib import Path
img_folder = r'/home/ktw/Desktop/images'
def get_random_image_path():
return os.path.join(img_folder, choice(os.listdir(img_folder)))
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title("Random card")
self.geometry("171x239")
self.resizable(width=False, height=False)
self.image = tk.PhotoImage(file=get_random_image_path())
self.button = tk.Button(self, image=self.image, command=self.assign_new_image)
self.button.pack()
def assign_new_image(self):
self.image = tk.PhotoImage(file=get_random_image_path())
self.button.configure(image=self.image)
if __name__=='__main__':
application = Application()
application.mainloop()
import tkinter as tk
import os
from random import choice
from pathlib import Path
from PIL import Image
img_folder = r'C:\\Users\\matt\OneDrive\Images\cards'
def get_random_image_path():
return os.path.join(img_folder, choice(os.listdir(img_folder)))
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title("Random card")
self.resizable(width=False, height=False)
self.image = tk.PhotoImage(get_random_image_path())
self.button = tk.Button(self, image=self.image, command=self.assign_new_image)
self.button.pack()
def assign_new_image(self):
self.image = tk.PhotoImage(file=get_random_image_path())
self.button.configure(image=self.image)
if __name__=='__main__':
application = Application()
application.mainloop()
and it says this:
File "C:\Users\matt\OneDrive\Images\cards\pack opener.py", line 31, in <module>
application = Application()
File "C:\Users\matt\OneDrive\Images\cards\pack opener.py", line 22, in __init__
self.button = tk.Button(self, image=self.image, command=self.assign_new_image)
File "C:\Users\matt\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2679, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Users\matt\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2601, in __init__
self.tk.call(
_tkinter.TclError: image "C:\\Users\\matt\OneDrive\Images\cards\Vettel.png" doesn't exist

How to print results iteratively on a canvas or text widget within a GUI made in Python?

I have made a simple GUI that has two buttons for a Square function and Exit button. I want to print the results on the canvas as shown in the expected results.
Here are my codes
from tkinter import *
from PIL import Image,ImageTk
from tkinter import filedialog
from PIL import Image, ImageTk
import os
import cv2
import numpy as np
import time
class application(Tk):
def __init__(self,parent):
Tk.__init__(self,parent)
self.parent = parent
self.minsize(width=300,height=500)
self.initialize()
def initialize(self):
self.grid_columnconfigure(0,weight=1)
self.grid_columnconfigure(1,weight=1)
self.grid_columnconfigure(2,weight=1)
#Button widgets
###########################################################################################
self.button1 = Button(self,text='Square',bg= 'blue',width=15,height=2, command =Square)
self.button1.grid(column=0,row=1,sticky='W',pady=5)
self.button3 = Button(self,text='Exit',bg= 'blue',width=10,height=2,command=self.destroy)
self.button3.grid(column=1,row=5,sticky='W',pady=5)
#Text widget for inserting the result
############################################################################################
self.canvas = Canvas(self,width=230,height=180,state=NORMAL)
self.canvas.grid(column=0,row=4,sticky='W',padx=100,pady=10)
#self.canvas.configure(bg='green')
#Label widget
############################################################################################
self.label3 = Label(self,text="Square",bg = 'red',width=10,height=2,anchor="center")
self.label3.grid(column=0,row=3,sticky='W',padx=120)
def Square(self):
for i in range(1,10):
y = i**2
print(i, y)
if __name__ == "__main__":
app = application(None)
#font.nametofont('TkDefaultFont').configure(size=10)
app['bg']='red'
app.title("square")
app.mainloop()
My expected results are given in the image below
Not entirely sure what you are asking. But, if you just want to create text on canvas use canvas.create_text(x, y, text).
from tkinter import *
from PIL import Image,ImageTk
class application(Tk):
def __init__(self,parent):
Tk.__init__(self,parent)
self.parent = parent
self.minsize(width=300,height=500)
self.initialize()
def initialize(self):
self.grid_columnconfigure(0,weight=1)
self.grid_columnconfigure(1,weight=1)
self.grid_columnconfigure(2,weight=1)
#Button widgets
###########################################################################################
self.button1 = Button(self,text='Square',bg= 'blue',width=15,height=2, command=self.Square)
self.button1.grid(column=0,row=1,sticky='W',pady=5)
self.button3 = Button(self,text='Exit',bg= 'blue',width=10,height=2,command=self.destroy)
self.button3.grid(column=1,row=5,sticky='W',pady=5)
#Text widget for inserting the result
############################################################################################
self.canvas = Canvas(self,width=230,height=180,state=NORMAL)
self.canvas.grid(column=0,row=4,sticky='W',padx=100,pady=10)
#self.canvas.configure(bg='green')
self.label3 = Label(self,text="Square",bg = 'red',width=10,height=2,anchor="center")
self.label3.grid(column=0,row=3,sticky='W',padx=120)
def Square(self):
for i in range(1,10):
y = i**2
print(i, y)
c_id = self.canvas.create_text(0, 20, text=f"{i}\t{y}", fill="blue", font="Times 14")
bbox = self.canvas.bbox(c_id)
self.canvas.coords(c_id, 100, bbox[3]*i)
if __name__ == "__main__":
app = application(None)
#font.nametofont('TkDefaultFont').configure(size=10)
app['bg']='red'
app.title("square")
app.mainloop()

How to display image with upload button in and resize it with window resize (Dynamically)with Tkinter python, I need to add upload button

I need to add upload button so that I can upload the picture and display with this class. Everything working but when I am adding the upload button its giving me some error and my code is not wokring.
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog
class Layout:
def __init__(self,master):
self.master = master
self.rootgeometry()
self.canvas = tk.Canvas(self.master)
self.canvas.pack()
self.background_image = Image.open(self.openfn())
self.image_copy = self.background_image.copy()
self.background = ImageTk.PhotoImage(self.background_image)
self.loadbackground()
def loadbackground(self):
self.label = tk.Label(self.canvas, image = self.background)
self.label.bind('<Configure>',self.resizeimage)
self.label.pack(fill='both', expand='yes')
def openfn(self):
filename = filedialog.askopenfilename(title='open')
return filename
def rootgeometry(self):
x=int(self.master.winfo_screenwidth()*0.7)
y=int(self.master.winfo_screenheight()*0.7)
z = str(x) +'x'+str(y)
self.master.geometry(z)
def resizeimage(self,event):
image = self.image_copy.resize((self.master.winfo_width(),self.master.winfo_height()))
self.image1 = ImageTk.PhotoImage(image)
self.label.config(image = self.image1)
root = tk.Tk()
a = Layout(root)
root.mainloop()
Create the Button widget within the class constructor and bind it with self.loadbackground. Also, you don't need to recreate the Label widget every time instead use label.configure(image=yourimage).
Here is the code:
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog
class Layout:
def __init__(self,master):
self.master = master
self.rootgeometry()
self.button = Button(self.master, text='Upload', command=self.loadbackground)
self.button.pack()
self.canvas = tk.Canvas(self.master)
self.canvas.pack(fill=BOTH, expand=True)
self.background_image = None
self.image_copy = None
self.background = None
self.label = tk.Label(self.canvas)
self.label.pack(fill='both', expand=True)
def loadbackground(self):
self.background_image = Image.open(self.openfn())
self.image_copy = self.background_image.copy()
self.background = ImageTk.PhotoImage(self.background_image.resize((self.canvas.winfo_width(), self.canvas.winfo_height())))
self.label.configure(image=self.background)
self.label.bind('<Configure>',self.resizeimage)
def openfn(self):
filename = filedialog.askopenfilename(title='open')
return filename
def rootgeometry(self):
x=int(self.master.winfo_screenwidth()*0.7)
y=int(self.master.winfo_screenheight()*0.7)
z = str(x) +'x'+str(y)
self.master.geometry(z)
def resizeimage(self,event):
image = self.image_copy.resize((event.width, event.height))
self.image1 = ImageTk.PhotoImage(image)
self.label.config(image = self.image1)
root = tk.Tk()
a = Layout(root)
root.mainloop()

'RawTurtle' object has no attribute 'Turtle'

I saw the sorting example in turtle demo which python includes and I would like to add similar animations in my program. My program is based in tkinter and I would like to insert turtle animations in a tkinter canvas (with RawTurtle) so first I tried to create a black box in the canvas and I get the following error message:
AttributeError: 'RawTurtle' object has no attribute 'Turtle'
Here's my code:
import tkinter
from turtle import *
class MyApp():
def __init__(self, parent):
self.p = parent
self.f = tkinter.Frame(self.p).pack()
self.c = tkinter.Canvas(self.f, height = '640', width = '1000')
self.c.pack()
self.t = RawTurtle(self.c)
self.main(5)
def main(self, size):
self.t.size = size
self.t.Turtle.__init__(self, shape="square", visible=False)
self.t.pu()
self.t.shapesize(5, 1.5, 2)
self.t.fillcolor('black')
self.t.st()
if __name__ == '__main__':
root= tkinter.Tk()
frame = MyApp(root)
root.mainloop()
You pretty much have it -- those two settings you were trying to change via the non-existent Turtle() instance method can be handled when creating the RawTurtle:
import tkinter
from turtle import RawTurtle
class MyApp():
def __init__(self, parent):
self.p = parent
self.f = tkinter.Frame(self.p).pack()
self.c = tkinter.Canvas(self.f, height=640, width=1000)
self.c.pack()
self.t = RawTurtle(self.c, shape='square', visible=False)
self.main(5)
def main(self, size):
self.t.size = size # does nothing if stamping with pen up
self.t.penup()
self.t.shapesize(5, 1.5, 2)
self.t.fillcolor('black') # the default
self.t.stamp()
if __name__ == '__main__':
root = tkinter.Tk()
frame = MyApp(root)
root.mainloop()

Why tkinter canvas doesn't update when used in a class

I am writing a small program, where I want to draw something on canvas. This code works for me;
import tkinter as tk
from PIL import Image, ImageTk
from l_systems import Lindenmayer
if __name__ == "__main__":
root = tk.Tk()
root.title("Draw Shapes with L-Equations")
cv = tk.Canvas(width=600, height=600, bg='white')
cv.pack()
image1 = Image.new("RGB", (600, 600), (255,255,255))
koch = Lindenmayer(image1)
koch.init(
iterations = 6,
angle = 25,
axiom = "---X",
rules = {"X":"2F-[1[X]+3X]4+F[3+FX]-X", "F":"FF"},
constants = "X") # This creates a drawing on PIL image
# Canvas.create_image expects a PhotoImage
photo = ImageTk.PhotoImage(image1)
cv.create_image((300,300), image=photo)
root.mainloop()
However, I want to organize my tkinter application as a class, therefore I have tried this code,
class main(tk.Frame):
w = 600
h = 600
def __init__(self,parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.cv = tk.Canvas(width=self.w, height=self.h, bg='white')
self.cv.pack()
self.render_image()
def render_image(self):
image1 = Image.new("RGB", (self.w, self.h), (255,255,255))
koch = Lindenmayer(image1)
koch.init(
iterations = 6,
angle = 25,
axiom = "---X",
rules = {"X":"2F-[1[X]+3X]4+F[3+FX]-X", "F":"FF"},
constants = "X"
)
photo = ImageTk.PhotoImage(image1)
self.cv.create_image((self.w/2,self.h/2), image=photo)
if __name__ == "__main__":
root = tk.Tk()
root.title("Draw Shapes with L-Equations")
app = main(root).pack()
root.mainloop()
In this second case, I don't see any drawing on canvas. It is just a white background. How can I fix this?
PhotoImage can have problem in classes and functions. Garbage collector can remove it from memory.
EDIT:
I could check this (because I have to Lindenmayer module)
but your class could look this:
Almost everything is in class.
Class names should normally use the CapWords convention. - see PEP 8 -- Style Guide for Python Code. Event SO use that rule to recognize classes in code and use light blue color.
import tkinter as tk
from PIL import Image, ImageTk
from l_systems import Lindenmayer
class Main(tk.Frame):
def __init__(self,parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.w = 600
self.h = 600
self.parent = parent
self.parent.title("Draw Shapes with L-Equations")
self.cv = tk.Canvas(width=self.w, height=self.h, bg='white')
self.cv.pack()
self.render_image()
self.parent.pack()
def render_image(self):
image1 = Image.new("RGB", (self.w, self.h), (255,255,255))
koch = Lindenmayer(image1)
koch.init(
iterations = 6,
angle = 25,
axiom = "---X",
rules = {"X":"2F-[1[X]+3X]4+F[3+FX]-X", "F":"FF"},
constants = "X"
)
self.photo = ImageTk.PhotoImage(image1)
self.cv.create_image((self.w/2,self.h/2), image=self.photo)
def run(self):
self.parent.mainloop()
if __name__ == "__main__":
Main(tk.Tk()).run()

Categories

Resources