【发布时间】:2021-03-11 14:52:30
【问题描述】:
我对 tkinter 窗口的全屏属性有疑问。主窗口具有全屏属性,第二个窗口必须显示在主窗口的顶部,而在外部单击时不会丢失。有什么帮助吗?
# 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()
【问题讨论】:
-
刚刚运行您的代码,
Toplevel在其他地方单击时不会关闭,此master.attributes("-topmost", True)负责将其保持在顶部。 -
@AST 它必须是特定于操作系统的,因为我可以重现 OP 的问题,当我点击主窗口时,顶层消失了。
-
@j_4321 就是这样。
-
@meth 打开顶层时主窗口“禁用”是否有问题?如果没有,可以通过绑定到 FocusOut 来防止它失去焦点,从而保持顶层的顶层。但这显然会阻止在打开顶层时在主窗口的 Text 或 Entry 小部件中写入任何内容。
-
不,没有问题。我要做的就是在顶层打开时保持主窗口显示处于禁用状态。