0

I try to build a short of captha app that must run on desktop and emulated on Android, so far I have success with the example "simple calculator" that use TK and Buttons.

I success to load the first row of pictures inside the buttons, but as soon as I try to update the images, buttons disapear

here my code (resumed for two buttons, but same the same)

#!/usr/bin/env python3

# Python program to create a simple GUI 
# calculator using Tkinter 

from tkinter import *
import os
from PIL import ImageTk,Image, ImageFilter, ImageFont , ImageDraw , ImageOps, ImageFile


# checkea que el script corre en display0, si no, configura el output grafico a este
if os.environ.get('DISPLAY','') == '':
    os.environ.__setitem__('DISPLAY', ':0.0')


# globally declare the ThisInstanceImages variable 
ThisInstanceImages = []


# Function to update each button is pressed
def press(num): 
    reloadImages()


def reloadImages():
    global ThisInstanceImages
    ...
    aux_IMG = actualImage.crop((10,10,110,110)).resize((250,250))

    if len(ThisInstanceImages) == 0:
        ThisInstanceImages.append(ImageTk.PhotoImage(aux_IMG))
    else:
        ThisInstanceImages[0] = ImageTk.PhotoImage(aux_IMG)

    aux_IMG = actualImage.crop((110,110,210,210)).resize((250,250))

    if len(ThisInstanceImages) == 1:
        ThisInstanceImages.append(ImageTk.PhotoImage(aux_IMG))
    else:
        ThisInstanceImages[1] = ImageTk.PhotoImage(aux_IMG)





# Driver code 
if __name__ == "__main__": 
    # create a GUI window 
    gui = Tk() 

    # set the background colour of GUI window 
    gui.configure(background="light green") 

    # set the title of GUI window 
    gui.title("Simple Calculator") 

    # set the configuration of GUI window 
    gui.geometry("640x480") 


    ## fisrt run - aux
    reloadImages()


    # create a Buttons and place at a particular 
    # location inside the root window . 
    # when user press the button, the command or 
    # function affiliated to that button is executed . 
    button1 = Button(gui, text='', image = ThisInstanceImages[0], fg='black', bg='black', 
                    command=lambda: press(1), height=250, width=250, borderwidth=0) 
    button1.grid(row=2, column=0) 

    button2 = Button(gui, text=' 2 ', image = ThisInstanceImages[1], fg='black', bg='black', 
                    command=lambda: press(2), height=250, width=250, borderwidth=0) 
    button2.grid(row=2, column=1) 


    ...



    # start the GUI 
    gui.mainloop() 

the first call [on ## fisrt run - aux] works ok, but as soon as button is pressed, and jump for second time on reloadImages() and hit the seventh line (ThisInstanceImages[0] = ImageTk.PhotoImage(aux_IMG)) app buttons disapear. I try with a little trick to do not delete any image, (really change), same result

here the trick:

def reloadImages():
    global ThisInstanceImages
    ...
    aux_IMG = actualImage.crop((10,10,110,110)).resize((250,250))
    ThisInstanceImages.append(ImageTk.PhotoImage(aux_IMG))

    aux_IMG = actualImage.crop((110,110,210,210)).resize((250,250))
    ThisInstanceImages.append(ImageTk.PhotoImage(aux_IMG))


    if len(ThisInstanceImages) > 2:
        ThisInstanceImages[0] = ThisInstanceImages[2]

is that achivable ??

raghad1
  • 1
  • 1
  • Are you sure the app is frozen, or is it just that the images no longer appear? – Bryan Oakley Jun 23 '22 at 18:13
  • I thought got frozen, but not, indeed buttons disapear. I try with a trick, adding the two new images to the array, and then , os second round, copy the 3rd into the 1st. Then buttons with the first image disapear. Not sure what have to do with garbage colector (al point in https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) becouse I work with a global variable – raghad1 Jun 23 '22 at 18:24

0 Answers0