TclError:无法iconify:设置override-redirect标志

时间:2015-03-21 18:20:21

标签: python tkinter root

我正在开展一个项目,在那里我摆脱了根窗口。我自己做了。我有它的工作,但我没有找到一种方法来最小化窗口。

我尝试使用root.iconify()设置root.root.overrideredirect(True)。但它给了我以下错误:TclError: can't iconify ".": override-redirect flag is set

如何在不出现此错误的情况下最小化窗口,以及如何将其最小化,就像普通的根窗口一样。

代码:

from Tkinter import *
import time
import os

class Application(Frame):
    def __init__(self, parent):
       Frame.__init__(self,parent)
       self.pack(fill=BOTH)

       self.create_widgets()

    def create_widgets(self):
        self.borderFrame = Frame(self, width=500, height=600, bg="Gray")
        self.borderFrame.pack_propagate(False)
        self.borderFrame.pack(side=TOP)

        self.holderFrame = Frame(self.borderFrame, width=500, height=570, bg="blue")
        self.holderFrame.pack_propagate(False)
        self.holderFrame.pack(side=BOTTOM)

        self.close = Label(self, font=("Arial", 11), bg="Gray", anchor=CENTER, text="X", cursor="hand2")
        self.close.place(x=460, y=0, width=40, height=30)

        self.min = Label(self, font=("Arial", 11), bg="Gray", anchor=CENTER, text="_", cursor="hand2")
        self.min.place(x=420, y=0, width=40, height=30)

        def hoverMin(event):
            event.widget.config(bg="lightBlue")

        def unHoverMin(event):
            event.widget.config(bg="Gray")

        self.min.bind("<Enter>", hoverMin)
        self.min.bind("<Leave>", unHoverMin)
        self.min.bind("<Button-1>", self.minimize)

        def hover(event):
            event.widget.config(bg="red")

        def unhover(event):
            event.widget.config(bg="Gray")

        self.close.bind("<Enter>", hover)
        self.close.bind("<Leave>", unhover)
        self.close.bind("<Button-1>", self.exitProgram)

        self.borderFrame.bind("<Button-1>", self.startMove)
        self.borderFrame.bind("<ButtonRelease-1>", self.stopMove)
        self.borderFrame.bind("<B1-Motion>", self.moving)

    def startMove(self, event):
        self.x = event.x
        self.y = event.y

    def stopMove(self, event):
        self.x = None
        self.y = None

    def moving(self,event):
        x = (event.x_root - self.x - self.borderFrame.winfo_rootx() + self.borderFrame.winfo_rootx())
        y = (event.y_root - self.y - self.borderFrame.winfo_rooty() + self.borderFrame.winfo_rooty())
        root.geometry("+%s+%s" % (x, y))

    def minimize(self, event):
        root.iconify()

    def exitProgram(self, event):
        os._exit(0)

root = Tk()
root.title("Draggable Root")
root.geometry("500x600")
root.overrideredirect(True)

app = Application(root)

root.mainloop()

1 个答案:

答案 0 :(得分:3)

调用overrideredirect(True)向窗口管理器声明您将处理所有自己的窗口管理。 Iconification是一个窗口管理器的东西。如果您只想隐藏窗口,那么将最小化函数更改为root.state(&#39;撤回&#39;)就可以了。调用root.state(&#39; normal&#39;)再次显示该窗口。

要显示在任务栏中,您需要在调用root.state(&#39; iconic&#39;)之前调用root.overrideredirect(False),然后再调用root.update_idletasks()。请参阅最小化方法的更改。

为了让窗口以你想要的方式回来,我制作了一个新的方法&#39; frame_mapped&#39;并将其绑定到borderFrame上的Map事件。当几何管理器(包/地块/网格)处理小部件时,会发生地图事件。因此,当您单击任务栏以取消最小化时,&#39; frame_mapped&#39;被触发再次调用overrideredirect(True)。

以下是修改后的代码:

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

import time
import os

class Application(Frame):
    def __init__(self, parent):
       Frame.__init__(self,parent)
       self.pack(fill=BOTH)

       self.create_widgets()

    def create_widgets(self):
        self.borderFrame = Frame(self, width=500, height=600, bg="Gray")
        self.borderFrame.pack_propagate(False)
        self.borderFrame.pack(side=TOP)

        self.holderFrame = Frame(self.borderFrame, width=500, height=570, bg="blue")
        self.holderFrame.pack_propagate(False)
        self.holderFrame.pack(side=BOTTOM)

        self.close = Label(self, font=("Arial", 11), bg="Gray", anchor=CENTER, text="X", cursor="hand2")
        self.close.place(x=460, y=0, width=40, height=30)

        self.min = Label(self, font=("Arial", 11), bg="Gray", anchor=CENTER, text="_", cursor="hand2")
        self.min.place(x=420, y=0, width=40, height=30)

        def hoverMin(event):
            event.widget.config(bg="lightBlue")

        def unHoverMin(event):
            event.widget.config(bg="Gray")

        self.min.bind("<Enter>", hoverMin)
        self.min.bind("<Leave>", unHoverMin)
        self.min.bind("<Button-1>", self.minimize)

        def hover(event):
            event.widget.config(bg="red")

        def unhover(event):
            event.widget.config(bg="Gray")

        self.close.bind("<Enter>", hover)
        self.close.bind("<Leave>", unhover)
        self.close.bind("<Button-1>", self.exitProgram)

        self.borderFrame.bind("<Button-1>", self.startMove)
        self.borderFrame.bind("<ButtonRelease-1>", self.stopMove)
        self.borderFrame.bind("<B1-Motion>", self.moving)

        self.borderFrame.bind("<Map>",self.frame_mapped)

    def startMove(self, event):
        self.x = event.x
        self.y = event.y

    def stopMove(self, event):
        self.x = None
        self.y = None

    def moving(self,event):
        x = (event.x_root - self.x - self.borderFrame.winfo_rootx() + self.borderFrame.winfo_rootx())
        y = (event.y_root - self.y - self.borderFrame.winfo_rooty() + self.borderFrame.winfo_rooty())
        root.geometry("+%s+%s" % (x, y))

    def frame_mapped(self,e):
        print(self,e)
        root.update_idletasks()
        root.overrideredirect(True)
        root.state('normal')


    def minimize(self, event):
        root.update_idletasks()
        root.overrideredirect(False)
        #root.state('withdrawn')
        root.state('iconic')

    def exitProgram(self, event):
        os._exit(0)

root = Tk()
root.title("Draggable Root")
root.geometry("500x600")
root.overrideredirect(True)

app = Application(root)

root.mainloop()