TclError: can't iconify: override-redirect flag is set
TclError: can't iconify: override-redirect flag is set
我正在做一个项目,在那里我摆脱了根 window。我自己做了。我让它工作,但我一直没能找到最小化 window 的方法。
我尝试使用 root.iconify()
和 root.root.overrideredirect(True)
集。但它给了我以下错误:TclError: can't iconify ".": override-redirect flag is set
。
如何最小化 window 而不会出现此错误,以及如何像普通根 window 一样最小化它。
代码:
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()
调用 overrideredirect(True) 向 window 经理声明您将处理您自己的所有 window-管理。图标化是 window 经理的事情。如果您只想隐藏 window,则将最小化函数更改为 root.state('withdrawn') 即可。调用 root.state('normal') 再次显示 window。
为了出现在任务栏中,您需要在调用 root.state('iconic') 之前调用 root.overrideredirect(False) 并且在此之前您需要调用 root.update_idletasks()。查看最小化方法的更改。
为了让 window 回到你想要的方式,我创建了一个新方法 'frame_mapped' 并将其绑定到 borderFrame 上的 Map 事件。当小部件被几何管理器 (pack/place/grid) 处理时,地图事件就会发生。因此,当您单击任务栏取消最小化时,'frame_mapped' 会触发再次调用 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()
我正在做一个项目,在那里我摆脱了根 window。我自己做了。我让它工作,但我一直没能找到最小化 window 的方法。
我尝试使用 root.iconify()
和 root.root.overrideredirect(True)
集。但它给了我以下错误:TclError: can't iconify ".": override-redirect flag is set
。
如何最小化 window 而不会出现此错误,以及如何像普通根 window 一样最小化它。
代码:
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()
调用 overrideredirect(True) 向 window 经理声明您将处理您自己的所有 window-管理。图标化是 window 经理的事情。如果您只想隐藏 window,则将最小化函数更改为 root.state('withdrawn') 即可。调用 root.state('normal') 再次显示 window。
为了出现在任务栏中,您需要在调用 root.state('iconic') 之前调用 root.overrideredirect(False) 并且在此之前您需要调用 root.update_idletasks()。查看最小化方法的更改。
为了让 window 回到你想要的方式,我创建了一个新方法 'frame_mapped' 并将其绑定到 borderFrame 上的 Map 事件。当小部件被几何管理器 (pack/place/grid) 处理时,地图事件就会发生。因此,当您单击任务栏取消最小化时,'frame_mapped' 会触发再次调用 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()