所以我只是想创建一个带有阴影的窗口。我想同时移动两个窗口。到目前为止,它是有效的。然而,当我点击边缘时;影子;窗户突出了。是否有可能将第二个窗口永久地留在后台?
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
class CFrame(Tk):
def __init__(self, parent=None, bg="#1b1e21", bd=1, bdcolor="lime"):
super().__init__(parent)
self.bg = bg
self.bd = bd
self.bdcolor = bdcolor
self.title("CFrame")
self.geometry("740x740+200+200")
self.overrideredirect(1)
self.bind("<B1-Motion>", self.dragWindow)
self.bind("<Button-1>", self.clickWindow)
self.shadow = Shadow()
self.headingFrame = Frame(self, bg="lime")
self.headingFrame.pack(fill="x")
self.mainFrame = Frame(self, bg=self.bg, highlightthickness=bd,
highlightbackground=self.bdcolor, highlightcolor=self.bdcolor)
self.mainFrame.pack(fill="both", expand="yes")
self.exitButton = ttk.Button(self.headingFrame, text="u2613", command=self.programQuit)
self.exitButton.pack(side="right")
self.offSetX = 0
self.offSetY = 0
def dragWindow(self, event):
x = self.winfo_pointerx() - self.offSetX
y = self.winfo_pointery() - self.offSetY
x2 = self.winfo_pointerx() - self.offSetX - 10
y2 = self.winfo_pointery() - self.offSetY - 10
self.geometry("+%d+%d" % (x,y))
self.shadow.geometry("+%d+%d" % (x2,y2))
def clickWindow(self, event):
self.offSetX = event.widget.winfo_rootx() - self.winfo_rootx() + event.x
self.offSetY = event.widget.winfo_rooty() - self.winfo_rooty() + event.y
def programQuit(self):
ex = messagebox.askyesno("Quit?", "Quit?")
if ex == 1:
self.destroy()
class Shadow(Tk):
def __init__(self):
super().__init__()
self.title("Shadow")
self.geometry("760x760+190+190")
self.overrideredirect(1)
self.attributes("-alpha", 0.3)
self.bg = Frame(self, bg="black")
self.bg.pack(fill="both", expand="yes")
if __name__ == '__main__':
cf = CFrame()
cf.mainloop()
也许代码有点太多了,但我认为你可以用整个例子更好地设计它
我已经在这里做了类似的事情。它通过绑定到伪阴影窗口上的"<FocusIn>"
并在主窗口上调用.focus_force()
来工作。像这样:
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
class CFrame(tk.Tk):
def __init__(self, bg="#1b1e21", bd=1, bdcolor="lime"):
super().__init__()
self.bg = bg
self.bd = bd
self.bdcolor = bdcolor
super().geometry("740x740+200+200")
super().overrideredirect(True)
super().bind("<B1-Motion>", self.drag_window)
super().bind("<Button-1>", self.click_window)
self.shadow = Shadow()
self.heading_frame = tk.Frame(self, bg="lime")
self.heading_frame.pack(fill="x")
self.main_frame = tk.Frame(self, bg=self.bg, highlightthickness=bd,
highlightbackground=self.bdcolor,
highlightcolor=self.bdcolor)
self.main_frame.pack(fill="both", expand="yes")
self.exit_button = ttk.Button(self.heading_frame, text="u2613",
command=self.program_quit)
self.exit_button.pack(side="right")
self.offset_x = 0
self.offset_y = 0
self.shadow.bind("<FocusIn>", self.focus_main)
def focus_main(self, event):
super().focus_force()
def drag_window(self, event):
x = self.winfo_pointerx() - self.offset_x
y = self.winfo_pointery() - self.offset_y
x2 = self.winfo_pointerx() - self.offset_x - 10
y2 = self.winfo_pointery() - self.offset_y - 10
super().geometry("+%d+%d" % (x,y))
self.shadow.geometry("+%d+%d" % (x2,y2))
def click_window(self, event):
self.offset_x = event.widget.winfo_rootx() - self.winfo_rootx() + event.x
self.offset_y = event.widget.winfo_rooty() - self.winfo_rooty() + event.y
def program_quit(self):
result = messagebox.askyesno("Quit?", "Are you sure you want to quit?")
if result:
super().destroy()
self.shadow.destroy()
class Shadow(tk.Tk):
def __init__(self):
super().__init__()
super().geometry("760x760+190+190")
super().overrideredirect(True)
super().attributes("-alpha", 0.3)
super().config(bg="black")
if __name__ == "__main__":
cf = CFrame()
cf.mainloop()
此外,我从阴影窗口中删除了框架,并将其替换为<tkinter.Tk>.config(bg="black")
。
另一件事:当您要使用.overrideredirect(True)
时,调用.title(...)
有意义吗?