使Tkinter窗口显示在任务栏中

时间:2010-11-01 01:02:46

标签: python windows-7 tkinter

我希望我的程序出现在任务栏中,但仍然没有传统的Windows寄存器。我怎么能这样做?我知道 self.overrideredirect(1),但这会从任务栏中删除我的程序。

这适用于Windows 7.

2 个答案:

答案 0 :(得分:4)

我没有声称这是“正确”的做法,但看看这是否适合你:

try:
    from tkinter import *
except ImportError:
    from Tkinter import *


class NewRoot(Tk):    
    def __init__(self):
        Tk.__init__(self)
        self.attributes('-alpha', 0.0)

class MyMain(Toplevel):
    def __init__(self, master):
        Toplevel.__init__(self, master)
        self.overrideredirect(1)
        self.attributes('-topmost', 1)
        self.geometry('+100+100')
        self.bind('<ButtonRelease-3>', self.on_close)  #right-click to get out

    def on_close(self, event):
        self.master.destroy()


if __name__ == '__main__':

    root = NewRoot()
    root.lower()
    root.iconify()
    root.title('Spam 2.0')

    app = MyMain(root)
    app.mainloop()

答案 1 :(得分:2)

您可以在根对象下添加一个顶层窗口,使root不可见,然后处理图标事件以隐藏或显示顶层窗口。

root = tkinter.Tk()
top = tkinter.Toplevel(root)
top.overrideredirect(1) #removes border but undesirably from taskbar too (usually for non toplevel windows)
root.attributes("-alpha",0.0)

#toplevel follows root taskbar events (minimize, restore)
def onRootIconify(event): top.withdraw()
root.bind("<Unmap>", onRootIconify)
def onRootDeiconify(event): top.deiconify()
root.bind("<Map>", onRootDeiconify)

window = tkinter.Frame(master=top)
window.mainloop()