0

Writing my first TKInter application that was given to me by a designer. Lots of images and event actions.

TKInter is pretty slick and I like it a lot! But certain things are giving me fits, and it feels like I am missing something going on in the background, or that Windows+TKInter is finicky

As an example, if I have a basic file like app.py:

app = MyApp()

app.mainloop()

and another file controller.py

class MyApp(tk.Tk):
  """Application root window and Controller for all event handling """

  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)

    bg = Image.open("./images/my_bg.png")
    bg = bg.resize((1024,600))
    bg_img = ImageTk.PhotoImage(bg)
    bg_label = ttk.Label(self, image=bg_img, background='black')
    bg_label.place(x=0, y=0)

The image is not rendered. Even if I move it to its own function and invoke that after MyApp is instantiated, I get nothing.

Things that I store in MyApp, like StringVars, work just fine and I can update those from MyApp based on an event and it's reflected in the GUI. But I've figured out that images and the Canvas/plots will not update.

I got by with jamming all UI generation in the main file, but I need to update the plot on the fly based on user input and it's just not happening.

This also fails to render the background image:

app.py:

app = MyApp()

app.setBgImage()

app.mainloop()

controller.py

class MyApp(tk.Tk):
  """Application root window and Controller for all event handling """

  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)


  def setBgImage(self):
    bg = Image.open("./images/my_bg.png")
    bg = bg.resize((1024,600))
    bg_img = ImageTk.PhotoImage(bg)
    bg_label = ttk.Label(self, image=bg_img, background='black')
    bg_label.place(x=0, y=0)

Anyone with experience on what's going on behind the scenes with TKInter to tell me what's going on?

My next step is to try it on a Linux OS and see if maybe Windows+TKInter has issues.

Any suggestions are much appreciated.

JGOLD
  • 1
  • FYI: I know this question was marked as a duplicate, but I don' t think the problem described in the linked answer is related to the problem you're having here. You might want to let the mods know. – JRiggles Dec 15 '22 at 20:21

1 Answers1

0

Try using pack instead of place for the label that contains the photo, then set it to fill the available space

bg_label.pack(expand=True, fill=tk.BOTH)  # 'BOTH' means fill both 'x' and 'y' axes
JRiggles
  • 4,847
  • 1
  • 12
  • 27
  • Thanks @JRiggles. I am currently testing your suggestion, though the artwork on my application has to be placed in a certain position. I'm also trying to add _everything_ as a property of the tk.TK app to see if they are preserved. – JGOLD Dec 16 '22 at 17:17
  • I suppose you could `pack()` the images into a parent `Frame` widget, and then `place` the `Frame` where you need it. I know that adds a bit of complexity, but it might work for you. – JRiggles Dec 16 '22 at 18:10