Feb-15-2023, 10:16 AM
(This post was last modified: Feb-15-2023, 11:40 AM by Gribouillis.)
I am getting this tcl error and unable to solve it. Here is the full exception. This happens when I exit the program
Error:Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\INDIAN\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\INDIAN\Desktop\python exercises\pygametooth\untiltledtry 15-2-23.py", line 65, in select_folder
canvas.delete("all")
File "C:\Users\INDIAN\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 2818, in delete
self.tk.call((self._w, 'delete') + args)
_tkinter.TclError: invalid command name ".!canvas"
Traceback (most recent call last):
File "C:\Users\INDIAN\Desktop\python exercises\pygametooth\untiltledtry 15-2-23.py", line 135, in <module>
canvas = Canvas(root, width=800, height=500, bg="white")
File "C:\Users\INDIAN\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 2683, in __init__
Widget.__init__(self, master, 'canvas', cnf, kw)
File "C:\Users\INDIAN\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 2567, in __init__
self.tk.call(
_tkinter.TclError: can't invoke "canvas" command: application has been destroyed
>>> import tkinter as tk
from tkinter import Canvas, NW
from tkinter import filedialog
import os
import pygame
import cv2
import numpy as np
from PIL import Image, ImageTk
image_tk = None # Placeholder variable for PhotoImage
# Initialize Pygame
pygame.init()
def select_folder():
# Open a file dialog to select the folder
root.filename = filedialog.askdirectory()
# Load the images from the selected folder
images = []
for f in os.listdir(root.filename):
if f.endswith((".png", ".jpg", ".bmp", ".dcm")):
image = cv2.imread(os.path.join(root.filename, f))
if image is not None:
images.append(image)
# Set the initial image
current_image = 0
# Main loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_image = (current_image - 1 + len(images)) % len(images)
elif event.key == pygame.K_RIGHT:
current_image = (current_image + 1) % len(images)
# Clear the canvas
canvas.delete("all")
# Get the dimensions of the current image
height, width = images[current_image].shape[:2]
# Set the rotation angle
angle = 90
# Calculate the center of the image
center = (width // 2, height // 2)
# Create the rotation matrix
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
# Calculate the new dimensions of the rotated image
new_width, new_height = height, width
# Rotate the image
rotated = cv2.warpAffine(images[current_image], rotation_matrix, (new_width, new_height))
# Convert the BGR image to RGB
rgb_image = cv2.cvtColor(rotated, cv2.COLOR_BGR2RGB)
# Convert the OpenCV image to a PIL ImageTk
pil_image = Image.fromarray(np.rot90(rgb_image, 3))
## pil_image = Image.fromarray(rgb_image)
image_tk = ImageTk.PhotoImage(image=pil_image)
# Display the image in the tkinter window
canvas.create_image(0, 0, image=image_tk, anchor=NW)
canvas.image = image_tk
# Update the screen
root.update()
def exit_program():
root.destroy()
def on_key_press(event):
if event.keysym == "Left":
pygame.event.post(pygame.event.Event(pygame.KEYDOWN, {"key": pygame.K_LEFT}))
elif event.keysym == "Right":
pygame.event.post(pygame.event.Event(pygame.KEYDOWN, {"key": pygame.K_RIGHT}))
# Create the GUI
##root = tk.Tk()
root.title("Multi-angle Image Viewer")
root.geometry("800x600")
# Add a button to select the folder
button = tk.Button(root, text="Select Folder", command=select_folder)
button1 = tk.Button(root, text="Exit", command=exit_program)
button.pack()
button1.pack()
# Create the canvas to display the images
canvas = Canvas(root, width=800, height=500, bg="white")
canvas.pack()
# Bind the arrow keys to the canvas
canvas.bind("<Key>", on_key_press)
canvas.focus_set()
# Run the GUI
# Run the GUI
root.mainloop()
### Create the canvas to display the images
canvas = Canvas(root, width=800, height=500, bg="white")
canvas.pack()
# Run the GUI
root.mainloop()
