Working in a secondary window with Tkinter, when I click on the "OK" button of the Messagebox message (Record insert correctly), this secondary window disappears and is hidden at the bottom of the taskbar. So immediately click "OK" in the Messagebox, I'll find myself automatically on the main window. I don't get errors
How can I not hide the secondary window in the taskbar and then stay inside the secondary when I click the "OK" button of the Messagebox? What should I write in the "Insert?"
Code Windows Main:
window=Tk()
window.title("main")
window.attributes('-zoomed', True)
Code Windows Secondary
from aaa import bbb
def form_secondary():
root = tk.Toplevel()
root.title("secondary")
root.geometry("1920x1080 0 0")
root.config(bg="white")
root.state("normal")
Function that launches MessageBox
def insert():
if aaaa() == "" or bbbb.get() =="" or cccc.get() == "" or ddddd.get() == "" or eeee.get() == "" or fffff.get() == "":
messagebox.showerror("There is some empty field. Fill out all the fields")
return
db.insert(aaaa.get(), bbbb.get(), cccc.get(), ddddd.get(), eeee.get, fffff.get())
messagebox.showinfo("Record insert correctly ")
clearAll()
dispalyAll()
CodePudding user response:
You can keep a child window on top of its parent window by making the child window a transient window of parent window.
Since creation of window
(in main script) and root
(inside form_secondary()
in another script) windows are in different scripts, you cannot refer window
inside form_secondary()
directly. One of the way is to pass window
to form_secondary()
as an argument:
def form_secondary(parent):
root = tk.Toplevel(parent)
root.title("secondary")
root.geometry("1920x1080 0 0")
root.config(bg="white")
root.state("normal")
# make this window a transient window
root.transient(parent)
And then pass window
as an argument when calling form_secondary()
in main script:
...
window=Tk()
window.title("main")
window.attributes('-zoomed', True)
...
# assume it is called inside a function
def some_func():
...
form_secondary(window)
...
...
Update: if it is called via a menu item, then use lambda
.
For example:
editmenu.add_command(label='Database', command=lambda: name.form_secondary(window))