I will try to simplify my question here. My problem is like, on my first window of tkinter I have a button that opens another tkinter window. You can say it a second window to keep it on top I use win2.attributes('-topmost', True)
. Then I have a browse button to import file from the computer but when I click it goes under the window 2.
Following is my code.
from tkinter import *
from tkinter.filedialog import askopenfilename
root = Tk()
root.title("Base Window")
root.geometry("300x300")
def browse():
file_to_open = askopenfilename()
def new_window():
win2 = Toplevel(root)
win2.title("Window 2")
win2.geometry("300x300")
win2.attributes('-topmost', True)
Button2 = Button(win2, text="Browse", command=browse).pack()
button1 = Button(root, text="New window", command=new_window).pack()
root.mainloop()
My browse window is going under the window 2. Can you please suggest me the best way to keep it on top. I am sharing the screenshot of the problem as well
Starting code
Click on new window
Click browse button and the browse window is hidden
Cheers
CodePudding user response:
You need to set the parent
option of askopenfilename()
to the toplevel window win2
:
from tkinter import *
from tkinter.filedialog import askopenfilename
root = Tk()
root.title("Base Window")
root.geometry("300x300")
def browse(parent):
file_to_open = askopenfilename(parent=parent)
def new_window():
win2 = Toplevel(root)
win2.title("Window 2")
win2.geometry("300x300")
win2.attributes('-topmost', True)
# pass 'win2' to browse()
Button2 = Button(win2, text="Browse", command=lambda:browse(win2)).pack()
button1 = Button(root, text="New window", command=new_window).pack()
root.mainloop()