1

I am trying to display a tkinter window (with an image,in my case) that doesn't show on the taskbar. I've already tried using the method described here, but the person that answered didn't provide an example so I am unable to replicate it (and I'm using Windows 10 so maybe that is another factor to consider).

My code is here. I am using Python 3.5

from tkinter import Toplevel, Tk, Label, PhotoImage

win = Tk()

win.attributes('-alpha', 0.0)
win.iconify()

window = Toplevel(win)
window.geometry("500x500+100+100")
window.overrideredirect(1)

photo = PhotoImage(file="testfile.png")

label = Label(window, image=photo)
label.pack()

win.mainloop()
nicochulo
  • 111
  • 2
  • 11

1 Answers1

2

The linked question contained an interesting comment recommending to reverse what was done in that other answer. Along with this accepted answer that explains that what is needed is just to set the WS_EX_TOOLWINDOW style, a simple example is:

import tkinter as tk
import tkinter.ttk as ttk
from ctypes import windll

GWL_EXSTYLE=-20
WS_EX_TOOLWINDOW=0x00000080

def set_toolwindow(root):
    hwnd = windll.user32.GetParent(root.winfo_id())
    style = windll.user32.GetWindowLongPtrW(hwnd, GWL_EXSTYLE)
    style = style | WS_EX_TOOLWINDOW
    res = windll.user32.SetWindowLongPtrW(hwnd, GWL_EXSTYLE, style)
    # re-assert the new window style
    root.wm_withdraw()
    root.after(10, lambda: root.wm_deiconify())

def main():
    root = tk.Tk()
    root.wm_title("AppWindow Test")
    button = ttk.Button(root, text='Exit', command=lambda: root.destroy())
    button.place(x=10,y=10)
    #root.overrideredirect(True)
    root.after(10, lambda: set_toolwindow(root))
    root.mainloop()

if __name__ == '__main__':
    main()
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Credits should remain to authors of the two linked posts. This just uses both to provide an example to OP. – Serge Ballesta Feb 16 '18 at 08:13
  • Thanks! I am a noobie and this example helped me a lot, but I don't know how to transform my code to integrate that functionality. I suppose I need to introduce my code into a "def main():", but I don't know how to do that. If you could please help me in that regard, I'd be very grateful – nicochulo Feb 16 '18 at 12:16
  • @nicochulo: `def main(): ... if __name__ == '__main__': main()` if a common idiom to allow a script to be executed as a stand alone program or included in another script. It has nothing to do with your current problem. The part of interest for you is `set_toolwindow`. – Serge Ballesta Feb 16 '18 at 12:22
  • I've tried it and it works perfectly! I just added that "def set_toolwindow(root):" and all of its code and I replaced all "root" with "win" (because in my case those are called win). I'll upvote and mark as answer – nicochulo Feb 16 '18 at 12:46