使 tkinter 窗口出现在任务栏中 [英] Make tkinter Window appear in the taskbar

查看:131
本文介绍了使 tkinter 窗口出现在任务栏中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个音乐播放器 GUI,但我无法让它出现在任务栏或 Alt-Tab 中.我已将 overrideredirect() 设置为 true 以删除边框和标题.我还做到了当用户执行鼠标单击并拖动"操作时,窗口会移动.代码如下:

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()

有什么办法可以让它至少出现在 Alt-Tab 中?

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

推荐答案

经过一些研究,我发现有一种方法可以做到这一点,但它涉及使用 ctypes 并且是仅适用于 Windows 的解决方案:

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()

这涉及使用 ctypes 来操作 windows 样式,但是您需要根据应用程序环境使用正确的 Get/Set 函数.对于 32 位窗口,您似乎需要使用:
GetWindowLongWSetWindowLongW

GetWindowLongASetWindowLongA

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

但 64 位需要:
GetWindowLongPtrWSetWindowLongPtrW

GetWindowLongPtrASetWindowLongPtrA
这个

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()

这篇关于使 tkinter 窗口出现在任务栏中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆