简体   繁体   中英

How to hide tkinter root window and show my image?

I have this code where I click the button and tkinter window shows an image along with a tk root window. I tried using root.withdraw() however this just ends up creating the tkinter root window and does not display my image.. Would need help in showing the image only and hiding the tkinter root window as well

def on_click_button():
    global stage
    stage == 'Menu'
    root = tk.Toplevel()
    photoImageObj = tk.PhotoImage(file="showimg.png")             
    lab = tk.Label(root, image=photoImageObj).pack()
    root.withdraw()
    root.mainloop()
    print('You clicked samsung note 20')

Tkinter sets the master attribute of a widget to be a reference to the master for that widget. Since you didn't explicitly set the master for the instance of Toplevel , master will be the root window. So, in this specific case you can call withdraw on root.master :

root.master.withdraw()

Personally, I would recommend against naming your Toplevel "root" since it's not the true root window. It makes the code a bit harder to understand since a true root window will not have a master.

You will want to provide a mechanism for showing the root window at some point, since withdrawing it will keep it alive but not visible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM