2017-08-28 80 views
0

我有一個UI根窗口,其中兩個其他Toplevel窗口在單獨的按鈕單擊上創建。這些Toplevel窗口被錨定到根窗口並沿着帶有根窗口的屏幕拖動。當按鈕窗口點擊時,使Toplevel窗口彈出到頂部

我的問題是如果我有另一個窗口打開,我的用戶界面隱藏在它後面,如果我點擊我的用戶界面從任務欄或小我可以看到在屏幕上,只有根窗口彈出和另一個Toplevel窗戶仍然藏在另一個窗戶後面。

我試過toplevel.lift()toplevel.wm_attributes("-topmost", 1),但都不給我想要的東西。

我該如何綁定Toplevel窗口,以便如果它們打開並且我點擊根窗口Toplevel窗口也會彈出到頂部?

+0

請提供[MCVE] –

+0

嘗試'toplevel_name.attributes(「 - 層次最高的」,真)' –

+0

事情是這使得頂層窗口的最頂層高於一切,即使我點擊另一個窗口。我想讓整個UI消失,如果我點擊其他東西,但是如果我再次點擊UI並且Toplevel打開,我想要頂層以及當單擊根Tk()窗口時出現在前面。 –

回答

1

下面是一個簡單的例子,它將打開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() 
+0

這是鎖定我的Toplevel窗口到我的根Tk窗口,這是偉大的。然而,當我再次點擊主窗口時,Toplevel窗口仍然沒有彈出。我打開Toplevel,點擊讓我們說我已經打開的文本文件,然後點擊任務欄中的用戶界面,只彈出根Tk窗口。 Toplevel窗口隱藏在該文本文件的後面,我必須專門點擊Toplevel窗口才能讓它們彈出到前面。 –

+0

它適合我。我唯一注意到的是,如果點擊Chrome之外的任何應用程序,然後重新選擇它,只會顯示其中一個頂層窗口。我大概可以解決這個問題,但是如果我點擊根窗口上的任何位置,2個頂部窗口會立即提升到主窗口上的位置。 –

+0

好的。我會看看我的實施,並讓你知道這裏。 Ty –

相關問題