1

probalby those are two separate issues, but i can not find help for any of them

I am doing an basic interface with TKINTER that work both on PC and raspberry with LCD screen, plenty of lines to raise this. Interface is basic, just a image with some bottons on top, buttons are made with shapes (i.e. create rectangle)

issue come first that background image is only displayed briefly and the disapear, but not on debuggin mode (Visual Studio code and F5 step by step), and second, buttons do not capture click of mouse if I do not add button backgroud, if added border (as bellow example), only get hitted on the border.

codesnipet:

import tkinter as tk

...

self.canvas = tk.Canvas(self, bg=self.backgroundColor, bd=0, highlightthickness=0, relief='ridge')
self.canvas.config(cursor="circle")
self.canvas.pack(fill=tk.BOTH, expand=1)


self.canvas.bind("<Key>", event_keypress)
    for i_v in range(2):
        for i_h in range (5):
            tagName = "pad_button_" + str(i_v * 5 + i_h)
            self.canvas.tag_bind(tagName,"<Button-1>", lambda event, arg=tagName: event_callback(event, arg) )


self.canvas.create_image(10,10, image = myImg, anchor = "nw")

sleep(1)

keysize = 10
buttonsWidth = 2
colorOutline = 'red'
color = ''  # <- 

for i_v in range (0,2):
    for i_h in range (0,5):
        
        v_position = i_v * (keysize)
        h_position = i_h * (keysize)

        tagName = "pad_button_" + str(i_v * 5 + i_h)
        
        self.canvas.create_rectangle( h_position 
                                    , v_position 
                                    , h_position + keysize 
                                    , v_position + keysize 
                                    , width=buttonsWidth
                                    , fill=color
                                    , outline=colorOutline
                                    , tags=["pad_button",tagName] )

self.canvas.update()

I know I am not very good, I am learning, and I got stuck with that for several days, looking help haven't been able to come forward

I tested the "in_function_scope", does not seem to be the thing as it disvanice also in:

myImg = ...

img1=tk_canvas.create_image(4,4, image = myImg, anchor = "nw")

tk_canvas.pack()
tk_canvas.update()

time.sleep(0.3)

myImg = ...
img2=tk_canvas.create_image(40,40, image = myImg, anchor = "nw")

tk_canvas.pack()
tk_canvas.update()

(not sure what I am doing with the pack as it does not have any effect)

ceduzy
  • 11
  • 2
  • 3
    You will need to provide a [mre] for your problems, not a few snippets of improperly indented code. – martineau Mar 07 '22 at 10:02
  • For your first issue, it is the same as this [question](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function). – acw1668 Mar 07 '22 at 10:08
  • @acw1668 , this seems not to be the case (probably I am worg), it does neither keep the first picture even if i paint both one after another outside , I added new few lines – ceduzy Mar 07 '22 at 12:45
  • 1
    Did you try changing `myImg` to `self.myImg`? – acw1668 Mar 07 '22 at 13:48
  • @acw1668 I think I discovered why, and it is surprising, may it be becouse I reuse the miImg variable?, so as soon as it get reasigned with the new image, the previous one on the cavas, get etrased. May it be this ? – ceduzy Mar 08 '22 at 19:06
  • 1
    Yes using same variable on two images, the first one will be garbage collected. – acw1668 Mar 09 '22 at 07:28

0 Answers0