I am developing an app for myself that (in this case) displays a list of items. However, this list of items takes about 10 seconds to pull, so in the meantime, I would like to show the user a loading screen. Currently, my program uses the place
method to place, and then remove a gif (animated using a custom image class), like this:
and fully loaded:
However, I have another frame (in the notebook- you can see the tabs) that I will eventually code to have content and an independent loading image (it will start/stop at different times than the first tab). However, when I move to the second tab while the first tab is loading, the place manager unwantedly keeps the image there:
How can I get this image to only display on the first tab but not the second? Since this is a project for myself, I am not focused on perfection, and if necessary, I will most likely just add a text element to the treeview stating it is loading (the other frame will also have a treeview too). I would also rather not learn a new language (I know some java and JS but python is my strength), because, as I said before, this is just a hobby for me.
Here is a minimal reproducible example. The code needs one file in the same directory (img.png) to run. You will notice that the loading image will be present in both tab1 and tab2, even though tab2 already has data.
import tkinter as tk from tkinter import ttk from time import sleep from PIL import Image, ImageTk root = tk.Tk() root.geometry('400x300') root.title('Notebook Demo') notebook = ttk.Notebook(root) notebook.pack(pady=10, expand=True) frame1 = ttk.Frame(notebook, width=400, height=280) frame2 = ttk.Frame(notebook, width=400, height=280) frame1.pack(fill='both', expand=True) frame2.pack(fill='both', expand=True) notebook.add(frame1, text='1') notebook.add(frame2, text='2') label1 = ttk.Label(frame1, text = "stuff of 1 - loading") label2 = tk.Label(frame2, text="label 2 data: already here", width=60, height=15) label1.pack() label2.pack() def load_data(): label1.config(text='label 1 data: 10110') label2.place_forget() image1 = Image.open("img.png").resize((50,100)) test = ImageTk.PhotoImage(image1) label2 = ttk.Label(image=test) label2.image = test label2.place(x=30,y=30) root.after(5000,load_data) root.mainloop()
Advertisement
Answer
As stated in the comments, the problem relays on..:
ttk.Label(image=test)
if no positional argument, means a argument without=
, is given as the master parameter, theroot
window is set by default.
Give a master to it (i.e ttk.Label(frame1,image=test)
)