meth
meth

Reputation: 99

Display toplevel window on another fullscreen window tkinter python

I have an issue with the fullscreen attribute of tkinter window. The main window has the fullscreen attribute and the second has to show up on top of the main without getting lost when clicking outside. Any help please?

# 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()

Upvotes: 1

Views: 795

Answers (1)

j_4321
j_4321

Reputation: 16169

This issue depends on how the window manager handles fullscreen windows and therefore, in some cases, simply using master.attributes("-topmost", True) is enough to keep the toplevel on top of the main window. However, in my case (Linux, XFCE desktop environment), it does not work. The workaround I have found is to refocus the toplevel when it looses focus with a binding to <FocusOut>:

master.bind('<FocusOut>', lambda ev: master.focus_force())

in Region_windows2().

Note that this workaround obviously prevents from writing anything in Text or Entry widgets in the main window while the toplevel is opened, but this is not an issue if the main window is "disabled" (e.g. using .grab_set() on the toplevel).

Upvotes: 3

Related Questions