tkinter label not always updating

Viewed 22

Without the: mainWindow.mainloop() at line 112 the program only shows the first slide

Without the: slideSourceImage = cv2.cvtColor(cv2.imread(imageList[j]), cv2.COLOR_BGR2RGB) slideSourceImage = cv2.resize(slideSourceImage, (WIDTH, HEIGHT), interpolation = cv2.INTER_NEAREST) slideImage = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(slideSourceImage)) mainLabel.imgtk = slideImage mainLabel.configure(image=slideImage) starting at line 156 Only the Blackground is shown

The two appear to be related

#   project:
#   file: tkinterQuestion1.py
#   this build started 13 apr 2022
#   last edit: 19 apr 2022

#************************************ notes ***********************************
#
#
#
#******************************************************************************

#************************************ to do ***********************************
#   
# 
#
#******************************************************************************

import tkinter as tk
import cv2
import PIL.Image, PIL.ImageTk
import time


WIDTH = 320
HEIGHT = 480
PERIOD = 6000


imageList = []
j = 0

#################### call back functions ####################



######################### functions #########################

def slideShow():
    global j
    # event = 0
    global mainWindow
    global mainCanvas
    global mainCanvasImage

 
    print('Now showing: ', imageList[j], '\n')

    slideSourceImage = cv2.cvtColor(cv2.imread(imageList[j]), cv2.COLOR_BGR2RGB)


    height, width, no_channels = slideSourceImage.shape

    # if the size of the image is not the display size scaling will be needed
    scale = False
    if (width != WIDTH):
        scale = True
    if (height != HEIGHT):
        scale = True
    print('Width: ', width) # testing only
    print('Height: ', height) # testing only
 
    # check ratio
    originalRatio = width/height
    print('originalRatio = ' + str(originalRatio)) # testing only
    displayRatio = WIDTH/HEIGHT
    print('displayRatio = ' + str(displayRatio)) # testing only
    
    if scale == True:
        print('Need to scale')
        # if the ratio is close scale it an allow slight distortion
        ratioDifference = abs(originalRatio - displayRatio)

        print('Ratio difference', ratioDifference)

        
        if ratioDifference <= 0.1:
            print('Ratios match or are close enough')
            newW = WIDTH
            newH = HEIGHT
        else: # ratios are not close enough      
            print('ratios are not close')
        
            if(originalRatio < displayRatio):
                print('Scale width, use height of ', HEIGHT) # testing only
                newW = int (width * (HEIGHT / height))
                newH = HEIGHT
                print('New width = ' + str(newW)) # testing only
 
            else:
                print('Scale height, use width of ', WIDTH) # testing only
                newW = WIDTH
                newH = int (height * (WIDTH / width))
                print('New height = ' + str(newH)) # testing only

        # scale image
        slideSourceImage = cv2.resize(slideSourceImage, (newW, newH), interpolation = cv2.INTER_NEAREST)
    else: 
        print('Do not need to scale')
        newW = WIDTH
        newH = HEIGHT
        

    slideImage = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(slideSourceImage))

    mainLabel.configure(image=slideImage)
    
    j = j + 1
    if j ==10:
        j = 0
        
    mainWindow.after(PERIOD, slideShow)
    # mainWindow.mainloop()
    
def slideTouch(event):
    print('clicked')
    mainWindow.destroy()

###################### main operation #######################

print('Starting tkinterQuestion1')

global mainWindow
global mainCanvas
global mainCanvasImage

for i in range(6):
    fileName  = 'MediaInUse/Slide' + str(i + 1) + '.jpg'
    print('File Name = ' + fileName)
    imageList.append(fileName)

imageList.append('MediaInUse/tall1.jpg')
imageList.append('MediaInUse/tall2.jpg')
imageList.append('MediaInUse/wide1.jpg')
imageList.append('MediaInUse/wide2.jpg')

    
print('Image List \n' + str(imageList))

mainWindow = tk.Tk()
mainWindow.geometry(str(WIDTH) + "x" + str(HEIGHT))
mainWindow.resizable(width=False, height=False)
mainWindow.overrideredirect(True)

mainCanvas = tk.Canvas(mainWindow, width = WIDTH, height = HEIGHT, bg='Black')
mainCanvas.pack()
mainCanvas.bind('<Button-1>', slideTouch)

mainFrame = tk.Frame(mainCanvas, bg='Black')
mainFrame.place(relx=0, rely=0, relwidth=1.0, relheight=1.0)
mainFrame.bind('<Button-1>', slideTouch)

mainLabel = tk.Label(mainFrame, bg='Gray10')
mainLabel.place(relx=0, rely=0, relwidth=1.0, relheight=1.0)
mainLabel.bind('<Button-1>', slideTouch)

slideSourceImage = cv2.cvtColor(cv2.imread(imageList[j]), cv2.COLOR_BGR2RGB)
slideSourceImage = cv2.resize(slideSourceImage, (WIDTH, HEIGHT), interpolation = cv2.INTER_NEAREST)
slideImage = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(slideSourceImage))
mainLabel.imgtk = slideImage
mainLabel.configure(image=slideImage)

j = j + 6

mainWindow.after(PERIOD, slideShow)
mainWindow.mainloop()

print('End tkinterQuestion1')
0 Answers
Related