[英]Display toplevel window on another fullscreen window tkinter python
我對 tkinter window 的全屏屬性有疑問。 主要的 window 具有全屏屬性,第二個必須顯示在主要的頂部而不會在點擊外部時迷路。 請問有什么幫助嗎?
# add the necessairy librairy
import tkinter as tk
def Region_windows2():
#master is the second window with toplevel option
master = tk.Toplevel(Mafenetre) #removed your mainframe class
w = 300 # width for the Tk root
h = 200 # height for the Tk root
# get screen width and height
ws = master.winfo_screenwidth() # width of the screen
hs = master.winfo_screenheight() # height of the screen
# calculate x and y coordinates for the Tk master window
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
# set the dimensions of the screen
# and where it is placed
master.geometry('%dx%d+%d+%d' % (w, h, x, y))
# when open the second window i want it to be on toplevel; it means when i click outside the frame it won't get hide
master.attributes("-topmost", True)
master.title("password")
# here's the main window
Mafenetre = tk.Tk()
#set main window title
Mafenetre.title("GUI")
Mafenetre['bg']='white' # couleur de fond
# get screen width and height
wf1= Mafenetre.winfo_screenwidth()
hf1= Mafenetre.winfo_screenheight()
A = str(wf1)
B = str(hf1)
# set the dimensions of the screen
# and where it is placed
Mafenetre.geometry(A+"x"+B)
Mafenetre.attributes('-fullscreen', True)
# add menubar to the main window
menubar = tk.Menu(Mafenetre, bg='#1f1b13', fg='white')
# menubar button to test the second window
menubar.add_command(label="tester", command=Region_windows2)
# add menubr to the main window
Mafenetre.config(menu=menubar)
Mafenetre.mainloop()
此問題取決於 window 管理器如何處理全屏 windows,因此,在某些情況下,只需使用master.attributes("-topmost", True)
就足以將頂層保持在主 Z05B8C742702FBF2DEZFC13A 之上但是,就我而言(Linux、XFCE 桌面環境),它不起作用。 我發現的解決方法是在頂層失去焦點時重新調整焦點並綁定到<FocusOut>
:
master.bind('<FocusOut>', lambda ev: master.focus_force())
在Region_windows2()
中。
請注意,此解決方法顯然會阻止在打開頂層時在主 window 中的Text
或Entry
小部件中寫入任何內容,但如果主 window 被“禁用”(例如在頂層使用.grab_set()
,這不是問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.