unix 当点击根窗口时,使顶层窗口弹出到顶部

luaexgnf  于 2023-10-18  发布在  Unix
关注(0)|答案(2)|浏览(154)

我有一个UI根窗口,其中两个其他顶层窗口得到单独的按钮点击创建。这些顶层窗口被锚定到根窗口,并与根窗口一起沿着屏幕拖动。
我的问题是,如果我有另一个窗口打开,我的UI是隐藏在它后面,如果我点击我的UI从窗口或小我可以在屏幕上看到,只有根Tk窗口弹出,其他Toplevel窗口仍然隐藏在其他窗口后面。
我试过toplevel.lift()toplevel.wm_attributes("-topmost", 1),但都没有给我给予我想要的。
我怎样才能绑定Toplevel窗口,这样如果它们打开了,我点击根窗口,Toplevel窗口也会弹出到顶部?

oo7oh9g9

oo7oh9g91#

下面是一个简单的例子,它将打开2个窗口并禁用根窗口上的所有内容,同时还绑定与该根窗口的任何交互,以提升其上方的所有顶部窗口。
我还绑定了顶层关闭事件,首先删除根绑定,然后销毁顶层,然后重新启用根窗口中的所有小部件。这应该是一个古老的例子,说明你正在努力做什么。
如果你有任何问题,请告诉我。

import tkinter as tk

class ExampleApp(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.master = master
        self.master.geometry("400x150")
        self.main_frame = tk.Frame(self.master)
        self.main_frame.pack(expand=tk.YES, fill=tk.BOTH)
        self.master.protocol('<WM_LBUTTONDBLCLK>', self.motion)
        tk.Label(self.main_frame, text = "This is the main window").pack()
        tk.Button(self.main_frame, text = "Open 2 top level windows!", command = self.open_windows).pack()

    def motion(self, event):
        x, y = event.x, event.y
        print('{}, {}'.format(x, y))

    def open_windows(self):
        self.top1 = tk.Toplevel(self.master)
        self.top2 = tk.Toplevel(self.master)
        self.top1.geometry("100x100")
        self.top2.geometry("100x100")
        # ties the window close event to our customer close method for toplevel
        self.top1.protocol("WM_DELETE_WINDOW", self.close_toplevels)
        self.top2.protocol("WM_DELETE_WINDOW", self.close_toplevels)
        self.master.bind("<Unmap>", self.icon_all)
        self.top1.bind("<Unmap>", self.icon_all)
        self.top2.bind("<Unmap>", self.icon_all)
        self.master.bind("<Map>", self.de_icon_all)
        self.top1.bind("<Map>", self.de_icon_all)
        self.top2.bind("<Map>", self.de_icon_all)

        for child in self.main_frame.winfo_children():
            child.configure(state='disable')

        tk.Label(self.top1, text ="Topwindow 1").pack()
        tk.Label(self.top2, text ="Topwindow 2").pack()

        # sets the top windows to their initial locations
        self.lock_top_to_root()

        #keeps the top windows in the specified locations compared to root window
        self.master.bind("<Configure>", self.lock_top_to_root)

    def withdraw_tops(self, event=None):
        self.top1.withdraw()
        self.top2.withdraw()

    def de_icon_tops(self, event=None):
        self.top1.deiconify()
        self.top2.deiconify()

    def icon_all(self, event=None):
        self.withdraw_tops()
        self.master.iconify()

    def de_icon_all(self, event=None):
        self.de_icon_tops()
        self.master.deiconify()
        self.lock_top_to_root()

    def lock_top_to_root(self, event=None):
        self.top1.lift() # lift both toplevel windows about root
        self.top2.lift()
        # places each top level at each side
        # this is not set up to compensate for the root being resized but can be if you need it to.
        self.top1.geometry('+{}+{}'.format(self.master.winfo_x()+10, self.master.winfo_y()+30))
        self.top2.geometry('+{}+{}'.format(self.master.winfo_x()+275, self.master.winfo_y()+30))

    def close_toplevels(self):
        # customer close method to reset everything
        self.master.unbind('<Configure>')
        self.master.unbind("<Unmap>")
        self.master.unbind("<Map>")
        self.top1.destroy()
        self.top2.destroy()
        for child in self.main_frame.winfo_children():
            child.configure(state='active')

root = tk.Tk()
my_example = ExampleApp(root)
root.mainloop()
kknvjkwl

kknvjkwl2#

“transient”-指定顶层窗口的父窗口。设置后,Toplevel窗口被视为临时窗口,并位于其父窗口的顶部。

class MyClass(TopLevel):
    def __init__(self, master):
        super().__init__()
        self.transient(master)

相关问题