当前位置: 首页 > 面试题库 >

使tkinter窗口出现在任务栏中

刘高峯
2023-03-14
问题内容

我正在制作音乐播放器GUI,但无法使其显示在任务栏或Alt-
Tab中。我已将overrideredirect()设置为true以删除边框和标题。我也做到了,这样当用户执行“鼠标单击并拖动”操作时,窗口就会移动。这是代码:

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中?


问题答案:

经过一些研究,我发现有一种方法可以执行此操作,但是它涉及使用ctypes,并且是Windows的唯一解决方案:

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

但64位的需要:
GetWindowLongPtrWSetWindowLongPtrW

GetWindowLongPtrASetWindowLongPtrA
看到这

或者,如果您默认需要这种行为:

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


 类似资料:
  • 问题内容: 我想让我的程序显示在任务栏中,但仍然没有传统的Windows寄宿生。我该怎么办?我知道 self.overrideredirect(1) ,但是这从任务栏中删除了我的程序。 这适用于Windows 7。 问题答案: 我没有断言这是“正确”的方法,但是请看这是否对您有用:

  • 这个问题我已经找了很多遍了,但似乎没有人知道。我在python 2.7中创建了一个简单的tkinter窗口(tcl 8.5),并希望它最大化,就像我要按右上角的maximize按钮一样。使用 选项不是一个选项,因为它会删除标题栏。 我尝试了以下操作: 问题是窗口现在在Windows任务栏下面,因此我的一些元素没有显示出来。一个简单的方法是将height设置为screenheight-some_co

  • 我用pyGtk制作了一扇没有装饰的窗户。该窗口隐藏在任务栏和所有窗口的顶部。在linux上,它可以正常工作,但在MS Windows窗口上,它有时会隐藏在其他窗口下,并且在Windows中的任务栏上总是有“python.exe”。 代表我问题的图片: 如何从任务栏隐藏这个“python.exe”窗口? 我的代码:

  • 问题内容: 我创建了一个窗口: 并删除标题栏: 现在,该窗口不在Windows的任务栏上。如何在任务栏中显示它?(如果其他窗户在我的顶部,我只想将窗户放到最前面) 问题答案: Tk没有提供一种方法来使具有设置了 overrideredirect 的顶级窗口出现在任务栏上。为此,该窗口需要应用WS_EX_APPWINDOW扩展样式,并且此类型的Tk窗口已设置WS_EX_TOOLWINDOW。我们可以

  • 问题内容: 如何使用JavaScript在任务栏中使用户的浏览器闪烁/闪烁/突出显示?例如,如果我每10秒发出一次AJAX请求,以查看用户在服务器上是否有任何新消息,我希望用户立即知道它,即使他当时正在使用其他应用程序。 编辑:这些用户确实希望在收到新消息时分心。 问题答案: 这不会使任务栏按钮以改变颜色的方式闪烁,但是标题将一直闪烁,直到他们移动鼠标为止。这应该可以跨平台使用,即使他们只是在其他

  • 我正在使用Python/Tkinter在XP上编写一个IM软件。现在,我已经完成了所有的主要功能,除了我不知道如何突出显示或改变颜色我的IM项目在任务栏在windows xp当窗口最小化到任务栏时,当收到一个新的消息。我已经搜索过了,但是得到了C#解。我在Python上需要帮助。谢啦!