I have a main window open. I don't want to close this window. (which seems to be in most of these questions).
when I click on a button a small window opens under the mouse position. I want this second window to close when either one of its buttons are pressed or after the mouse moves away.
My problem is when I go into a function from either of the above actions I do not seem to have a reference to close this window. as in 'alt.destroy'
My thought is to use the mouse moves away option as it should cover all needs.
Here is a sample code of what I am trying to do.
from tkinter import *
backGd='#333333'
FrGd='white'
def AlignmentTypes():
alt_x_y=ListWinStartPsn()
alt = Tk()
alt.geometry(f"100x150+{alt_x_y[0]}+{alt_x_y[1]+20}")
#alt.configure(bg=backGd)
alt.bind("<Leave>", closeAltWin)
Titlelabel=Label(alt, text="Select Alignment Type",bg=backGd,fg=FrGd)
alLeft_btn=Button(alt,text="Align Left",fg="Dark Blue",width=11)
alCenter_btn=Button(alt,text="Align Center",fg="Dark Blue",width=11)
alRight_btn=Button(alt,text="Align Right",fg="Dark Blue",width=11)
alJustify_btn=Button(alt,text="Align Justify",fg="Dark Blue",width=11)
Titlelabel.pack()
alLeft_btn.pack()
alCenter_btn.pack()
alRight_btn.pack()
alJustify_btn.pack()
def ListWinStartPsn():
x, y = btn1.winfo_rootx(), btn1.winfo_rooty()
return(x,y)
def closeAltWin(event):
pass
#alt.destroy
root = Tk()
root.title('Workopedia')
root.geometry('500x300')
frame1=Frame(root)
frame1.pack()
btn1=Button(frame1,text="click",command=AlignmentTypes)
btn1.pack()
frame2=Frame(root)
frame2.pack()
lbl1=Label(frame1,text="Stuff")
lbl1.pack()
root.mainloop()
Closing the second window while leaving it is not possible as it is a default behavior. But when the second window is open, if you enter into the first window, the second window can be closed.
The difference between the above two is: leaving the second window means(or leaving any window) when the second window is open, when you switch to any other program in your computer(either with alt + tab
or using mouse), Even this is leaving the second window. This is because : When you're working in the second window, suddenly if you get any phone call and you want to change to new program in your computer to take the detail, You can't save and close the window, go to another window and come back. So, it is designed in such a way the window can't be closed without any action by the user.
Entering the first window is almost like leaving the second window. Only thing if you move your mouse to the first window, when the second window is open, then the second window will close. Only thing, If you move the mouse to some other position of your desktop but not to the first window, then the second window won't close.
The following code will close the second window if you press any of the buttons in the second window or if you enter first window
from tkinter import *
backGd='#333333'
FrGd='white'
# declaring a dict as I don't want to declare root as global variable
dct = {}
def AlignmentTypes():
# we already defined as dct['window'] is root. But giving a variable name for dct['window']
# Let the name of the variable be 'root' again for the sake of convenience.
root = dct['window']
#The functions are defined in the second window so that these functions can be accessed by 2nd window.
def close_window(*event):
#Unbind root with '<Enter>' to avoid error while moving the mouse to root window after the 2nd window is closed
root.bind('<Enter>', lambda e:'break')
#Close the second winsow
alt.destroy()
# This function also defned inside the second window as it is used in 2nd window
def ListWinStartPsn():
x, y = btn1.winfo_rootx(), btn1.winfo_rooty()
return(x,y)
alt_x_y=ListWinStartPsn()
alt = Tk()
alt.geometry(f"100x150+{alt_x_y[0]}+{alt_x_y[1]+20}")
#alt.configure(bg=backGd)
# map all the buttons with the function close_window
Titlelabel=Label(alt, text="Select Alignment Type",bg=backGd,fg=FrGd)
alLeft_btn=Button(alt,text="Align Left",fg="Dark Blue",width=11, command = close_window)
alCenter_btn=Button(alt,text="Align Center",fg="Dark Blue",width=11, command = close_window)
alRight_btn=Button(alt,text="Align Right",fg="Dark Blue",width=11, command = close_window)
alJustify_btn=Button(alt,text="Align Justify",fg="Dark Blue",width=11, command = close_window)
Titlelabel.pack()
alLeft_btn.pack()
alCenter_btn.pack()
alRight_btn.pack()
alJustify_btn.pack()
# Bind root, <Enter> with the function close_window.
root.bind('<Enter>', close_window)
root = Tk()
root.title('Workopedia')
root.geometry('500x300')
# to avoid global vaiable, make root as a value of the dict
dct['window'] = root
frame1=Frame(root)
frame1.pack()
btn1=Button(frame1,text="click",command=AlignmentTypes)
btn1.pack()
frame2=Frame(root)
frame2.pack()
lbl1=Label(frame1,text="Stuff")
lbl1.pack()
root.mainloop()