Make tkinter Window appear in the taskbar

FoxSGR

I'm making a music player GUI and I can't make it appear on the taskbar or in Alt-Tab. I have set overrideredirect() to true to remove the borders and the title. I also made it so that when the user does a 'mouse click and drag' action the window moves. Here's the code:

import tkinter
import sys
import os


class Win(tkinter.Tk):
    global imgfile
    imgfile = r"play.png"

    def __init__(self, master=None):
        def close():
            self.destroy()
            sys.exit()

        def dirchosen():
            global songlist
            songlist = []
            try:
                os.chdir(dirinput.get())
            except:
                print("Invalid Directory")
                raise
            for file in os.listdir(dirinput.get()):
                songlist.append(file)

        tkinter.Tk.__init__(self, master)
        self.overrideredirect(True)
        self._offsetx = 0
        self._offsety = 0

        self.bind('<Button-1>', self.clickwin)
        self.bind('<B1-Motion>', self.dragwin)

        self.geometry("350x200")
        self.config(bg="#FF4766")

        titlelabel = tkinter.Label(self, text="FoxSGR Media Player", bg="#FF4766", font=("Segoe UI", 12))
        titlelabel.pack(ipady=4)

        chdirbutton = tkinter.Button(self, relief="groo", activebackground="#FF8080", command=dirchosen)
        chdirbutton.config(text="Choose Directory", bg="#FF4766", font=("Segoe UI", 8))
        chdirbutton.pack()
        chdirbutton.place(relx=0.66, rely=0.22)

        dirinput = tkinter.Entry(self, font=("Segoe UI", 8), width="34")
        dirinput.pack()
        dirinput.place(relx=0.05, rely=0.23)

        xbutton = tkinter.Button(self, text="x", height="1", command=close)
        xbutton.config(bg="red", activebackground="#FF8080", relief="groo", font=("Segoe UI", 8))
        xbutton.pack()
        xbutton.place(relx=0.90, rely=0.05)

    def dragwin(self, event):
        x = self.winfo_pointerx() - self._offsetx
        y = self.winfo_pointery() - self._offsety
        self.geometry('+{x}+{y}'.format(x=x, y=y))

    def clickwin(self, event):
        self._offsetx = event.x
        self._offsety = event.y

win = Win()

# Had to set the images appart from up there

imgplay = tkinter.PhotoImage(file=imgfile)
playbutton = tkinter.Button(win, image=imgplay, bg="#FF4766", relief="flat")
playbutton.pack()
playbutton.place(rely=0.45, relx=0.4)

imgnext = tkinter.PhotoImage(file="next.png")
nextbutton = tkinter.Button(win, image=imgnext, bg="#FF4766", relief="flat")
nextbutton.pack()
nextbutton.place(rely=0.45, relx=0.57)

imgprev = tkinter.PhotoImage(file="previous.png")
prevbutton = tkinter.Button(win, image=imgprev, bg="#FF4766", relief="flat")
prevbutton.pack()
prevbutton.place(rely=0.45, relx=0.30)

win.mainloop()

Is there any way that I can make it at least appear in Alt-Tab?

James Kent

after a bit of research i have found that there is a way to do this, but it involves using ctypes and is a windows only solution:

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

GWL_EXSTYLE=-20
WS_EX_APPWINDOW=0x00040000
WS_EX_TOOLWINDOW=0x00000080

def set_appwindow(root):
    hwnd = windll.user32.GetParent(root.winfo_id())
    style = windll.user32.GetWindowLongW(hwnd, GWL_EXSTYLE)
    style = style & ~WS_EX_TOOLWINDOW
    style = style | WS_EX_APPWINDOW
    res = windll.user32.SetWindowLongW(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_appwindow(root))
    root.mainloop()

if __name__ == '__main__':
    main()

this involves using ctypes to manipulate the windows style, however you need to use the right Get/Set functions depending on the applications environment. for 32 bit windows it seems you need to use either:
GetWindowLongW and SetWindowLongW
or
GetWindowLongA and SetWindowLongA

but 64 bit needs:
GetWindowLongPtrW and SetWindowLongPtrW
or
GetWindowLongPtrA and SetWindowLongPtrA
see this

or alternatively, if you want this behaviour by default:

import tkinter as tk

from ctypes import windll

class Tk(tk.Tk):
    def overrideredirect(self, boolean=None):
        tk.Tk.overrideredirect(self, boolean)
        GWL_EXSTYLE=-20
        WS_EX_APPWINDOW=0x00040000
        WS_EX_TOOLWINDOW=0x00000080
        if boolean:
            print("Setting")
            hwnd = windll.user32.GetParent(self.winfo_id())
            style = windll.user32.GetWindowLongW(hwnd, GWL_EXSTYLE)
            style = style & ~WS_EX_TOOLWINDOW
            style = style | WS_EX_APPWINDOW
            res = windll.user32.SetWindowLongW(hwnd, GWL_EXSTYLE, style)
        self.wm_withdraw()
        self.wm_deiconify()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Tkinter window with both title bar and windows taskbar

From Dev

Tkinter window with both title bar and windows taskbar

From Dev

TkInter: how can I make objects appear on my second window rather than the first?

From Dev

Tkinter widgets set to appear in multiple windows appear in the same window

From Dev

How do I make a Chrome extension's window blink in the taskbar?

From Dev

How to make a Tkinter window not resizable?

From Dev

How to make Form1 appear / disappear when clicking icon on taskbar?

From Dev

Different icon for Window and Taskbar

From Dev

Taskbar broken in Window 7

From Dev

Close window from the taskbar?

From Dev

How to make a window fullscreen in a secondary display with tkinter?

From Dev

Tkinter, make a window when certain conditions are met?

From Dev

How to make window appear on top of all other windows?

From Dev

Make my wpf application Full Screen (Cover taskbar and title bar of window)

From Dev

How to make Excel completely Full screen, hiding the Title-bar of Window & the Windows Taskbar?

From Dev

tkinter messagebox not showing on Windows taskbar

From Dev

Hide console window, not the taskbar button

From Dev

Opening a window without an icon in the taskbar

From Dev

Combine taskbar shortcut and window icons

From Dev

Group window tabs on taskbar/panel

From Dev

Second Idea window not shown in taskbar

From Dev

Hide window from Linux taskbar

From Dev

How to make a background image of a modal window appear simultaneously with a modal window, not before?

From Dev

How to make a background image of a modal window appear simultaneously with a modal window, not before?

From Dev

How can I make a window appear on top of another window, without closing when I click the second?

From Dev

Window is appearing behind the Windows taskbar settings window

From Dev

Window is appearing behind the Windows taskbar settings window

From Dev

How To Let Your Main Window Appear after succesful login in Tkinter(PYTHON 3.6

From Dev

How to make tkinter Window floating in i3 windowmanager

Related Related

  1. 1

    Tkinter window with both title bar and windows taskbar

  2. 2

    Tkinter window with both title bar and windows taskbar

  3. 3

    TkInter: how can I make objects appear on my second window rather than the first?

  4. 4

    Tkinter widgets set to appear in multiple windows appear in the same window

  5. 5

    How do I make a Chrome extension's window blink in the taskbar?

  6. 6

    How to make a Tkinter window not resizable?

  7. 7

    How to make Form1 appear / disappear when clicking icon on taskbar?

  8. 8

    Different icon for Window and Taskbar

  9. 9

    Taskbar broken in Window 7

  10. 10

    Close window from the taskbar?

  11. 11

    How to make a window fullscreen in a secondary display with tkinter?

  12. 12

    Tkinter, make a window when certain conditions are met?

  13. 13

    How to make window appear on top of all other windows?

  14. 14

    Make my wpf application Full Screen (Cover taskbar and title bar of window)

  15. 15

    How to make Excel completely Full screen, hiding the Title-bar of Window & the Windows Taskbar?

  16. 16

    tkinter messagebox not showing on Windows taskbar

  17. 17

    Hide console window, not the taskbar button

  18. 18

    Opening a window without an icon in the taskbar

  19. 19

    Combine taskbar shortcut and window icons

  20. 20

    Group window tabs on taskbar/panel

  21. 21

    Second Idea window not shown in taskbar

  22. 22

    Hide window from Linux taskbar

  23. 23

    How to make a background image of a modal window appear simultaneously with a modal window, not before?

  24. 24

    How to make a background image of a modal window appear simultaneously with a modal window, not before?

  25. 25

    How can I make a window appear on top of another window, without closing when I click the second?

  26. 26

    Window is appearing behind the Windows taskbar settings window

  27. 27

    Window is appearing behind the Windows taskbar settings window

  28. 28

    How To Let Your Main Window Appear after succesful login in Tkinter(PYTHON 3.6

  29. 29

    How to make tkinter Window floating in i3 windowmanager

HotTag

Archive